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:
def main(argv: list[str]):
parser = ap_sub.ArgumentParser(epilog=explanation)
parser.scan("mysubcmds.subcmd1", "mysubcmds.subcmd2") # or provide module object instead of str
args = parser.parse_args(argv[1:])
parser.execute_subcommand(args) # or supply nothing, then parse_args() will be called internally
if __name__ == '__main__':
main(sys.argv)
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.*", strict=True)
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=False) (the default) will ignore non-subcommand modules.
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. - 1.2, 2024-03: Better and clearer pattern for the main routine in the documentation.
- ...
Metadata
Release files for argparse_subcommand 1.2
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 | |
|---|---|---|---|---|
| argparse_subcommand-1.2.tar.gz | 4.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| argparse_subcommand-1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.5 kB
Release files / argparse_subcommand-1.2.tar.gz
| Download URL | argparse_subcommand-1.2.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0fb3f2055fd845ba48268879d67ae0632948d379015e2853efb15c38bcc7965a
|
|
BLAKE2b-256 checksum How to use checksums |
55dae4bfceb9698149f8c839b2d91189dc6f95f0f1c35ee94b7eb2b09828ee4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.2 Linux/5.10.102.1-microsoft-standard-WSL2
|
Release files / argparse_subcommand-1.2-py3-none-any.whl
| Download URL | argparse_subcommand-1.2-py3-none-any.whl |
|---|---|
| Size | 7.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce0718bbd51448203adb603af84d0a78bb514a1492bb6ba84bf93dd4fa82b3c5
|
|
BLAKE2b-256 checksum How to use checksums |
f7836a153bb8ba00c6616db04ad6a7f40f5bee152485f0d4584b63d55a79acd6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.11.2 Linux/5.10.102.1-microsoft-standard-WSL2
|
Security for argparse_subcommand 1.2
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.