2022-07-11 20:10:55 +09:00
2022-07-11 20:10:55 +09:00
2022-07-11 11:50:41 +09:00
2022-07-11 04:01:43 +02:00
2022-07-11 17:31:34 +09:00
2022-07-11 20:10:55 +09:00
2022-07-11 17:31:34 +09:00

magicli

Automatically call args parsed by docopt as functions.

Install

pip install git+https://git.beelm.eu/patrick/magicli

Get started

Basic usage example.

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.

args = magicli(docopt(__doc__))

Minimal Hello World example

hello.py

"""
    Usage:
    hello say <name> [--amount <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

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!
Description
Automatically turn functions of file into command line interface.
https://pypi.org/project/magicli/
Readme
Languages
Python 100%