Utility usage#

Refer to argclz.core

parse_args#

Parse the provided list of command-line arguments and apply the parsed values to the given instance

refer to parse_args()

  • Example usage

from argclz import parse_args

class Opt(AbstractParser):
    name: str = argument('--name', required=True)
    count: int = argument('--count', type=int, default=1)


opt = Opt()
args = ['--name', 'Alice', '--count', '3']  # same as ['--name=Alice', '--count=3']
parse_args(opt, args)

print(opt.name)   # Alice
print(opt.count)  # 3

as_dict#

collect all argument attributes into a dictionary with attribute name to its value

refer to as_dict()

  • Example usage

from argclz import as_dict

class Opt(AbstractParser):
    name: str = argument('--name', default='guest')
    age: int = argument('--age')

opt = Opt()
opt.name = 'Alice'
opt.age = 20

print(as_dict(opt))
  • Output

{'name': 'Alice', 'age': 20}

with_options#

Option class can be composed by inherition. Child option class can also change the value from parent’s argument. As well as disable it (by replacing a value)

refer to argument.with_option

Use together with as_argument()

  • Example usage

class Parent(AbstractParser):
    mode: str = argument('--mode', choices=['train', 'test'], default='train')

class Child(Parent):
    # Override mode to change the default
    mode = as_argument(Parent.mode).with_options(default='test')

print_help(Child)
  • Output

usage: test.py [-h] [--mode {train,test}]

options:
  -h, --help           show this help message and exit
  --mode {train,test}

with_defaults#

Initialize argument attributes with a proper default.

refer to with_defaults()

Option class can be any class contains argument() or other argument kinds. The difference between a class inherit from AbstractParser() and a class does not is that the former will initialize its argument attributes when creating via with_defaults().

from argclz.core import with_defaults

class Option:
    a: bool = argument('-a') #1

print(Option().a) # [2] raise AttributeError
print(with_defaults(Option()).a) # False
  1. A normal class with a bool attribute a, which has a proper default value False in common sense.

  2. An AttributeError was raised, because attribute is not initialized yet.