Multiple commands

Clize provides two different approaches to presenting multiple actions in one command-line interface.

Alternate actions
The program only has one primary action, but one or more auxilliary functions.
Multiple commands
The program has multiple subcommands most of which are a major function of the program.

Alternate actions

You can specify alternate functions to be run using the alt parameter on run() when specifying one function. Let’s add a --version command to echo.py:

#!/usr/bin/env python

from clize import run


def do_nothing():
    """Does nothing"""
    return "I did nothing, I swear!"


version = 0.2

def version_():
    """Show the version"""
    return 'Do Nothing version {0}'.format(version)

if __name__ == '__main__':
    run(do_nothing, alt=version_)
$ python altcommands.py --version
echo version 0.2

You can pass multiple aternate commands by passing a list to alt=. Their names are drawn from the original function names. Underscores are converted to dashes and those on the extremities are stripped away.

Alternatively, you can pass any iterable of functions or a mapping(dict or collections.OrderedDict) to run. In the case of a mapping, the keys are used without transformation to create the command names.

Multiple commands

You can specify multiple commands by passing multiple functions to run(). Alternative actions are however not compatible with this feature.

#!/usr/bin/env python
from clize import run


def add(*text):
    """Adds an entry to the to-do list.

    text: The text associated with the entry.
    """
    return "OK I will remember that."


def list_():
    """Lists the existing entries."""
    return "Sorry I forgot it all :("


if __name__ == '__main__':
    run(add, list_, description="""
        A reliable to-do list utility.

        Store entries at your own risk.
        """)
$ python multicommands.py --help
Usage: examples/multicommands.py command [args...]

A reliable to-do list utility.

Store entries at your own risk.

Commands:
  add    Adds an entry to the to-do list.
  list   Lists the existing entries.
$ python multicommands.py add A very important note.
OK I will remember that.
$ python multicommands.py list
Sorry I forgot it all :(

Alternatively, you can pass any iterable of functions or a mapping(dict or collections.OrderedDict) to run like with the alt parameter explained earlier.