argparse

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值