单纯就是为了偷懒,形似 Click
#test.py
from mylib.ez.argparse import *
from mylib.ez import AttrName
pr = ParserRigger()
ro = RawObject
an = AttrName() # an.attribute == 'attribute'
@pr.super_command()
@pr.argument(an.x)
@pr.argument(an.y)
@pr.argument(an.z)
@pr.flag(an.l, help='print in multiple lines')
@pr.call_map(an.x, ro(1111), an.z, UnknownArguments(), multi_line=an.l)
def xyz(x, y, z, more, multi_line=False):
print(f'x={x}', f'y={y}', f'z={z}', 'and more', *more, sep='\n' if multi_line else ', ')
an.x = an.y = an.z = an.l = None
pr.parse_known_args()
pr.run()
>test
usage: test.py [-h] [-l] x y z
test.py: error: the following arguments are required: x, y, z
>test -h
usage: test.py [-h] [-l] x y z
positional arguments:
x
y
z
optional arguments:
-h, --help show this help message and exit
-l print in multiple lines
>test 0 0 0 a b c
x=0, y=1111, z=0, and more, a, b, c
>test 0 0 0 a b c -l
x=0
y=1111
z=0
and more
a
b
c