magicli/README.md

106 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# magiʟɪ
Automatically turn functions of file into command line interface.
## Install
```
pip install magicli
```
## Get started
Hello world example.
Docstrings can be added to variables by using `typing.Annotated`.
A short option can be added as a third argument to `Annotated`.
By default the first function is called automatically. All following functions in the file are treated as commands. Functions starting with an underscore are ignored.
```python
from typing import Annotated
def hello(
name: Annotated[str, 'Person to call'],
amount: Annotated[int, 'Person to call', 'a']=1,
):
for _ in range(amount):
print(f"Hello {name}!")
if __name__ == '__main__':
import magicli
```
```bash
$ hello world 3
Hello world!
Hello world!
Hello world!
```
### Define name of CLI in `setup.py`
In order to define the name of the CLI, it needs to be defined in the `setup.py` file. The following code sets up a sample CLI with the following folder structure.
```bash
hello/
└── setup.py
└── hello.py
```
`setup.py`
```python
from setuptools import setup
setup(
name='hello',
version='0.1.0',
install_requires=[
'magicli'
],
entry_points={
'console_scripts':[
'hello=hello:main'
]
}
)
```
`hello.py`
```python
from magicli import magicli
def main():
magicli()
def hello(name='World', amount=1):
for _ in range(int(amount)):
print(f'Hello {name}!')
```
The script can then be called in the following way.
```bash
hello Name --amount 3
```
This outputs
```bash
Hello Name!
Hello Name!
Hello Name!
```
### Help message
By default, a help message will be created based on the available functions.
For the example above, calling `hello --help` will display this help message.
```bash
Usage:
hello --name --amount
```