Create argparser automatically from functions
Project description
Overview
A wrapper around argparser to help build CLIs from functions. Uses type-hints extensively :snake:.
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 otherargparse
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.
- e.g.
def main(param: ...)
->-p, --param
- If needed you could declare it inside docstring like
:param arg1: -a --arg this is the document
.
- e.g.
-
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 theArgumentParser.parse_args()
_arger_
-> to get the parser instance
Argparser enhancements
- web-ui : https://github.com/nirizr/argparseweb
- extra actions : https://github.com/kadimisetty/action-hero
- automatic shell completions using argcomplete
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file arger-1.4.8.tar.gz
.
File metadata
- Download URL: arger-1.4.8.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c042bfdb71b355f7562bbfe2a831531b84b1632c1e3dfdfe010ab9878b17b132 |
|
MD5 | f7798ec911b2106357a09d97afcf0c48 |
|
BLAKE2b-256 | 14d60b014019defc6d297264cc07d8e17d57f161c93929a3e86d315f9735836a |
File details
Details for the file arger-1.4.8-py3-none-any.whl
.
File metadata
- Download URL: arger-1.4.8-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.15
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2019c6b48b4e045846171ec4347a03ca2a065a7b59f05e097186de2c9a56db34 |
|
MD5 | b256aeecfedad5e3ed733362af409052 |
|
BLAKE2b-256 | da65143290358f0b067eaa3d891c343633c142688d2f98dc1c5447b9d53cb0ba |