Single function
In a file named 1-single-function.py:
from argparse import Namespace
from arger import Arger
from tests.utils import _reprint
def main(param1: int, param2: str, _namespace_: Namespace, kw1=None, kw2=False):
"""Example function with types documented in the docstring.
:param param1: The first parameter.
:param param2: The second parameter.
:param kw1: this is optional parameter.
:param kw2: this is boolean. setting flag sets True.
"""
_reprint(**locals())
arger = Arger(
main,
prog="pytest", # for testing purpose. otherwise not required
)
if __name__ == "__main__":
arger.run()
Usage
-
Show help message
$ python docs/examples/1-single-function.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. options: -h, --help show this help message and exit -k, --kw1 KW1 this is optional parameter. (default: None) -w, --kw2 this is boolean. setting flag sets True. (default: False) -
Run with required positional arguments
$ python docs/examples/1-single-function.py 10 p2 _namespace_ (<class 'argparse.Namespace'>): [('__func_0', '_dispatch'), ('__level__', 0), ('kw1', None), ('kw2', False), ('param1', 10), ('param2', 'p2')] kw1 (<class 'NoneType'>): None kw2 (<class 'bool'>): False param1 (<class 'int'>): 10 param2 (<class 'str'>): p2 -
Running without required arguments raises an error
$ python docs/examples/1-single-function.py usage: pytest [-h] [-k KW1] [-w] param1 param2 pytest: error: the following arguments are required: param1, param2 -
Running with an invalid flag raises an error
$ python docs/examples/1-single-function.py --invalid usage: pytest [-h] [-k KW1] [-w] param1 param2 pytest: error: the following arguments are required: param1, param2 -
Running with an incorrect type for a positional argument raises a type error
$ python docs/examples/1-single-function.py p1 p2 usage: pytest [-h] [-k KW1] [-w] param1 param2 pytest: error: argument param1: invalid int value: 'p1'