Subcommand extension for argparse package
Project description
subcmdparse
argparse extension for declarative subcommands. Each subcommand is a class with on_parser_init(parser) and on_command(args) hooks; a SubcommandParser ties them together and runs the right one based on sys.argv.
Adds nice-to-haves on top of stock argparse: a --help-all action that prints help for the whole tree, optional argcomplete integration, tolerant subparser dispatch, and allow_unknown_args passthrough.
Python ≥ 3.7. Optional: argcomplete.
Install
pip install -e .
Quick start
from subcmdparse import Subcommand, SubcommandParser
class Greet(Subcommand):
def on_parser_init(self, parser):
parser.add_argument("name")
parser.add_argument("--shout", action="store_true")
def on_command(self, args):
msg = f"hello, {args.name}"
print(msg.upper() if args.shout else msg)
class Echo(Subcommand):
def on_parser_init(self, parser):
parser.add_argument("text", nargs="+")
def on_command(self, args):
print(" ".join(args.text))
if __name__ == "__main__":
p = SubcommandParser(prog="demo")
p.add_subcommands(Greet(), Echo())
p.exec_subcommands()
$ python demo.py greet Alice --shout
HELLO, ALICE
API
from subcmdparse import Subcommand, SubcommandParser
Subcommand
Base class for a single subcommand. Override:
| Hook | Purpose |
|---|---|
on_parser_init(self, parser) |
Add this subcommand's arguments to its own ArgumentParser. |
on_command(self, args, unknown_args=None) |
Run when this subcommand is selected. unknown_args is populated when allow_unknown_args=True. |
on_exception(self, exc) (optional) |
Custom error handler for exceptions raised inside on_command. |
Class attributes you can set instead of constructor args: name (CLI name, defaults to class name lowercased), help, description, allow_unknown_args.
Subcommands can themselves contain nested Subcommands — pass them to SubcommandParser.add_subcommands inside a parent's on_parser_init, or expose them via the class's nested-subcommands attribute.
SubcommandParser(*args, argcomplete=False, add_help_all=True, **kwargs)
Subclass of argparse.ArgumentParser.
| Method / property | Description |
|---|---|
add_subcommands(*subcommands, title=None, required=True, help=None, metavar='SUBCOMMAND') |
Register one or more Subcommand instances. |
parse_args(...) |
As in argparse; also registers pending subcommands and runs argcomplete. |
exec_subcommands(parsed_args=None) |
Parse args (if not given) and dispatch to the selected subcommand's on_command. |
add_help (read/write) |
Enable/disable the standard -h/--help after construction. |
allow_unknown_args (read/write) |
When True, unrecognised args are passed to on_command instead of erroring. |
argcomplete |
When True, calls argcomplete.autocomplete(self) during parse_args. |
add_help_all |
When True (default), exposes --help-all which prints help for every subcommand recursively. |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file subcmdparse-0.1.10.tar.gz.
File metadata
- Download URL: subcmdparse-0.1.10.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c944a09b29f024d31146a2481160e338e187d9b2b3f6ff42850bc6491db641ae
|
|
| MD5 |
b2d425f729eb1f6aec1ad691f2cd4d1c
|
|
| BLAKE2b-256 |
5b02be12c6bec666c600828f5470afe745f615a50dd71284203f4b013e7be931
|
File details
Details for the file subcmdparse-0.1.10-py3-none-any.whl.
File metadata
- Download URL: subcmdparse-0.1.10-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c570f6830956341a8d7ff370125eb0504167f51e61e7651a74a1ea3619985bd
|
|
| MD5 |
d59bb09df04f6c8b9e1e5dc21c95b645
|
|
| BLAKE2b-256 |
c2298bec4a6de8e07a948a4c49480aecc25ecff6a39d71a67f625ac92f46024c
|