recommended command-line parse module
ls - default usage
ls dir - position argument 位置参数,仅依赖于 where on the command line
ls -l - optional argument
--help - come across a program without using it before
help option
--help, shortened to -h, the only option that is no need to be specified.
positional arguments and optional arguments
winter@winter-P910:~/PycharmProjects/automail$ runpy prog.py --help
usage: prog.py [-h] echo
positional arguments:
echo
- 按照严格的位置顺序收集参数
- default选项貌似没用
optional arguments:
-h, --help show this help message and exit
- 如果 optional argument is not used, then 被赋予 None,除非有default选项。
- 添加action='store_true'可以成为开关,不需要在命令行中添加value
- 'store_true'是特定的字符串
- '-v', '--verbose'添加缩写,可以按如下方式使用,'-v','--v','--verbose'
- 如果不使用,则默认为False,action是使用上述三种方式时的默认值
combining positional and optional arguments
Note that the order does not matter
示例:
import argparse
parse = argparse.ArgumentParser()
parse.add_argument('square', type=int, help='display a square of a given number')
# parse.add_argument('-v', '--verbose', action='store_true', help='increase output verbosity')
# parse.add_argument('-v', '--verbose', type=int, choices=[0, 1, 2], help='increase output verbosity')
parse.add_argument('-v', '--verbose', default=0, action='count', help='increase output verbosity')
args = parse.parse_args()
res = args.square**2
if args.verbose == 0:
print('0: {}'.format(res))
elif args.verbose == 1:
print('1: {}'.format(res))
elif args.verbose == 2:
print('2: {}'.format(res))
else:
print(res)
stdout:
winter@winter-P910:~/PycharmProjects/automail$ runpy prog.py 5 -v 0
usage: prog.py [-h] [-v] square
prog.py: error: unrecognized arguments: 0
winter@winter-P910:~/PycharmProjects/automail$ runpy prog.py 5 -v
1: 25
winter@winter-P910:~/PycharmProjects/automail$ runpy prog.py 5 -vv
2: 25
winter@winter-P910:~/PycharmProjects/automail$ runpy prog.py 5 -vvv
25
winter@winter-P910:~/PycharmProjects/automail$ runpy prog.py 5
0: 25
示例:
import argparse
parser = argparse.ArgumentParser(description="calculate X to the power of Y")
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
parser.add_argument("x", type=int, help="the base")
parser.add_argument("y", type=int, help="the exponent")
args = parser.parse_args()
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))
- group中的选项不能同时设定
- 可以添加description
stdout:
$ python3 prog.py --help
usage: prog.py [-h] [-v | -q] x y
calculate X to the power of Y
positional arguments:
x the base
y the exponent
optional arguments:
-h, --help show this help message and exit
-v, --verbose
-q, --quiet
$ python3 prog.py 4 2
4^2 == 16
$ python3 prog.py 4 2 -q
16
$ python3 prog.py 4 2 -v
4 to the power 2 equals 16
$ python3 prog.py 4 2 -vq
usage: prog.py [-h] [-v | -q] x y
prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose
$ python3 prog.py 4 2 -v --quiet
usage: prog.py [-h] [-v | -q] x y
prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose