Giter Club home page Giter Club logo

arger's Introduction

Overview

A wrapper around argparser to help build CLIs from functions. Uses type-hints extensively ๐Ÿ.

PyPi Version Python Version PyPI License

Setup

โš™๏ธ Installation

Install it directly into an activated virtual environment:

$ pip install arger

๐Ÿ“š Usage

  • create a python file called test.py
from arger import Arger


def main(param1: int, param2: str, kw1=None, kw2=False):
    """Example function with types documented in the docstring.
    
    Args:
        param1: The first parameter.
        param2: The second parameter.
        kw1: this is optional parameter.
        kw2: this is boolean. setting flag sets True.
    """
    print(locals())


arger = Arger(
    main,
    prog="pytest",  # for testing purpose. otherwise not required
)

if __name__ == "__main__":
    arger.run()
  • Here Arger is just a subclass of ArgumentParser. It will not conceal you from using other argparse libraries.

  • run this normally with

$ python test.py -h
usage: pytest [-h] [-k KW1] [-w] param1 param2

Example function with types documented in the docstring.

positional arguments:
  param1             The first parameter.
  param2             The second parameter.

optional arguments:
  -h, --help         show this help message and exit
  -k KW1, --kw1 KW1  this is optional parameter. (default: None)
  -w, --kw2          this is boolean. setting flag sets True. (default: False)
$ python test.py 100 param2
{'param1': 100, 'param2': 'param2', 'kw1': None, 'kw2': False}
  • Checkout examples folder and documentation to see more of arger in action. It supports any level of sub-commands.

Features

  • Uses docstring to parse help comment for arguments. Supports

  • Flags will be generated from parameter-name.

    1. e.g. def main(param: ...) -> -p, --param
    2. If needed you could declare it inside docstring like :param arg1: -a --arg this is the document.
  • one can use Argument class to pass any values to the parser.add_argument function

  • The decorated functions can be composed to form nested sub-commands of any level.

  • Most of the Standard types supported. Please see examples for more supported types with examples.

NOTE

  • *args supported but no **kwargs support yet.
  • all optional arguments that start with underscore is not passed to Parser. They are considered private to the function implementation. Some parameter names with special meaning
    • _namespace_ -> to get the output from the ArgumentParser.parse_args()
    • _arger_ -> to get the parser instance

Argparser enhancements

arger's People

Contributors

dependabot[bot] avatar jnoortheen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

arger's Issues

Handling docstrings when using Argument class

Say, I want to create a countable argument like this

Verbosity = Argument(type=int, action='count', default=0)

@Arger.init(prog='parse')
def arger(verbosity: int = Verbosity, config_path: Path = DEFAULT_CONFIG_PATH):
    """My Parser.

    Args:
        verbosity: Counted argument, use -v for info and -vv for debug.
        config_path: Path to config file.
    """

The help string for verboisty shows this

  -v VERBOSITY, --verbosity VERBOSITY
                        Counted argument, use -v for info and -vv for debug. (default: <Argument: (), {'default': 0, 'type': <class 'int'>, 'action':
                        'count'}>)

Can we modify the default value part of the help string to show as 0 instead of
(default: <Argument: (), {'default': 0, 'type': <class 'int'>, 'action': 'count'}>)

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.