import argparse
parser
= argparse
.ArgumentParser
(description
='Process some integers.')
parser
.add_argument
('integers', metavar
='N', type=int, nargs
='+',
help='an integer for the accumulator')
parser
.add_argument
('--sum', dest
='accumulate', action
='store_const',
const
=sum, default
=max,
help='sum the integers (default: find the max)')
args
= parser
.parse_args
()
print(args
.accumulate
(args
.integers
))
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser
初始化对象
class argparse.ArgumentParser
(prog
=None, usage
=None, description
=None, epilog
=None, parents
=[], formatter_class
=argparse
.HelpFormatter
, prefix_chars
='-', fromfile_prefix_chars
=None, argument_default
=None, conflict_handler
='error', add_help
=True, allow_abbrev
=True)
参数描述
progThe name of the program (default: sys.argv[0])usageThe string describing the program usage (default: generated from arguments added to parser)descriptionText to display before the argument help (default: none)epilogText to display after the argument help (default: none)parentsA list of ArgumentParser objects whose arguments should also be includedformatter_classA class for customizing the help outputprefix_charsThe set of characters that prefix optional arguments (default: ‘-‘)fromfile_prefix_charsThe set of characters that prefix files from which additional arguments should be read (default: None)argument_defaultThe global default value for arguments (default: None)conflict_handlerThe strategy for resolving conflicting optionals (usually unnecessary)add_helpAdd a -h/–help option to the parser (default: True)allow_abbrevAllows long options to be abbreviated if the abbreviation is unambiguous. (default: True)
对象方法
ArgumentParser
.add_argument
(name
or flags
...[, action
][, nargs
][, const
][, default
][, type][, choices
][, required
][, help][, metavar
][, dest
])
参数描述
name or flags一个name或者列表包含多个name, e.g. foo or -f, --foo.actionThe basic type of action to be taken when this argument is encountered at the command line.nargs设置参数的个数constA constant value required by some action and nargs selections.default参数的默认值type输入参数的类型choices参数的值只可以在这其中选取required参数是必须的,不能为空help(str) 参数的说明metavarA name for the argument in usage messages.dest默认使用name中-或者--后的,也可以通过dest来指定
https://www.cnblogs.com/shmily2018/p/11592448.html https://blog.csdn.net/the_time_runner/article/details/97941409