创建clis的小库
main-dec的Python项目详细描述
main_dec
一个小库,用于在python中进行无痛命令行参数解析。
安装
> pip install main-dec
快速启动
# my_cli.pyfromtypingimportTuplefrommain_decimportmain@maindefrun(required_str:str,optional_int=1,optional_tuple:Tuple[int,...]=()):""" A small example cli :param required_str: a required str :param optional_int: an optional int :param optional_tuple: an optional tuple """print('required_str',required_str)print('optional_int',optional_int)print('optional_tuple',optional_tuple)
> python -m my_cli -h usage: my_cli [-h] [--optional-tuple OPTIONAL_TUPLE [OPTIONAL_TUPLE ...]] [--optional-int OPTIONAL_INT] required_strA small example clipositional arguments: required_str a required stroptional arguments: -h, --help show this help message and exit --optional-tuple OPTIONAL_TUPLE [OPTIONAL_TUPLE ...] an optional tuple --optional-int OPTIONAL_INT an optional int> python -m my_cli arg --optional-int 2 --optional-tuple 123required_str argoptional_int 2optional_tuple (1, 2, 3)
必需和可选参数
函数的位置参数将被解析为cli所需的参数。 函数的可选参数将被分析为可选参数。
# my_cli.pyfrommain_decimportmain@maindefrun(required_arg:str,optional_arg=''):pass
> python -m my_cli -h usage: my_cli [-h] [--optional-arg OPTIONAL_ARG] required_argpositional arguments: required_argoptional arguments: -h, --help show this help message and exit --optional-arg
标志
可选的bool
参数被解析为标志,以便传递它们
在命令线上“翻转”他们的
真实。
# my_cli.pyfrommain_decimportmain@maindefrun(positive_flag=False,negative_flag=True):print('positive_flag',positive_flag)print('negative_flag',negative_flag)
> python -m my_cli --postive-flag --negative-flag positive_flag Truenegative_flag False
类型转换
PEP484带注释的参数和具有默认值的参数将具有 在传递给函数之前转换的类型。
# my_cli.pyfromtypingimportTuplefrommain_decimportmain@maindefrun(required_float:float,optional_tuple:Tuple[float,...]=()):print('required_float',required_float)print('optional_tuple',optional_tuple)
> python -m my_cli 1 --optional-tuple 234required_float 1.0optional_tuple (2.0, 3.0, 4.0)
目前支持的类型有str
、bytes
、int
、float
、list
(包括typing.List
)、tuple
。
(包括typing.Tuple
)和Enum
。str
是默认类型
对于没有注释且没有默认值的参数。
元组参数
元组参数可以解析为变长元组,也可以解析为定长元组。
定长元组是不带类型变量...
注释的参数,
或具有混合类型的默认值的参数
# my_cli.pyfromtypingimportTuplefrommain_decimportmain@maindefrun(fixed_length_tuple1:Tuple[int,int],fixed_length_tuple2=(1,'arg')):pass
> python -m my_cli -h usage: my_cli [-h] [--fixed-length-tuple2 FIXED_LENGTH_TUPLE2 FIXED_LENGTH_TUPLE2] fixed_length_tuple1 fixed_length_tuple1positional arguments: fixed_length_tuple1optional arguments: -h, --help show this help message and exit --fixed-length-tuple2 FIXED_LENGTH_TUPLE2 FIXED_LENGTH_TUPLE2
变长元组是参数
- 用
...
作为类型变量进行注释(例如Tuple[int, ...]
) - 只是用
Tuple
或tuple
注释
- 将元组作为具有同构类型的默认值(例如
(1, 2)
)
枚举参数
可以使用用Enum
或默认Enum
类型注释的参数
为了强制一个参数必须有特定的值。
# my_cli.pyfromenumimportEnumfrommain_decimportmainclassChoice(Enum):first=1second=2@maindefrun(argument_with_choices:Choice):print('argument_with_choices',argument_with_choices)
> python -b my_cli -h usage: my_cli [-h] {first,second}positional arguments: {first,second}optional arguments: -h, --help show this help message and exit> python -m my_cli second argument_with_choices Choice.second
这可以与诸如typing.Tuple
和typing.List
之类的泛型类型组合使用。
作为具有tuple
或具有Enum
元素的list
类型的默认参数的参数。
cli文档
rest或google风格的doc字符串被解析并用于创建用法 以及帮助信息
# my_cli.pyfrommain_decimportmain@maindefrun(arg:str):""" An example cli :param arg: A required argument """
> python -m my_cli -h usage: my_cli [-h] argAn example clipositional arguments: arg A required argumentoptional arguments: -h, --help show this help message and exit