File: README.md

package info (click to toggle)
python-asyncclick 8.3.0.5%2Basync-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,664 kB
  • sloc: python: 14,154; makefile: 12; sh: 10
file content (120 lines) | stat: -rw-r--r-- 3,973 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# $ asyncclick_

Asyncclick is a fork of Click (described below) that works with trio or asyncio.

AsyncClick allows you to seamlessly use async command and subcommand handlers.


<div align="center"><img src="https://raw.githubusercontent.com/pallets/click/refs/heads/stable/docs/_static/click-name.svg" alt="" height="150"></div>

# Click

Click is a Python package for creating beautiful command line interfaces
in a composable way with as little code as necessary. It's the "Command
Line Interface Creation Kit". It's highly configurable but comes with
sensible defaults out of the box.

It aims to make the process of writing command line tools quick and fun
while also preventing any frustration caused by the inability to
implement an intended CLI API.

Click in three points:

-   Arbitrary nesting of commands
-   Automatic help page generation
-   Supports lazy loading of subcommands at runtime


## A Simple Example

```python
import asyncclick as click
import anyio

@click.command()
@click.option("--count", default=1, help="Number of greetings.")
@click.option("--name", prompt="Your name", help="The person to greet.")
async def hello(count, name):
    """Simple program that greets NAME for a total of COUNT times."""
    for _ in range(count):
        click.echo(f"Hello, {name}!")
        await anyio.sleep(0.2)

if __name__ == '__main__':
    hello()
    # alternately: anyio.run(hello.main)
```

```
$ python hello.py --count=3
Your name: Click
Hello, Click!
Hello, Click!
Hello, Click!
```

## Differences to Click

This async-ized version of Click is mostly backwards compatible for "normal" use:
you can freely mix sync and async versions of your command handlers and callbacks.

Several advanced methods, most notably :meth:`BaseCommand.main`, and
:meth:`Context.invoke`, are now asynchronous.

The :meth:`BaseCommand.__call__` alias now invokes the main entry point via
`anyio.run`. If you already have an async main program, simply use
``await cmd.main()`` instead of ``cmd()``.

:func:`asyncclick.prompt` is asyncronous and accepts a ``blocking`` parameter
that switches between "doesn't affect your event loop but has unwanted effects when
interrupted" (bugfix pending) and "pauses your event loop but is safe to interrupt"
with Control-C". The latter is the default until we fix that bug.

You cannot use Click and AsyncClick in the same program. This is not a problem
in practice, as replacing ``import click`` with ``import asyncclick as click``, and
``from click import ...`` with ``from asyncclick import ...``, should be all that's
required.

### Notable packages supporting asyncclick

* [OpenTelemetry][opentelemetry] supports instrumenting asyncclick.

[opentelemetry]: https://pypi.org/project/opentelemetry-instrumentation-asyncclick/


## Donate

The Pallets organization develops and supports Click and other popular
packages. In order to grow the community of contributors and users, and
allow the maintainers to devote more time to the projects, [please
donate today][].

[please donate today]: https://palletsprojects.com/donate

The AsyncClick fork is maintained by Matthias Urlichs <matthias@urlichs.de>.

## Contributing

### Click

See our [detailed contributing documentation][contrib] for many ways to
contribute, including reporting issues, requesting features, asking or answering
questions, and making PRs.

[contrib]: https://palletsprojects.com/contributing/

### AsyncClick

You can file async-specific issues, ideally including a corresponding fix,
to the [MoaT/asyncclick][moat] repository on github.

[moat]: https://github.com/M-o-a-T/asyncclick

#### Testing

If you find a bug, please add a testcase to prevent it from recurring.

In tests, you might wonder why `runner.invoke` is not called asynchronously.
The reason is that there are far too many of these calls to modify them all.
Thus ``tests/conftest.py``  contains a monkeypatch that turns this call
into a thread that runs this call using `anyio.run`.