Skip to content

Arger from function

in a file named src.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import Optional

from arger import Arger
from tests.utils import _reprint


ctx = {}


@Arger.init(prog="pytest")  # any argument to the parser
def arger(verbose=False, log=False, log_file: Optional[str] = None):
    """App Description goes here.

    :param verbose: verbose output
    :param log_file: name of the log file to write output
    """
    ctx.update(**locals())


container = []


@arger.add_cmd
def create(name: str):
    """Create 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: Optional[str]):
    """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 entrypoint. It will get executed before other sub-commands.