Skip to main content

structure and simplify subcommand handling with argparse

Project description

argparse_subcommand

Library to extend Python argparse stdlib with easy handling of subcommands

Extends argparse.ArgumentParser with a facility for configuring subcommands by convention. Each subcommand lives in its separate Python module.
The name of the subcommand is the module name (without superpackage names) with underscore replaced by dash.

To be a subcommand module, a module must have

import argparse_subcommand as ap_sub

meaning = "some help text for the subcommand"
def add_arguments(parser: ap_sub.ArgumentParser): ...  # configure the subcommand's sub-argparser
def execute(args: ap_sub.Namespace): ...  # run the subcommand

The module can also optionally have:

aliases = ["subcmd-alias1", "subcmd-alias2"]  # optional.

for making available the same subcommand under one or more alternative names (e.g. an abbreviation).

For use, create the parser as usual and then call the submodule scanner:

parser = ap_sub.ArgumentParser(epilog=explanation)
parser.scan("mysubcmds.subcmd1", "mysubcmds.subcmd2")  # or provide module object instead of str
args = parser.parse_args()
parser.execute_subcommand(args)  # or supply nothing, then parse_args() will be called internally

By convention, the subcommand modules (and only they) all go into a common package. If you do that, you can scan them all at once:

parser.scan("mysubcmds.*")

argparse_subcommand uses only one sub-parser group, so that subcommands cannot be nested, there is only one level of subcommands. It will execute importlib.import_module() on all modules mentioned in a scan() call as strings.
Multiple calls to scan() are allowed, each can have one or more arguments.
scan(..., strict=True) will exit when encountering a non-subcommand-module.
scan(..., trace=True) produces output helpful for debugging your subcommands setup.

That's all.

Version history

  • 1.0, 2023-05:
    First release
  • 1.1, 2024-01:
    Error messages and trace messages go to stderr, not stdout.
    More informative messages when scanned modules are non-subcommand modules. Small improvements to documentation.
    Added version history.
  • ...

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

argparse_subcommand-1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

argparse_subcommand-1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file argparse_subcommand-1.1.tar.gz.

File metadata

  • Download URL: argparse_subcommand-1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.2 Linux/5.10.102.1-microsoft-standard-WSL2

File hashes

Hashes for argparse_subcommand-1.1.tar.gz
Algorithm Hash digest
SHA256 534c05a6d293ad96f7c72a868cefb028bf4d57eea97205d8ff1790bd5792825c
MD5 a7feb57a1c28f9ebcbedf9f00e524a56
BLAKE2b-256 f80a80478365e410eabfe1613d4a1efc64e14a2301c85f8649a8d7311a3ad871

See more details on using hashes here.

File details

Details for the file argparse_subcommand-1.1-py3-none-any.whl.

File metadata

  • Download URL: argparse_subcommand-1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.2 Linux/5.10.102.1-microsoft-standard-WSL2

File hashes

Hashes for argparse_subcommand-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5852e841f0392bd97e12561b03ad21bf9c16cbc3965d48a2849bc6f0b349297
MD5 d81491e7ba7111c729d4e485bf3dcab9
BLAKE2b-256 6c506acc69b8c6031a90c7f159446a9117ec3909e715aa97f63a3b44c3b90eee

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