parsers', 'add_argument', 'add_argument_group', 'add_help', 'add_mutually_exclusive_group', 'add_subparsers', 'argument_default', 'conflict_handler', 'convert_arg_line_to_args', 'description', 'epilog', 'error', 'exit', 'format_help', 'format_usage', 'format_version', 'formatter...
positional arguments:textoptional arguments: -h, --help show this help message andexit -e, --encrypt -d, --decrypt -k KEY, --keyKEY 但是,仔细看了这段代码后,我发现(虽然有点主观)函数开头的几行(从7行到13行)定义了参数,但定义方式并不太优雅:它太臃肿了,而且完全是程式化的。
这时–H和–r的位置可以随意安排了。 互斥参数:mutually exclusive arguments 我们可以在程序中创建一个互斥组,argparse 将会确保互斥组中只有一个参数在命令行中可用。 ''' ''' import math import argparse parser = argparse.ArgumentParser(description='Calculate a volume of a cylinder') parser.add_argument('...
group = parser.add_mutually_exclusive_group(required=True) group.add_argument('--foo', action='store_true') group.add_argument('--bar', action='store_false') print(parser.parse_args([])) #异常 <<< usage: PROG [-h] (--foo | --bar) PROG: error: one of the arguments --foo -...
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...
互斥参数就是多个参数之间彼此互斥,不能同时出现。使用互斥参数首先通过ArgumentParser.add_mutually_exclusive_group在解析器中添加一个互斥组,然后在这个组里添加参数,那么组内的所有参数都是互斥的。 比如,我们希望通过命令行来告知乘坐的交通工具,要么是汽车,要么是公交,要么是自行车,那么就可以这么写: ...
ArgumentParserlei add_argument类函数实参的字符串表示,add_argument函数定义add_argument(self, *args,**kwargs) ''' for option in options: eval('self.parser.add_argument(%s)' % option) def add_exclusive_arguments(self, options:list): ''' 添加互斥选项 :param options 格式为list,形如以下 [ (...
$ python file_parser_no_help.py --helpusage: File parser [--infile INFILE] [--out OUT]File parser: error: unrecognized arguments: --help 在下一节中,你将学习如何为你的参数添加别名! 添加别名 别名是一个花哨的词,指的是使用一个替代的标志来做同样的事情。例如,你知道你可以使用 -h和--help...
remove 移除操作options: -h, --help show this help message and exitpython tool.py add -h输出:usage: tool.py add [-h] itempositional arguments: item 要添加的项options: -h, --help show this help message and exit2. 互斥参数组 (Mutually Exclusive Groups)互斥参数组可以确保一...
args,它是一个由 positional arguments 组成的列表。 Actions action 是 parse_args() 方法的参数之一,它指示 optparse 当解析到一个命令行参数时该如何处理。actions 有一组固定的值可供选择,默认是’store‘,表示将命令行参数值保存在 options 对象里。