Add automatic entry point and example
This commit is contained in:
parent
b2da98add7
commit
7ba4bcefbe
18
docs/example.py
Normal file
18
docs/example.py
Normal 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}!')
|
||||
@ -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)
|
||||
27
readme.md
27
readme.md
@ -1,4 +1,4 @@
|
||||
# magicli
|
||||
# magiᴄʟɪ✨
|
||||
|
||||
Automatically call args parsed by `docopt` as functions.
|
||||
|
||||
@ -15,7 +15,6 @@ Basic usage example.
|
||||
```python
|
||||
from docopt import docopt
|
||||
from magicli import magicli
|
||||
|
||||
def cli():
|
||||
magicli(docopt(__doc__))
|
||||
```
|
||||
@ -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 <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
|
||||
@ -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!
|
||||
|
||||
8
setup.py
8
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']}
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user