Skip to main content

Automatically call args parsed by `docopt` as functions.

Project description

magiᴄʟɪ✨

Automatically call args parsed by docopt as functions.

Install

pip install 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

After installing magicli, this example can be called from the command line.

hello.py

"""
    Usage:
        magicli_example <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}!')

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

from setuptools import setup


setup(
    name='magicli_example',
    version='0.1.0',
    install_requires=[
        'magicli'
    ],
    entry_points={
        'console_scripts':[
            'magicli_example=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:

magicli_example World -a 3

Results in the following output:

Hello World!
Hello World!
Hello World!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magicli-0.1.1.tar.gz (2.7 kB view hashes)

Uploaded Source

Built Distribution

magicli-0.1.1-py3-none-any.whl (3.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page