python argparse

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 included
formatter_classA class for customizing the help output
prefix_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值