ArgTyper
– ArgumentParser on Typehints and Decorators
ArgTyper provides and overlay for argparse.ArgumentParser to create parsers based on function type hint information enhanced with information provided by function decorators.
Additionally argtyper supports handling of async coroutines as well as normal functions.
Quickstart
In it’s simplest form, usage of ArgTyper looks as follows:
import argtyper
async def hello(name: str, amount: int = 2):
print("\n".join([f"Hello {name.upper()}"] * amount))
at = argtyper.ArgTyper(hello)
at()
Assuming the Python code above is saved into a file called simple.py, it can
be run at the command line and provides useful help messages:
$ python simple.py -h
usage: simple.py [-h] [--amount AMOUNT] NAME
positional arguments:
NAME provide argument of type STR
optional arguments:
-h, --help show this help message and exit
--amount AMOUNT provide argument of type INT
When run with the appropriate arguments it looks like the following:
$ python simple.py Yoda
Hello YODA
Hello YODA
$ python simple.py Yoda --amount 4
Hello YODA
Hello YODA
Hello YODA
Hello YODA
If invalid arguments are passed in, it will issue an error:
$ python simple.py Yoda --amount z
usage: simple.py [-h] [--amount AMOUNT] NAME
Error: simple.py: argument --amount: invalid int value: 'z'
For a more in-depth look on what ArgTyper can do for you, take a look at the following chapters.
Introduction
Known Limitations
custom argparse.Actions do not support async mode. If you use custom actions while calling the parser in async mode, those can potentially block the event loop for other tasks. This shouldn’t be a problem if they are not performing any I/O bound operations, though.
Module Objects