A python module that extends argparser to create nested namespace trees for subparsers.
Project description
nested-argparse 💬 → 🅰.🅱.🆒
nested-argparse is a python module that non-invasively builds on top of the built-in argparse library to allow subparsers to parse into their own nested namespaces.
The library exposes a class NestedArgumentParser which allows arbitrary nesting without worry of namespace conflicts. This is achieved with the following principles of this library:
- Inversion of Control: A parser, when adding a subparser, is in control of what name to use for the sub-namespace which the subparser sends its parsed args to.
- Drop-In Replacement: The constructor for
nested_argparse.NestedArgumentParsercan be substituted in directly to where the constructor forargparse.ArgumentParseris being used. All subsequent method calls and subparser API calls should work without any additional code change! - Customizeability: There are additional
kwargsexposed to further customize the nesting options to your liking, if the defaults do not suit your scenario.
The main difference between this library and its built-in counterpart is the return value of the parse_args method. Instead of a flat namespace containing all parsed arguments across all subparsers, NestedArgumentParser will produce a namespace tree.
Simple Conceptual Example
Given the following parser:
Root Parser
├─ positional_1
├─ --optional_1
├─ --optional_2
└─ sub parsers with dest='subcommand'
├─ Sub Parser 1 with name='sub1'
│ ├─ --optional_1
│ └─ --optional_2 with dest='optional2AltName'
└─ Sub Parser 2 with name='sub2'
├─ --optional_1
└─ --optional_2
And the following args to parse:
Alice --optional_1=Bob sub1 --optional_1=Carol --optional_2=David
The built-in ArgumentParser would not be able to handle the duplication in dests, but NestedArgumentParser will produce the following result when run through parse_args:
Namespace(
subcommand='sub1',
positional_1='Alice',
optional_1='Bob',
sub1=Namespace(
optional_1='Carol',
optional2AltName='David'
)
)
API Documentation
The library exposes the following modules.
Module nested_argparse
The module exports the following classes.
Class NestedArgumentParser
- extends
argparser.ArgumentParser - for documentation for the superclass, see the official Python API reference docs for
argparse.
Constructor
In addition to the parameters available to ArgumentParser constructor, the following parameters are also accepted:
-
Param
nest_dir, optional, type:Optional[str]- When a string is passed in, it is used as the attribute name in the parent namespace to which the nested namespace, where the parsed values will be stored, is assigned to.
- When
Noneis passed in, no nested namespace is created, and parsed values are directly assigned to the parent namespace. This is the behavior of the baseArgumentParser. - Default value:
None.
-
Param
nest_separator, optional, type:str- It is used as the separator to delimit components in the nest path when representing the path as a string (for example, this is used to generate
dests) - Default value:
'__'.
- It is used as the separator to delimit components in the nest path when representing the path as a string (for example, this is used to generate
-
Param
nest_path, optional, type:Optional[List[str]]- When a list of strings is passed in, it is used as a sequence of nested attribute names from the parent namespace which locates the nested namespace where the parsed values will be stored.
- When
Noneis passed in, no nested namespace is created, and parsed values are directly assigned to the parent namespace. This is the behavior of the baseArgumentParser.
Override NestedArgumentParser.add_argument
Instead of adding an argument definition which stores the parsed value to dest in the flat top-level namespace, the parsed value will be stored at attribute with the name given by dest in the namesapce at the nesting path associated with this parser.
Override NestedArgumentParser.add_subparsers
The return value of this method is an instance of internal subparser handler _NestedSubParsersAction, which exposes extra options for adding subparsers.
Override NestedArgumentParser.parse_args
The return value of this method is a namespace tree rather than a flat namespace. The tree is built according to the nesting paths associated with each of the parsed values.
Override NestedArgumentParser.parse_known_args
The return value of this method is a namespace tree rather than a flat namespace. The tree is built according to the nesting paths associated with each of the parsed values.
Class _NestedSubParsersAction
Override _NestedSubParsersAction.add_parser
- Param
nest_dir, optional, type:Optional[str]- When a string is passed in, it is used as the attribute name in the parent namespace to which the subparser will store its parsed values to.
- When
Noneis passed in, thedestfield is used as the nesting directory instead. - Default value:
None.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nested_argparse-0.2.0.tar.gz.
File metadata
- Download URL: nested_argparse-0.2.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5052e25a6423c41f01e8f1166dbb57f33df161200276e6d73ec5e2f04cd9922
|
|
| MD5 |
effe08b9c2bf871993b735159ca1f3f5
|
|
| BLAKE2b-256 |
9b3630e3d0522197db36d15dd7ae5cefd303a9f92985e98bd4cdc23a94204577
|
Provenance
The following attestation bundles were made for nested_argparse-0.2.0.tar.gz:
Publisher:
publish.yml on stephen-zhao/nested_argparse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nested_argparse-0.2.0.tar.gz -
Subject digest:
b5052e25a6423c41f01e8f1166dbb57f33df161200276e6d73ec5e2f04cd9922 - Sigstore transparency entry: 1554407758
- Sigstore integration time:
-
Permalink:
stephen-zhao/nested_argparse@9fe18488d878944a8ab7fc7dc5b17ced8cb6904d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/stephen-zhao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9fe18488d878944a8ab7fc7dc5b17ced8cb6904d -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file nested_argparse-0.2.0-py3-none-any.whl.
File metadata
- Download URL: nested_argparse-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9490e65ce6cd88c8c7a609c22941cc7ca6902abae617482d2b450069561d5890
|
|
| MD5 |
b3e34ef2c92e5519210748d35b769988
|
|
| BLAKE2b-256 |
660a8685ba3d55def7611512ecd7d88eb6ed249e53d9238814f37dc6b6c24813
|
Provenance
The following attestation bundles were made for nested_argparse-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on stephen-zhao/nested_argparse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nested_argparse-0.2.0-py3-none-any.whl -
Subject digest:
9490e65ce6cd88c8c7a609c22941cc7ca6902abae617482d2b450069561d5890 - Sigstore transparency entry: 1554407770
- Sigstore integration time:
-
Permalink:
stephen-zhao/nested_argparse@9fe18488d878944a8ab7fc7dc5b17ced8cb6904d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/stephen-zhao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9fe18488d878944a8ab7fc7dc5b17ced8cb6904d -
Trigger Event:
workflow_run
-
Statement type: