Arger from function
In a file named 3-arger-as-function.py:
from arger import Arger
from tests.utils import _reprint
ctx = {}
def main(verbose=False, log=False, log_file: str | None = None):
"""App Description goes here.
:param verbose: Verbose output
:param log_file: Name of the log file to write output to
"""
ctx.update(**locals())
arger = Arger(func=main, prog="pytest")
container = []
@arger.add_cmd
def create(name: str):
"""Create a new test.
:param name: Name of the test
"""
container.append(container)
_reprint(**locals(), **ctx)
@arger.add_cmd
def remove(name: str):
"""Remove a test.
:param name: Name of the test
"""
if remove in container:
container.remove(remove)
_reprint(**locals(), **ctx)
@arger.add_cmd
def list(filter: str | None):
"""List all tests."""
_reprint(**locals(), container=container, **ctx)
if __name__ == "__main__":
arger.run()
The functions are converted to sub-commands, while the function arger serves as the entry point.
It will get executed before other sub-commands.
Usage
-
Show help message with global options and sub-commands
$ python docs/examples/3-arger-as-function.py -h usage: pytest [-h] [-v] [-l] [-o LOG_FILE] {create,remove,list} ... App Description goes here. options: -h, --help show this help message and exit -v, --verbose Verbose output (default: False) -l, --log -o, --log-file LOG_FILE Name of the log file to write output to (default: None) commands: {create,remove,list} create Create a new test. remove Remove a test. list List all tests. -
Show help message for the 'create' sub-command
$ python docs/examples/3-arger-as-function.py create -h usage: pytest create [-h] name Create a new test. positional arguments: name Name of the test options: -h, --help show this help message and exit -
Show help message for the 'remove' sub-command
$ python docs/examples/3-arger-as-function.py remove -h usage: pytest remove [-h] name Remove a test. positional arguments: name Name of the test options: -h, --help show this help message and exit -
Show help message for the 'list' sub-command
$ python docs/examples/3-arger-as-function.py list -h usage: pytest list [-h] [filter] List all tests. positional arguments: filter options: -h, --help show this help message and exit -
Run 'create' sub-command with required positional argument
$ python docs/examples/3-arger-as-function.py create folder1 log (<class 'bool'>): False log_file (<class 'NoneType'>): None name (<class 'str'>): folder1 verbose (<class 'bool'>): False -
Run 'remove' sub-command
$ python docs/examples/3-arger-as-function.py remove folder1 log (<class 'bool'>): False log_file (<class 'NoneType'>): None name (<class 'str'>): folder1 verbose (<class 'bool'>): False -
Run 'list' sub-command
$ python docs/examples/3-arger-as-function.py list container (<class 'list'>): [] filter (<class 'NoneType'>): None log (<class 'bool'>): False log_file (<class 'NoneType'>): None verbose (<class 'bool'>): False -
Run 'list' sub-command with global log-file option
$ python docs/examples/3-arger-as-function.py --log-file file.log list container (<class 'list'>): [] filter (<class 'NoneType'>): None log (<class 'bool'>): False log_file (<class 'str'>): file.log verbose (<class 'bool'>): False -
Run 'list' sub-command with global option and positional filter
$ python docs/examples/3-arger-as-function.py --log-file file.log list filterme container (<class 'list'>): [] filter (<class 'str'>): filterme log (<class 'bool'>): False log_file (<class 'str'>): file.log verbose (<class 'bool'>): False