Changes to readme

This commit is contained in:
Patrick Elmer 2022-07-11 20:10:55 +09:00
parent 325551343b
commit b2da98add7
2 changed files with 75 additions and 3 deletions

View File

@ -1,5 +1,5 @@
"""
# Getting started
# Get started
```python
from docopt import docopt

View File

@ -4,13 +4,85 @@ Automatically call args parsed by `docopt` as functions.
## Install
`pip install git+https://git.beelm.eu/patrick/magicli`
```
pip install git+https://git.beelm.eu/patrick/magicli
```
## Getting started
## 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!
```