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.NestedArgumentParser
can be substituted in directly to where the constructor forargparse.ArgumentParser
is being used. All subsequent method calls and subparser API calls should work without any additional code change! - Customizeability: There are additional
kwargs
exposed 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.
Quick Example
Given the following parser:
Root Parser with prog='root'
├─ 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 dest
s, but NestedArgumentParser
will produce the following result when run through parse_args
:
Namespace(
root=Namespace(
subcommand='sub1',
positional_1='Alice',
optional_1='Bob',
sub1=Namespace(
optional_1='Carol',
optional2AltName='David'
)
)
)
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file nested_argparse-0.1.1.tar.gz
.
File metadata
- Download URL: nested_argparse-0.1.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7bd99e66101c52348c41de40cd18b7da85e302805652ce03727e46d61a7fa03 |
|
MD5 | 31408ef55de8f21f94f4c3452a0371d2 |
|
BLAKE2b-256 | 94afdff131cd5a8c4c1ab12b27417e5a76acbcad14124e6f2cb616839f9349c6 |
File details
Details for the file nested_argparse-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: nested_argparse-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 526aabdf5bda64ceaa604cfee1f6f12d3fb610abb3dac1d5a2426376b79a07b4 |
|
MD5 | 18250a9dfc5ccee38a8cb6867823fc25 |
|
BLAKE2b-256 | c80ede2a11f0e305d7c081ee527905f9041855ec7dc1f040250f4e7ba3e6ac4b |