Skip to main content

Simple argument parser for documented functions

Project description

argParseFromDoc

A simple python package for creating/updating argparse ArgumentParser(s) given a documented function.

Content

Installation

  • Option 1. Cloning this repository:
git clone https://github.com/rsanchezgarc/argParseFromDoc.git
cd argParseFromDoc
pip install .
  • Option 2. Installing with pip
pip install git+https://github.com/rsanchezgarc/argParseFromDoc

Supported features

  • Argument types:
    • int, str, float and bool
    • (Homogeneous) Lists of any of the previous types (defined astyping.List[primitive_type])
    • Files (defined astyping.TextIO and typing.BinaryIO)
  • Ignoring/selecting a subset of the arguments of the function
  • Creating a new parser or adding new arguments to it
  • Several docsctring formats (see docstring_parser )
  • Support for methods assuming first argument in definition is self

Assumptions

  • Positional arguments. Functions can have positional arguments, but the parser will treat them as if they were keyword/optional (always --argname VALUE)
  • If no default value is provided for an argument in the typing hint, argument will be considered as required (parser.add_argument(..., required=True)). The same applies to default=None except if the name of the argument is included in args_optional. E.g get_parser_from_function(..., args_optional=[name1, name2...])
  • Boolean arguments:
    • Boolean arguments must be provided with default value.
    • If a boolean argument defaults to False (name:bool=False), the parser sets the argument name=True if --name flag provided.
    • If a boolean argument defaults to True (name:bool=True), the parser sets the argument name=False if --NOT_name flag provided. Please notice that the name of the argument in the argument parser has been changed from name to --NOT_name to reflect that but the argument is stored using the original name, so no further changes are required
  • Multiple arguments can be provided if using typing.List. For example: def fun(several_strings: List[str]):
  • Setting deafult values for typing.TextIO and typing.BinaryIO is not advisable, as they should be opened files. If you are only employing the function for the argument parser, you could default it to string values pointing files, but again, this is not encouraged. Instead, if you want to set a default filename, use type str. The main purpose of typing.TextIO and typing.BinaryIO in the parser is to allow pipes. For example:
    #These two commands are equivalent
    python count_lines --inputFile /etc/passwd 
    cat /etc/passwd | python count_lines --inputFile -
    
    
  • Methods, including __init__, are supported providing self is always using in as the first argument in the definition
  • When defining functions, *arg and **kwargs are ignored for the parser. No other * or ** argument is supported.

Usage

You only need to document the type and possible default values for the arguments of your functions with typing and the description of each within the docstring. Examples of documented functions are:

def add(a: int, b: int):
    '''
    @param a: first number
    @param b: second number
    '''
    return a + b
    
def printYourAge(age: int, name: str = "Unknown"):
    '''
    @param age: your age
    @param name: your name
    '''
    return a + b
    
def addList(several_nums: List[int], b: int=1):
    '''
    @param several_nums: first number
    @param b: second number
    '''
    return [a + b for a in several_nums]

Then, obtaining an ArgumentParser for any of these functions (say add) is as easy as:

if __name__ == "__main__":
    from argParseFromDoc import get_parser_from_function
    parser = get_parser_from_function(add)
    args = parser.parse_args()
    print(add(**vars(args)))

If you want to add to a previously instantiated parser the arguements of the function, you just need to provide the original parser (or group) to the get_parser_from_function function.

if __name__ == "__main__":
    from argParseFromDoc import get_parser_from_function
    #standard ArgumentParser
    from argparse import ArgumentParser
    parser = ArgumentParser(prog="Add_example")
    parser.add_argument("--other_type_of_argument", type=str, default="Not provided")
    #####################################################
    # ### If you prefer a group instead of a whole parser
    # group = parser.add_argument_group()
    # get_parser_from_function(add, parser=group)
    #####################################################
    #provide the original parser to get_parser_from_function that will add the new options to the parser
    get_parser_from_function(add, parser=parser)
    args = parser.parse_args()
    print(add(**vars(args)))

Finally, if your function has some arguments that you do not want to include to the parser, you can use the args_to_ignore or args_to_include options.

def manyArgsFun(a: int, b: int, c: int = 1, d: int = 2, e: str = "oneStr"):
    '''

    :param a: a
    :param b: b
    :param c: c
    :param d: d
    :param e: e
    :return:
    '''
    print(e)
    return sum([a, b, c, d])


if __name__ == "__main__":
    from argParseFromDoc import get_parser_from_function
    # parser = get_parser_from_function(manyArgsFun, args_to_ignore=["c", "d", "e"])
    parser = get_parser_from_function(manyArgsFun, args_to_include=["a", "b"])
    args = parser.parse_args()
    print(manyArgsFun(**vars(args)))

Some additional examples can be found in examples folder

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

argParseFromDoc-0.0.1.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

argParseFromDoc-0.0.1-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file argParseFromDoc-0.0.1.tar.gz.

File metadata

  • Download URL: argParseFromDoc-0.0.1.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for argParseFromDoc-0.0.1.tar.gz
Algorithm Hash digest
SHA256 42cd440f120a67e8c9ecbacc41ac7a3a243f4f3b32c6ef54a573ff8a1b2e23c6
MD5 71e0e791b02d706dc6221361db7ae473
BLAKE2b-256 81016bf4bd68350d2dc9e2365f1c535019c074f0bbf808da0a337d2cdedef8af

See more details on using hashes here.

File details

Details for the file argParseFromDoc-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: argParseFromDoc-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for argParseFromDoc-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 39a40a604139a230799e29dd011d12b0b2b0d15484637f3a62753a0db5f6fbc0
MD5 0b9b9252fe5ee769c5a90957dae812a9
BLAKE2b-256 2244ea84e522a73affc6fcfd13e55dc93ad784ab67a2b508783c6b0d2a8ac394

See more details on using hashes here.

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