Building argument parser from a function annotation. A simple utility inspired by Fire and docopt. Ad-hoc solution for someone who often writes scripts with a single entry point.
How?
The process of building CLI with ancli is very simple.
Write a plain Python function with annotated parameters.
Wrap it with make_cli.
Run your script.
Examples
1. Function with annotated parameters
The function run has explicitly annotated parameters and its signature is used to instantiate argparse.ArgumentParser instance that accepts parameters with specific types and default (if any) parameters. If default value is not provided, then the parameter is considered to be required.
from ancli import make_cli
def run(path: str, flag: bool = True, iterations: int = 1):
print(f'run: path={path}, flag={flag}, iterations={iterations}')
if __name__ == '__main__':
make_cli(run)
Now this snippet can be used as follows.
$ python script.py --path file.txt --flag 0
run: path=file.txt, flag=False, iterations=1
2. Function without annotations
The functions without type annotations try to infer the parameters types based on their default values.
from ancli import make_cli
def run(a, b=2, c=3.0):
for param in (a, b, c):
print(type(param))
if __name__ == '__main__':
make_cli(run)
The parameters without default values are considered as strings.
$ python script.py --a 1 --b 2 --c 3.0
<type 'str'>
<type 'int'>
<type 'float'>
3. Running ancli as a module
Running package as a module allows to dynamically build a CLI from some function. You just need to specify a path to the module, and function which should be treated as an entry point.
$ python -m ancli examples.functions:compute --a 2 --b 6
42
Metadata
Release files for ancli 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ancli-0.1.3.tar.gz | 3.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ancli-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.9 kB
Release files / ancli-0.1.3.tar.gz
| Download URL | ancli-0.1.3.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2d7af39b313ba45a72618b304a8bd12c076f45786ced273ea6bdbcf8f9bf6585
|
|
BLAKE2b-256 checksum How to use checksums |
c0dc7fde3ad1c2583c787b55da57c6cee38525c99f1ef76956c22d7e2d79139c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
|
Release files / ancli-0.1.3-py3-none-any.whl
| Download URL | ancli-0.1.3-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ef3165fa9799d260c8131981ddf5b99786708d460c24c5ce1371f7d0c35267a2
|
|
BLAKE2b-256 checksum How to use checksums |
dfa59e1e170ebfebfe50287fb74a473871122efa0a91b2b2d3feec0528e3fb2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
|