让 argparse 也可以通过修饰器形式设置命令行参数
資深大佬 : ungrown 10
https://github.com/mo-han/mo-han-toolbox/blob/8e2e92d0c1b771155bac463f836da34a94982b29/mylib/ez/argparse.py
单纯就是为了偷懒,形似 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
大佬有話說 (0)