Skip to main content

Nested arguments parser

Project description

nestargs

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

PyPI PyPI - Python Version Python Tests codecov pre-commit.ci status

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.

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.

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

You can also get each value.

args.apple.price
# => 1.5

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

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

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

nestargs-1.0.0.tar.gz (5.6 kB view hashes)

Uploaded Source

Built Distribution

nestargs-1.0.0-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page