nestargs


Namenestargs JSON
Version 0.5.0 PyPI version JSON
download
home_pagehttps://github.com/speg03/nestargs
SummaryNested arguments parser
upload_time2021-05-11 04:21:13
maintainer
docs_urlNone
authorTakahiro Yano
requires_python>=3.5,<4.0
licenseMIT
keywords argparse arguments parser cli command line interface
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # nestargs

nestargs is a Python library that defines nested program arguments. It is based on argparse.

[![PyPI](https://img.shields.io/pypi/v/nestargs.svg)](https://pypi.org/project/nestargs/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nestargs.svg)](https://pypi.org/project/nestargs/)
[![Python Tests](https://github.com/speg03/nestargs/workflows/Python%20Tests/badge.svg)](https://github.com/speg03/nestargs/actions?query=workflow%3A%22Python+Tests%22)
[![codecov](https://codecov.io/gh/speg03/nestargs/branch/master/graph/badge.svg)](https://codecov.io/gh/speg03/nestargs)

Read this in Japanese: [日本語](https://github.com/speg03/nestargs/blob/master/README.ja.md)

## Installation

```
pip install nestargs
```

## Basic usage

Define program arguments in the same way as argparse. A nested structure can be represented by putting a dot in the program argument name.

```python
import nestargs

parser = nestargs.NestedArgumentParser()

parser.add_argument("--apple.n", type=int)
parser.add_argument("--apple.price", type=float)

parser.add_argument("--banana.n", type=int)
parser.add_argument("--banana.price", type=float)

args = parser.parse_args(
    ["--apple.n=2", "--apple.price=1.5", "--banana.n=3", "--banana.price=3.5"]
)
# => NestedNamespace(apple=NestedNamespace(n=2, price=1.5), banana=NestedNamespace(n=3, price=3.5))
```

Let's take out only the program argument apple.

```python
args.apple
# => NestedNamespace(n=2, price=1.5)
```

You can also get each value.

```python
args.apple.price
# => 1.5
```

If you want a dictionary format, you can get it this way.

```python
vars(args.apple)
# => {'n': 2, 'price': 1.5}
```

## Define program arguments from functions

The function `register_arguments` can be used to define program arguments from the parameters any function.

In the following example, program arguments with multiple prefixes are defined as the `n` and `price` parameters of the function `total_price`. At this time, the behavior of the program argument is automatically determined according to the default value of the parameter.

```python
import nestargs


def total_price(n=1, price=1.0):
    return n * price


parser = nestargs.NestedArgumentParser()
parser.register_arguments(total_price, prefix="apple")
parser.register_arguments(total_price, prefix="banana")

args = parser.parse_args(
    ["--apple.n=2", "--apple.price=1.5", "--banana.n=3", "--banana.price=3.5"]
)
# => NestedNamespace(apple=NestedNamespace(n=2, price=1.5), banana=NestedNamespace(n=3, price=3.5))
```

You can call the function with the values obtained from the program arguments as follows:

```python
apple = total_price(**vars(args.apple))
banana = total_price(**vars(args.banana))

print(apple + banana)
# => 13.5
```

### Option decorator

Program argument settings can be added by attaching an `option` decorator to the target function. The settings that can be added are based on `ArgumentParser.add_argument` of `argparse`.

```python
@nestargs.option("n", help="number of ingredients")
@nestargs.option("price", help="unit price of ingredients")
def total_price(n=1, price=1.0):
    return n * price


parser = nestargs.NestedArgumentParser()
parser.register_arguments(total_price, prefix="apple")
```

This code is equivalent to the following code:

```python
def total_price(n=1, price=1.0):
    return n * price


parser = nestargs.NestedArgumentParser()
parser.add_argument("--apple.n", type=int, default=1, help="number of ingredients")
parser.add_argument(
    "--apple.price", type=float, default=1.0, help="unit price of ingredients"
)
```

### Ignores decorator

By attaching an `ignores` decorator to the target function, you can specify parameters that do not register in the program arguments.

```python
@nestargs.ignores("tax", "shipping")
def total_price(n=1, price=1.0, tax=1.0, shipping=0.0):
    return n * price * tax + shipping


parser = nestargs.NestedArgumentParser()
parser.register_arguments(total_price, prefix="apple")

args = parser.parse_args(["--apple.n=2", "--apple.price=1.5"])
# => NestedNamespace(apple=NestedNamespace(n=2, price=1.5))
# Not included tax and shipping parameters

apple = total_price(**vars(args.apple))
# => 3.0
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/speg03/nestargs",
    "name": "nestargs",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.5,<4.0",
    "maintainer_email": "",
    "keywords": "argparse,arguments,parser,cli,command,line,interface",
    "author": "Takahiro Yano",
    "author_email": "speg03@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/cc/00/da6c07f39f5c8ecd547f852260c3d8f5b52808d7c373ad56d54f9ac6fa4e/nestargs-0.5.0.tar.gz",
    "platform": "",
    "description": "# nestargs\n\nnestargs is a Python library that defines nested program arguments. It is based on argparse.\n\n[![PyPI](https://img.shields.io/pypi/v/nestargs.svg)](https://pypi.org/project/nestargs/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/nestargs.svg)](https://pypi.org/project/nestargs/)\n[![Python Tests](https://github.com/speg03/nestargs/workflows/Python%20Tests/badge.svg)](https://github.com/speg03/nestargs/actions?query=workflow%3A%22Python+Tests%22)\n[![codecov](https://codecov.io/gh/speg03/nestargs/branch/master/graph/badge.svg)](https://codecov.io/gh/speg03/nestargs)\n\nRead this in Japanese: [\u65e5\u672c\u8a9e](https://github.com/speg03/nestargs/blob/master/README.ja.md)\n\n## Installation\n\n```\npip install nestargs\n```\n\n## Basic usage\n\nDefine program arguments in the same way as argparse. A nested structure can be represented by putting a dot in the program argument name.\n\n```python\nimport nestargs\n\nparser = nestargs.NestedArgumentParser()\n\nparser.add_argument(\"--apple.n\", type=int)\nparser.add_argument(\"--apple.price\", type=float)\n\nparser.add_argument(\"--banana.n\", type=int)\nparser.add_argument(\"--banana.price\", type=float)\n\nargs = parser.parse_args(\n    [\"--apple.n=2\", \"--apple.price=1.5\", \"--banana.n=3\", \"--banana.price=3.5\"]\n)\n# => NestedNamespace(apple=NestedNamespace(n=2, price=1.5), banana=NestedNamespace(n=3, price=3.5))\n```\n\nLet's take out only the program argument apple.\n\n```python\nargs.apple\n# => NestedNamespace(n=2, price=1.5)\n```\n\nYou can also get each value.\n\n```python\nargs.apple.price\n# => 1.5\n```\n\nIf you want a dictionary format, you can get it this way.\n\n```python\nvars(args.apple)\n# => {'n': 2, 'price': 1.5}\n```\n\n## Define program arguments from functions\n\nThe function `register_arguments` can be used to define program arguments from the parameters any function.\n\nIn the following example, program arguments with multiple prefixes are defined as the `n` and `price` parameters of the function `total_price`. At this time, the behavior of the program argument is automatically determined according to the default value of the parameter.\n\n```python\nimport nestargs\n\n\ndef total_price(n=1, price=1.0):\n    return n * price\n\n\nparser = nestargs.NestedArgumentParser()\nparser.register_arguments(total_price, prefix=\"apple\")\nparser.register_arguments(total_price, prefix=\"banana\")\n\nargs = parser.parse_args(\n    [\"--apple.n=2\", \"--apple.price=1.5\", \"--banana.n=3\", \"--banana.price=3.5\"]\n)\n# => NestedNamespace(apple=NestedNamespace(n=2, price=1.5), banana=NestedNamespace(n=3, price=3.5))\n```\n\nYou can call the function with the values obtained from the program arguments as follows:\n\n```python\napple = total_price(**vars(args.apple))\nbanana = total_price(**vars(args.banana))\n\nprint(apple + banana)\n# => 13.5\n```\n\n### Option decorator\n\nProgram argument settings can be added by attaching an `option` decorator to the target function. The settings that can be added are based on `ArgumentParser.add_argument` of `argparse`.\n\n```python\n@nestargs.option(\"n\", help=\"number of ingredients\")\n@nestargs.option(\"price\", help=\"unit price of ingredients\")\ndef total_price(n=1, price=1.0):\n    return n * price\n\n\nparser = nestargs.NestedArgumentParser()\nparser.register_arguments(total_price, prefix=\"apple\")\n```\n\nThis code is equivalent to the following code:\n\n```python\ndef total_price(n=1, price=1.0):\n    return n * price\n\n\nparser = nestargs.NestedArgumentParser()\nparser.add_argument(\"--apple.n\", type=int, default=1, help=\"number of ingredients\")\nparser.add_argument(\n    \"--apple.price\", type=float, default=1.0, help=\"unit price of ingredients\"\n)\n```\n\n### Ignores decorator\n\nBy attaching an `ignores` decorator to the target function, you can specify parameters that do not register in the program arguments.\n\n```python\n@nestargs.ignores(\"tax\", \"shipping\")\ndef total_price(n=1, price=1.0, tax=1.0, shipping=0.0):\n    return n * price * tax + shipping\n\n\nparser = nestargs.NestedArgumentParser()\nparser.register_arguments(total_price, prefix=\"apple\")\n\nargs = parser.parse_args([\"--apple.n=2\", \"--apple.price=1.5\"])\n# => NestedNamespace(apple=NestedNamespace(n=2, price=1.5))\n# Not included tax and shipping parameters\n\napple = total_price(**vars(args.apple))\n# => 3.0\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Nested arguments parser",
    "version": "0.5.0",
    "split_keywords": [
        "argparse",
        "arguments",
        "parser",
        "cli",
        "command",
        "line",
        "interface"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "953f8013f6f7e1ee39276fa4086ef3ea",
                "sha256": "14f5c1247641cb4f16ecb6d63c1a1777a9f5f880f3cb07b65304b8b99abe84df"
            },
            "downloads": -1,
            "filename": "nestargs-0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "953f8013f6f7e1ee39276fa4086ef3ea",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5,<4.0",
            "size": 5366,
            "upload_time": "2021-05-11T04:21:12",
            "upload_time_iso_8601": "2021-05-11T04:21:12.376370Z",
            "url": "https://files.pythonhosted.org/packages/09/33/0fdd6473d61058db40b390da343fc23bc415a38ca9a2aff8d68c5befc198/nestargs-0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "72888514fcde625455de83bf5e4ce371",
                "sha256": "1891510baf72832427137b2dc42f207b40e364c20f9e6088c17cfa961703ecef"
            },
            "downloads": -1,
            "filename": "nestargs-0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "72888514fcde625455de83bf5e4ce371",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5,<4.0",
            "size": 5030,
            "upload_time": "2021-05-11T04:21:13",
            "upload_time_iso_8601": "2021-05-11T04:21:13.670316Z",
            "url": "https://files.pythonhosted.org/packages/cc/00/da6c07f39f5c8ecd547f852260c3d8f5b52808d7c373ad56d54f9ac6fa4e/nestargs-0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-05-11 04:21:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": null,
    "github_project": "speg03",
    "error": "Could not fetch GitHub repository",
    "lcname": "nestargs"
}
        
Elapsed time: 0.24766s