From 7ba4bcefbef95ba40b858972c3b29e5ea121ea02 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 12 Jul 2022 08:39:09 +0900 Subject: [PATCH] Add automatic entry point and example --- docs/example.py | 18 ++++++++++++++++++ magicli/magicli.py => magicli.py | 8 +++++++- magicli/__init__.py | 0 readme.md | 29 +++++++++++++++++++---------- setup.py | 8 +++++--- 5 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 docs/example.py rename magicli/magicli.py => magicli.py (84%) delete mode 100644 magicli/__init__.py diff --git a/docs/example.py b/docs/example.py new file mode 100644 index 0000000..7d71a05 --- /dev/null +++ b/docs/example.py @@ -0,0 +1,18 @@ +""" + Usage: + hello [--amount=] + + -a= --amount= How often to greet +""" + +from docopt import docopt +from magicli import magicli + + +def cli(): + magicli(docopt(__doc__)) + + +def main(name, amount = 1): + for _ in range(int(amount)): + print(f'Hello {name}!') diff --git a/magicli/magicli.py b/magicli.py similarity index 84% rename from magicli/magicli.py rename to magicli.py index 5dcc5c1..b510d23 100644 --- a/magicli/magicli.py +++ b/magicli.py @@ -12,7 +12,7 @@ args = magically(docopt(__doc__)) import inspect -def magicli(args, glbls=None): +def magicli(args, glbls=None, entry_point='main'): """ Calls all callable functions with all arguments. """ @@ -22,6 +22,12 @@ def magicli(args, glbls=None): cleaned_args = clean_args(args) args = args_set_in_cli(cleaned_args) + + # Add main function to possible callable functions. + # Main function will be called if it exists. + if entry_point not in args: + args[entry_point] = True + for arg in args: if arg in glbls: func = glbls.get(arg) diff --git a/magicli/__init__.py b/magicli/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/readme.md b/readme.md index 6959468..f80a874 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# magicli +# magiᴄʟɪ✨ Automatically call args parsed by `docopt` as functions. @@ -15,12 +15,11 @@ 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 `_`) . +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. @@ -32,14 +31,16 @@ args = magicli(docopt(__doc__)) ## Minimal `Hello World` example +After installing, this example can be called from the command line + ### hello.py ```python """ Usage: - hello say [--amount ] + magicli_example [--amount=] - -a, --amount How often do greet + -a= --amount= How often to greet """ from docopt import docopt @@ -50,36 +51,44 @@ def cli(): magicli(docopt(__doc__)) -def say(name, amount = 1): +def main(name, amount = 1): for _ in range(int(amount)): print(f'Hello {name}!') ``` +Note that `magicli` will automatically try to call the `main()` function. + +This can be changed to another function through the settings `magicli(docopt(__doc__), entry_point='another_function')`. + ### setup.py + ```python from setuptools import setup setup( - name='hello', + name='magicli_example', version='0.1.0', install_requires=[ 'docopt' ], entry_points={ 'console_scripts':[ - 'hello=hello:cli' + 'magicli_example=docs.example:cli' ] } ) ``` +Note: Make sure that the entry point specified in your setup.py is not the main function, otherwise it will not work. + Calling the script: ``` -hello say World 3 +magicli_example World -a 3 ``` -results in the following output + +Results in the following output: ``` Hello World! diff --git a/setup.py b/setup.py index 14efc85..f254bac 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from setuptools import setup, find_packages +from setuptools import setup with open('readme.md') as f: @@ -10,8 +10,9 @@ setup( version='0.1.0', description='Automatically call args parsed by `docopt` as functions.', long_description=long_description, - packages=find_packages(), - install_requires=[], + install_requires=[ + 'docopt' + ], extras_require={ 'tests':[ 'pytest', @@ -19,4 +20,5 @@ setup( }, keywords=[], classifiers=[], + entry_points={'console_scripts':['magicli_example=docs.example:cli']} )