From b2da98add79fb2af56b4c2c7127e88256cf33609 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 11 Jul 2022 20:10:55 +0900 Subject: [PATCH] Changes to readme --- magicli/magicli.py | 2 +- readme.md | 76 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/magicli/magicli.py b/magicli/magicli.py index bf512da..5dcc5c1 100644 --- a/magicli/magicli.py +++ b/magicli/magicli.py @@ -1,5 +1,5 @@ """ -# Getting started +# Get started ```python from docopt import docopt diff --git a/readme.md b/readme.md index e338d34..6959468 100644 --- a/readme.md +++ b/readme.md @@ -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 [--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! +```