/subcommand-py

Dynamic dispatch to CLI subcommands.

Primary LanguagePython

subcommand-pattern

Testing various subcommand patterns.

Tip

Each structure has its own git tag. Open them to see the differences:

Structure

./
├── commands/
│   ├── bash_script
│   ├── bash_script.py
│   ├── clone.py
│   └── echo.py
└── main.py

main.py

Entrypoint.

Registers all subcommands and does the actual parsing.

commands/__init__.py

Imports all subcommands beneath it.

Subcommand Structure

A subcommand must have a setup function with the following signature:

def setup(subparsers: argparse._SubParsersAction) -> None:
    cmd = subparsers.add_parser(...)
    cmd.set_defaults(run=...)