Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

subcmdparse-0.1.11.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

subcmdparse-0.1.11-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file subcmdparse-0.1.11.tar.gz.

File metadata

  • Download URL: subcmdparse-0.1.11.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

Hashes for subcmdparse-0.1.11.tar.gz
Algorithm Hash digest
SHA256 34ac990f2e501276d310feeebc0d903434d85cb83a207f843fecf9aa9f973073
MD5 01045171d8dc4113bcc4394a2d7d1f78
BLAKE2b-256 20a78688d952dda4bed8b93172401970c333259b2f569a792139d40346050d7a

See more details on using hashes here.

File details

Details for the file subcmdparse-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: subcmdparse-0.1.11-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

Hashes for subcmdparse-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 c712bafee40374aa5a20b84e5dc4aa47858003823606788b627039f23f88eb15
MD5 067471a0cd06f0ba4e2cc004bfb05996
BLAKE2b-256 6bc0ce3d593f9cf4815bac4542a3944d76f0640d2221d0a7531c74134ebafe7a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page