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)
参数 | 描述 |
---|---|
prog | The name of the program (default: sys.argv[0]) |
usage | The string describing the program usage (default: generated from arguments added to parser) |
description | Text to display before the argument help (default: none) |
epilog | Text to display after the argument help (default: none) |
parents | A list of ArgumentParser objects whose arguments should also be included |
formatter_class | A class for customizing the help output |
prefix_chars | The set of characters that prefix optional arguments (default: ‘-‘) |
fromfile_prefix_chars | The set of characters that prefix files from which additional arguments should be read (default: None) |
argument_default | The global default value for arguments (default: None) |
conflict_handler | The strategy for resolving conflicting optionals (usually unnecessary) |
add_help | Add a -h/–help option to the parser (default: True) |
allow_abbrev | Allows 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. |
action | The basic type of action to be taken when this argument is encountered at the command line. |
nargs | 设置参数的个数 |
const | A constant value required by some action and nargs selections. |
default | 参数的默认值 |
type | 输入参数的类型 |
choices | 参数的值只可以在这其中选取 |
required | 参数是必须的,不能为空 |
help | (str) 参数的说明 |
metavar | A 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