步驟:
import argparse
# 收集命令行參數(shù)信息
parser = argparse.ArgumentParser(description="calculate X to the power of Y")
# 添加參數(shù)
parser.add_argument("x", type=int, help="the base")
# 返回已解析的參數(shù)
args = parser.parse_args()
print(args.x)
實(shí)例:
import argparse
parser = argparse.ArgumentParser(description="calculate X to the power of Y")
# 創(chuàng)建命令行參數(shù)組,兩個(gè)參數(shù)互相排斥 [-v | -q]
group = parser.add_mutually_exclusive_group()
# 以 - 和 -- 命名的參數(shù)是,可選參數(shù)
# action="store_true" 表示當(dāng)該可選參數(shù)出現(xiàn)時(shí)抗悍,它的值為 True 瞎领,否則,F(xiàn)alse
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
# 不以連接符開頭的參數(shù)為必須參數(shù)(位置參數(shù)) (require argument)
# type=int , 將值轉(zhuǎn)換為 int 類型,默認(rèn)為 string
parser.add_argument("x", type=int, help="the base")
parser.add_argument("y", type=int, help="the exponent")
# 解析參數(shù),將命令行參數(shù)作為 args 的屬性, args.verbose
args = parser.parse_args()
# 以下是獲取已解析的命令行參數(shù)
answer = args.x**args.y
if args.quiet:
print(answer)
elif args.verbose:
print("{} to the power {} equals {}".format(args.x, args.y, answer))
else:
print("{}^{} == {}".format(args.x, args.y, answer))
arg.py
argparse 會(huì)自動(dòng)生成
-h
--help
及其他幫助信息
其他
限制參數(shù)的值
# choices:參數(shù) -v 只能是列表中的值
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2], help="increase output verbosity")
統(tǒng)計(jì)參數(shù)出現(xiàn)的次數(shù)
# action='count' 返回參數(shù)出現(xiàn)的次數(shù):-vvv (verbosity=3)
# 可用于顯示信息的詳細(xì)程度
parser.add_argument("-v", "--verbosity", action="count", help="increase output verbosity")
詳細(xì)
https://docs.python.org/3/library/argparse.html#module-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))
參數(shù)說明
ArgumentParser
ArgumentParser
最常用的參數(shù)是:description
parser = argparse.ArgumentParser(description='Process some integers.')
add_augmemt
add_augment 用來定義如何解析一個(gè)參數(shù)
它接受的第一個(gè)位置參數(shù)是 name / flag:
-
'integers'
:name -
'-v, --verbose'
:flag
執(zhí)行 args = parser.parse_args()
以上參數(shù)的值可以通過 args.integers
搏嗡, args.verbose
獲取拉一;
- 可以通過參數(shù)
dest=sum
來表示該參數(shù)的值通過args.sum
獲炔珊小; - 默認(rèn)蔚润,name / flag 也會(huì)顯示在幫助信息中磅氨,可以通過
metavar
修改
ArgumentParser.add_argument
(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
Define how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are:
-
name or flags - Either a name or a list of option strings, 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 - The number of command-line arguments that should be consumed.
- const - A constant value required by some action and nargs selections.
- default - The value produced if the argument is absent from the command line.
- type - The type to which the command-line argument should be converted.
- choices - A container of the allowable values for the argument.
- required - Whether or not the command-line option may be omitted (optionals only).
- help - A brief description of what the argument does.
- metavar - A name for the argument in usage messages.
-
dest - The name of the attribute to be added to the object returned by
parse_args()
.