互斥参数:mutually exclusive arguments 我们可以在程序中创建一个互斥组,argparse 将会确保互斥组中只有一个参数在命令行中可用。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 importmath importargparse parser=argparse.ArgumentParser(description='Calculate a volume of a cyli...
cls.__name__, key))#name and suffix are mutually exclusiveif'name'ininitkwargsand'suffix'ininitkwargs:raiseTypeError("%s() received both `name` and `suffix`, which are""mutually exclusive arguments."% (cls.__name__))defview(request, *args, **kwargs): self= cls(**initkwargs)if'g...
这时–H和–r的位置可以随意安排了。 互斥参数:mutually exclusive arguments 我们可以在程序中创建一个互斥组,argparse 将会确保互斥组中只有一个参数在命令行中可用。 ''' ''' import math import argparse parser = argparse.ArgumentParser(description='Calculate a volume of a cylinder') parser.add_argument('...
positional arguments:textoptional arguments: -h, --help show this help message andexit -e, --encrypt -d, --decrypt -k KEY, --keyKEY 但是,仔细看了这段代码后,我发现(虽然有点主观)函数开头的几行(从7行到13行)定义了参数,但定义方式并不太优雅:它太臃肿了,而且完全是程式化的。
'_match_arguments_partial', '_mutually_exclusive_groups', '_negative_number_matcher', '_option_string_actions', '_optionals', '_parse_known_args', '_parse_optional', '_pop_action_class', '_positionals', '_print_message', '_read_args_from_files', '_registries', '_registry_get', '...
使用互斥参数首先通过 ArgumentParser.add_mutually_exclusive_group 在解析器中添加一个互斥组,然后在这个组里添加参数,那么组内的所有参数都是互斥的。 比如,我们希望通过命令行来告知乘坐的交通工具,要么是汽车,要么是公交,要么是自行车,那么就可以这么写:
def main(): # create parser descStr = """ This program analyzes playlist files (.xml) exported from iTunes. """① parser = argparse.ArgumentParser(description=descStr) # add a mutually exclusive group of arguments② group = parser.add_mutually_exclusive_group...
PROG: error: one of the arguments --foo --barisrequired 請注意,當前互斥的參數組不支持add_argument_group()的title和description參數。 注:本文由純淨天空篩選整理自python.org大神的英文原創作品argparse.ArgumentParser.add_mutually_exclusive_group。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授...
args,它是一个由 positional arguments 组成的列表。 Actions action 是 parse_args() 方法的参数之一,它指示 optparse 当解析到一个命令行参数时该如何处理。actions 有一组固定的值可供选择,默认是’store‘,表示将命令行参数值保存在 options 对象里。
py [-h] [--user USER] [--password PASSWORD] [--name NAME] optional arguments: -h, --help show this help message and exit --name NAME group_name: --user USER --password PASSWORD 互斥参数分组:有些参数不能同时设置. import argparse parser = argparse.ArgumentParser() group = parser.add...