python-cli-runner

Toolkit that organize multiple entry points of your codebase with Docopt powerful.


License
Other
Install
pip install python-cli-runner==0.0.3

Documentation

About

Toolkit that organize multiple entry points of your codebase with Docopt powerful.

Quickstart

Install python-cli-runner:

$ pip install python-cli-runner

Create my_runner.py:

from cli_runner.loader import ServiceLoader
from cli_runner.base import NoArgumentService, DocoptService

class HelloWorld(NoArgumentService):
    name = 'hello'
    help = 'Command without parameters'

    def run(self):
        print 'hello world'

class FooBar(DocoptService):
    name = 'foo'
    help = """
        Parametrized command

        Usage:
            foo [--first] [--second]

        Options:
            [--first]   First parameter
            [--second]  Second parameter
    """

    def run(self):
        print 'bar'
        if self.arguments.get('--first'):
            print 'first is present'
        if self.arguments.get('--second'):
            print 'second is present'

if __name__ == '__main__':
    ServiceLoader().run()

Now you can execute it without parameters for show help screen:

$ python my_runner.py
Usage:
    my_runner.py [<service>] [...]
    my_runner.py (-h | --help)

Options:
    -h --help       This screen
    [<service>]     Run service
    [...]           Service options

Available services:
    foo    Parametrized command
    hello  Command without parameters

Non parametrized command:

$ python my_runner.py hello
hello world

Docopt command:

$ python my_runner.py foo --help
Parametrized command

Usage:
    foo [--first] [--second]

Options:
    [--first]   First parameter
    [--second]  Second parameter


$ python my_runner.py foo
bar


$ python my_runner.py foo --first
bar
first is present

Use cases