nested-argparse


Namenested-argparse JSON
Version 0.1.2 PyPI version JSON
download
home_page
SummaryA python module that extends argparser to create nested namespace trees for subparsers.
upload_time2021-08-08 07:11:48
maintainer
docs_urlNone
authorStephen Zhao
requires_python>=3.6
licenseMIT License
keywords argparse nested namespace subparser conflict parser cli command subcommand
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # nested-argparse 💬 → 🅰.🅱.🆒

[![PyPI](https://img.shields.io/pypi/v/nested-argparse?color=brightgreen&label=pypi%20package)](https://pypi.org/project/nested-argparse/)
![PyPI - Status](https://img.shields.io/pypi/status/nested-argparse)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nested-argparse)
[![PyPI - License](https://img.shields.io/pypi/l/nested-argparse)](https://github.com/stephen-zhao/nested_argparse/blob/main/LICENSE)

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 for `argparse.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.

## 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:

```sh
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`:

```py
Namespace(
  subcommand='sub1',
  positional_1='Alice',
  optional_1='Bob',
  sub1=Namespace(
    optional_1='Carol',
    optional2AltName='David'
  )
)
```


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "nested-argparse",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "argparse,nested,namespace,subparser,conflict,parser,cli,command,subcommand",
    "author": "Stephen Zhao",
    "author_email": "mail@zhaostephen.com",
    "download_url": "https://files.pythonhosted.org/packages/fe/1a/67decd61cb897688ee7d3598fdebd1ba20e6b6e23f40d9742f8f5b6fa699/nested_argparse-0.1.2.tar.gz",
    "platform": "",
    "description": "# nested-argparse \ud83d\udcac \u2192 \ud83c\udd70.\ud83c\udd71.\ud83c\udd92\n\n[![PyPI](https://img.shields.io/pypi/v/nested-argparse?color=brightgreen&label=pypi%20package)](https://pypi.org/project/nested-argparse/)\n![PyPI - Status](https://img.shields.io/pypi/status/nested-argparse)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nested-argparse)\n[![PyPI - License](https://img.shields.io/pypi/l/nested-argparse)](https://github.com/stephen-zhao/nested_argparse/blob/main/LICENSE)\n\nnested-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.\n\nThe library exposes a class `NestedArgumentParser` which allows arbitrary nesting without worry of namespace conflicts. This is achieved with the following principles of this library:\n\n- **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.\n- **Drop-In Replacement:** The constructor for `nested_argparse.NestedArgumentParser` can be substituted in directly to where the constructor for `argparse.ArgumentParser` is being used. All subsequent method calls and subparser API calls should work without any additional code change!\n- **Customizeability:** There are additional `kwargs` exposed to further customize the nesting options to your liking, if the defaults do not suit your scenario.\n\nThe 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.\n\n## Simple Conceptual Example\n\nGiven the following parser:\n\n```\nRoot Parser\n \u251c\u2500 positional_1\n \u251c\u2500 --optional_1\n \u251c\u2500 --optional_2\n \u2514\u2500 sub parsers with dest='subcommand'\n     \u251c\u2500 Sub Parser 1 with name='sub1'\n     \u2502   \u251c\u2500 --optional_1\n     \u2502   \u2514\u2500 --optional_2 with dest='optional2AltName'\n     \u2514\u2500 Sub Parser 2 with name='sub2'\n         \u251c\u2500 --optional_1\n         \u2514\u2500 --optional_2\n```\n\nAnd the following args to parse:\n\n```sh\nAlice --optional_1=Bob sub1 --optional_1=Carol --optional_2=David\n```\n\nThe 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`:\n\n```py\nNamespace(\n  subcommand='sub1',\n  positional_1='Alice',\n  optional_1='Bob',\n  sub1=Namespace(\n    optional_1='Carol',\n    optional2AltName='David'\n  )\n)\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "A python module that extends argparser to create nested namespace trees for subparsers.",
    "version": "0.1.2",
    "split_keywords": [
        "argparse",
        "nested",
        "namespace",
        "subparser",
        "conflict",
        "parser",
        "cli",
        "command",
        "subcommand"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "c1abd33c2f04834040c7c1f90c60a6d6",
                "sha256": "abffa93d59bbe1ab2c744b4d060954d66652a9fadeafa93c113d973a1f13402c"
            },
            "downloads": -1,
            "filename": "nested_argparse-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c1abd33c2f04834040c7c1f90c60a6d6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 6549,
            "upload_time": "2021-08-08T07:11:47",
            "upload_time_iso_8601": "2021-08-08T07:11:47.562145Z",
            "url": "https://files.pythonhosted.org/packages/77/10/5f9801f6e4439641a68e0d4be8b25069d9a2fcced1c79e5741f2c4271b45/nested_argparse-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "fa4757475e8591f96e821160ededdb96",
                "sha256": "581996b7423efe26150f8f2d0a558bee69102a6f943b4b1c8c7318b94af275db"
            },
            "downloads": -1,
            "filename": "nested_argparse-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "fa4757475e8591f96e821160ededdb96",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 6813,
            "upload_time": "2021-08-08T07:11:48",
            "upload_time_iso_8601": "2021-08-08T07:11:48.986536Z",
            "url": "https://files.pythonhosted.org/packages/fe/1a/67decd61cb897688ee7d3598fdebd1ba20e6b6e23f40d9742f8f5b6fa699/nested_argparse-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-08-08 07:11:48",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "nested-argparse"
}
        
Elapsed time: 0.27494s