Add automatic entry point and example

This commit is contained in:
Patrick Elmer 2022-07-12 08:39:09 +09:00
parent b2da98add7
commit 7ba4bcefbe
5 changed files with 49 additions and 14 deletions

18
docs/example.py Normal file
View File

@ -0,0 +1,18 @@
"""
Usage:
hello <name> [--amount=<int>]
-a=<int> --amount=<int> 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}!')

View File

@ -12,7 +12,7 @@ args = magically(docopt(__doc__))
import inspect import inspect
def magicli(args, glbls=None): def magicli(args, glbls=None, entry_point='main'):
""" """
Calls all callable functions with all arguments. Calls all callable functions with all arguments.
""" """
@ -22,6 +22,12 @@ def magicli(args, glbls=None):
cleaned_args = clean_args(args) cleaned_args = clean_args(args)
args = args_set_in_cli(cleaned_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: for arg in args:
if arg in glbls: if arg in glbls:
func = glbls.get(arg) func = glbls.get(arg)

View File

View File

@ -1,4 +1,4 @@
# magicli # magiᴄʟɪ✨
Automatically call args parsed by `docopt` as functions. Automatically call args parsed by `docopt` as functions.
@ -15,12 +15,11 @@ Basic usage example.
```python ```python
from docopt import docopt from docopt import docopt
from magicli import magicli from magicli import magicli
def cli(): def cli():
magicli(docopt(__doc__)) 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. All functions that are called this way need to be imported.
@ -32,14 +31,16 @@ args = magicli(docopt(__doc__))
## Minimal `Hello World` example ## Minimal `Hello World` example
After installing, this example can be called from the command line
### hello.py ### hello.py
```python ```python
""" """
Usage: Usage:
hello say <name> [--amount <amount>] magicli_example <name> [--amount=<int>]
-a, --amount How often do greet -a=<int> --amount=<int> How often to greet
""" """
from docopt import docopt from docopt import docopt
@ -50,36 +51,44 @@ def cli():
magicli(docopt(__doc__)) magicli(docopt(__doc__))
def say(name, amount = 1): def main(name, amount = 1):
for _ in range(int(amount)): for _ in range(int(amount)):
print(f'Hello {name}!') 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 ### setup.py
```python ```python
from setuptools import setup from setuptools import setup
setup( setup(
name='hello', name='magicli_example',
version='0.1.0', version='0.1.0',
install_requires=[ install_requires=[
'docopt' 'docopt'
], ],
entry_points={ entry_points={
'console_scripts':[ '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: Calling the script:
``` ```
hello say World 3 magicli_example World -a 3
``` ```
results in the following output
Results in the following output:
``` ```
Hello World! Hello World!

View File

@ -1,4 +1,4 @@
from setuptools import setup, find_packages from setuptools import setup
with open('readme.md') as f: with open('readme.md') as f:
@ -10,8 +10,9 @@ setup(
version='0.1.0', version='0.1.0',
description='Automatically call args parsed by `docopt` as functions.', description='Automatically call args parsed by `docopt` as functions.',
long_description=long_description, long_description=long_description,
packages=find_packages(), install_requires=[
install_requires=[], 'docopt'
],
extras_require={ extras_require={
'tests':[ 'tests':[
'pytest', 'pytest',
@ -19,4 +20,5 @@ setup(
}, },
keywords=[], keywords=[],
classifiers=[], classifiers=[],
entry_points={'console_scripts':['magicli_example=docs.example:cli']}
) )