Quickstart
Create a two-command hello-world. Save this as hello.py:
- Subcommand parser — inherits from
Parserlike the root app. - Optional flag — same kwargs as
argparse.add_argument(). cli_runis what runs for this command; parameters match argument destinations.- Second subcommand parser.
- Repeatable option via
action="append". - Parent options (
debug,config) are available on childcli_runtoo. - Root application parser.
- Global options live on the root and apply to every subcommand.
- Bind a child with
Command(...)(argparse subparser under the hood). - Second binding — the attribute name becomes the CLI name (
command2). - Instantiating the root parses
sys.argvand runs the matched command.
Make it executable (or call it with Python) and inspect help:
$ python hello.py --help
usage: hello.py [-h] [--debug] [--config CONFIG] {command1,command2} ...
Demo application with options and two subcommands.
positional arguments:
subcommands:
command1 Command 1, which says hello.
command2 Command 2, with option and positional arguments.
options:
-h, --help show this help message and exit
--debug Enable debug mode (default: False)
--config CONFIG, -c CONFIG Config file path (default: config.yaml)
Try a few calls:
What next?
- Walk through the basic guide, then nested commands.
- Add a star feature when you need it: Views, Logging, Config, Completion, Error handling.
- Browse the feature overview or the API reference.