Skip to main content

Create argparser automatically from function signatures

Project description

Overview

A lightweight wrapper around argparse to help build CLIs from functions.

PyPi Version Python Version PyPI License

Setup

:gear: Installation

Install it directly into an activated virtual environment:

$ pip install arger

:books: 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.
  • No external dependency other than stdlib.

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

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

arger-2.2.1.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arger-2.2.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file arger-2.2.1.tar.gz.

File metadata

  • Download URL: arger-2.2.1.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for arger-2.2.1.tar.gz
Algorithm Hash digest
SHA256 885b40b44c07c54dde108c8616f2994597bf306d5c0423f2167acae207f6e88f
MD5 6f8e7e57880668694514463725a5d77f
BLAKE2b-256 b5f76d90393357e055cafb2951c26f72161671da1555fbc71c8475a15903f04c

See more details on using hashes here.

Provenance

The following attestation bundles were made for arger-2.2.1.tar.gz:

Publisher: publish.yml on jnoortheen/arger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file arger-2.2.1-py3-none-any.whl.

File metadata

  • Download URL: arger-2.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for arger-2.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3494a4226c645dbc4272e8dc7a46c4d9251d7d16cb183600a640fe280bf8613b
MD5 dccbe230b354d98162b853a2582cffeb
BLAKE2b-256 eb461634af9dc929bf625b9495ea8d7a430ba0893efea2aaf371dca4a31ff5ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for arger-2.2.1-py3-none-any.whl:

Publisher: publish.yml on jnoortheen/arger

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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