magicli/readme.md
2022-07-11 20:10:55 +09:00

89 lines
1.5 KiB
Markdown

# magicli
Automatically call args parsed by `docopt` as functions.
## Install
```
pip install git+https://git.beelm.eu/patrick/magicli
```
## Get started
Basic usage example.
```python
from docopt import docopt
from magicli import magicli
def cli():
magicli(docopt(__doc__))
```
Functions that share a name with the keys or `dict` returned by `docopt` are automatically called with all required args if specified (Note that keys are converted to valid python function names, i.e. stripping the characters `<` `>` `-` and replacing `-` with `_`) .
All functions that are called this way need to be imported.
`magicli` also returns a dict with converted keys, and these can be used as `args` if needed.
```python
args = magicli(docopt(__doc__))
```
## Minimal `Hello World` example
### hello.py
```python
"""
Usage:
hello say <name> [--amount <amount>]
-a, --amount How often do greet
"""
from docopt import docopt
from magicli import magicli
def cli():
magicli(docopt(__doc__))
def say(name, amount = 1):
for _ in range(int(amount)):
print(f'Hello {name}!')
```
### setup.py
```python
from setuptools import setup
setup(
name='hello',
version='0.1.0',
install_requires=[
'docopt'
],
entry_points={
'console_scripts':[
'hello=hello:cli'
]
}
)
```
Calling the script:
```
hello say World 3
```
results in the following output
```
Hello World!
Hello World!
Hello World!
```