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. |
Metadata
Release files for subcmdparse 0.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| subcmdparse-0.1.11.tar.gz | 10.5 kB | Without provenance | Details for subcmdparse-0.1.11.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| subcmdparse-0.1.11-py3-none-any.whl | Python 3 | none | any | Details for subcmdparse-0.1.11-py3-none-any.whl |
Total release size: 16.7 kB
Release files / subcmdparse-0.1.11.tar.gz
| Download URL | subcmdparse-0.1.11.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
34ac990f2e501276d310feeebc0d903434d85cb83a207f843fecf9aa9f973073
|
|
BLAKE2b-256 checksum How to use checksums |
20a78688d952dda4bed8b93172401970c333259b2f569a792139d40346050d7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / subcmdparse-0.1.11-py3-none-any.whl
| Download URL | subcmdparse-0.1.11-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c712bafee40374aa5a20b84e5dc4aa47858003823606788b627039f23f88eb15
|
|
BLAKE2b-256 checksum How to use checksums |
6bc0ce3d593f9cf4815bac4542a3944d76f0640d2221d0a7531c74134ebafe7a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Security for subcmdparse 0.1.11
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.