All the commands registration and execution is handled by the CliApp class. You need to create an instance of it on top of your main module, and then call its .run() method when you’re ready to go:
from clitools import CliApp
cli = CliApp()
# ...
if __name__ == '__main__':
cli.run()
Each command is simply a function to be called when the appropriate sub-command is selected.
To register a new command, simply use the CliApp().command decorator.
Arguments, options, documentation, etc. are all taken from function arguments and docstring.
For example:
@cli.command
def hello(name):
print("Hello, {0}!".format(name))
You’ll get a sub-command named hello, accepting a single (required) positional argument, name.
If you invoke the script like this:
% ./my-script.py hello world
You’ll get the string Hello, world! on the output.
Suppose we’d now want to make the argument optional:
@cli.command
def hello(name='world'):
print("Hello, {0}!".format(name))
Let’s invoke it:
% ./my-script.py
Hello, world!
% ./my-script.py --name Python
Hello, Python!
The arguments generation pattern is quite smart, to try to guess the appropriate argument type based on the argument default value.
Warning
There is no support for variable arguments / kwargs (yet). If you use them in your command function, they will simply be ignored.
You can use the CliApp().arg class to manually specify arguments/kwargs to .add_argument() call.
The default value you specify will then be set as the actual default value on the function:
@cli.command
def hello(name=cli.arg(default='world'))
print("hello, " + name)
% ./my-script.py hello
hello, world
% ./my-script.py hello --name=python
hello, python
>>> hello()
hello, world
>>> hello(name='python')
hello, python