# 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-parser
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 = 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.
This is rarely a limitation.
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.
That's all.
Raw data
{
"_id": null,
"home_page": "https://github.com/prechelt/argparse_subcommand",
"name": "argparse-subcommand",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "argparse",
"author": "Lutz Prechelt",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/05/72/e932c93f6d6c40d4affe3867d25de35fb69f34cc6f177ebac502f4fe8080/argparse_subcommand-1.0.tar.gz",
"platform": null,
"description": "# argparse_subcommand\n\nLibrary to extend Python argparse stdlib with easy handling of subcommands\n\nExtends `argparse.ArgumentParser` with a facility for configuring subcommands by convention.\nEach subcommand lives in its separate Python module. \nThe name of the subcommand is the module name (without superpackage names)\nwith underscore replaced by dash. \n\nTo be a subcommand module, a module must have \n\n```\nimport argparse_subcommand as ap_sub\n\nmeaning = \"some help text for the subcommand\"\ndef add_arguments(parser: ap_sub.ArgumentParser): ... # configure the subcommand's sub-parser\ndef execute(args: ap_sub.Namespace): ... # run the subcommand\n```\n\nThe module can also optionally have:\n\n```\naliases = [\"subcmd-alias1\", \"subcmd-alias2\"] # optional.\n```\n\nfor making available the same subcommand under one or more \nalternative names (e.g. an abbreviation).\n\nFor use, create the parser as usual and then call the submodule scanner:\n\n```\nparser = ArgumentParser(epilog=explanation)\nparser.scan(\"mysubcmds.subcmd1\", \"mysubcmds.subcmd2\") # or provide module object instead of str\nargs = parser.parse_args()\nparser.execute_subcommand(args) # or supply nothing, then parse_args() will be called internally\n```\n\nBy convention, the subcommand modules (and only they) all go into a common package.\nIf you do that, you can scan them all at once:\n\n```\nparser.scan(\"mysubcmds.*\")\n```\n\n\n`argparse_subcommand` uses only one sub-parser group, so that\nsubcommands cannot be nested, there is only one level of subcommands.\nThis is rarely a limitation. \nIt will execute `importlib.import_module()` on all modules mentioned in a `scan()` call as strings. \nMultiple calls to `scan()` are allowed, each can have one or more arguments. \n`scan(..., strict=True)` will exit when encountering a non-subcommand-module. \n\nThat's all.",
"bugtrack_url": null,
"license": "LICENSE",
"summary": "structure and simplify subcommand handling with argparse",
"version": "1.0",
"project_urls": {
"Homepage": "https://github.com/prechelt/argparse_subcommand",
"Repository": "https://github.com/prechelt/argparse_subcommand"
},
"split_keywords": [
"argparse"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "017db44842448ea31f2547c515d7b6b9d838092fbaa9c28a3f13af561abb4327",
"md5": "853ec1f59f9c9cb3602aa5bdd31bf154",
"sha256": "88ae11f2ef3cbf88aded200e5fa699716633e6bc398f01b2b87401ec4d8837e8"
},
"downloads": -1,
"filename": "argparse_subcommand-1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "853ec1f59f9c9cb3602aa5bdd31bf154",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 6433,
"upload_time": "2023-05-31T09:30:34",
"upload_time_iso_8601": "2023-05-31T09:30:34.062312Z",
"url": "https://files.pythonhosted.org/packages/01/7d/b44842448ea31f2547c515d7b6b9d838092fbaa9c28a3f13af561abb4327/argparse_subcommand-1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0572e932c93f6d6c40d4affe3867d25de35fb69f34cc6f177ebac502f4fe8080",
"md5": "97cb26a6f6f1c0c49121b9a518049f2c",
"sha256": "b932dad40a35626f53af153fb07129b7f2f2735e511d84f59b340ae00cdde96b"
},
"downloads": -1,
"filename": "argparse_subcommand-1.0.tar.gz",
"has_sig": false,
"md5_digest": "97cb26a6f6f1c0c49121b9a518049f2c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 3639,
"upload_time": "2023-05-31T09:30:36",
"upload_time_iso_8601": "2023-05-31T09:30:36.096221Z",
"url": "https://files.pythonhosted.org/packages/05/72/e932c93f6d6c40d4affe3867d25de35fb69f34cc6f177ebac502f4fe8080/argparse_subcommand-1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-31 09:30:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "prechelt",
"github_project": "argparse_subcommand",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "argparse-subcommand"
}