Skip to main content

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.

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)

Source distribution for subcmdparse 0.1.11
File Size Uploaded
subcmdparse-0.1.11.tar.gz 10.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for subcmdparse 0.1.11
File Interpreter ABI Platform
subcmdparse-0.1.11-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.1.11 This release

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page