Code Monkey home page Code Monkey logo

nestargs's Introduction

nestargs

nestargs is a Python library that treats command line arguments as a hierarchical structure. The functionality for interpreting command line arguments is the same as argparse.

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

Installation

pip install nestargs

Basic usage

When defining command line arguments, use "." as the delimiter. to represent a variable hierarchy. The following code example defines an n and price variable in the apple hierarchy and another separate n and price variable in the banana hierarchy.

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))

Variables obtained by parsing command line arguments can be referenced by hierarchy.

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

Of course, you can also refer directly to variables lower down in the hierarchy.

args.apple.price
# => 1.5

When referring to each level of hierarchy, you can use vars to create a dictionary format.

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

Use a different delimiter for namespace

The default namespace delimiter is "." but can be any other character. In that case, specify the delimiter in the NestedArgumentParser constructor argument.

import nestargs

parser = nestargs.NestedArgumentParser(delimiter="/")
parser.add_argument("--apple/n", type=int)

args = parser.parse_args(["--apple/n=1"])
# => _NestedNamespace(apple=_NestedNamespace(n=1))

However, references to variables must be separated by "." delimiter when referring to variables.

args.apple.n
# => 1

nestargs's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar keisen avatar pre-commit-ci[bot] avatar speg03 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.