标签:equal zed 变量 pos ack password 要求 action 用法
argparse是python标准库里面用来处理命令行参数的库。
import argparse parser = argparse.ArgumentParser() parser.parse_args()
输出结果
XXX>python arg_example.py XXX>python arg_example.py -h usage: arg_example.py [-h] optional arguments: -h, --help show this help message and exit XXX>python arg_example.py a usage: arg_example.py [-h] arg_example.py: error: unrecognized arguments: a XXX>
positional arguments为英文定义,中文名叫有翻译为定位参数的,用法是不用带-就可用
import argparse parser = argparse.ArgumentParser() parser.add_argument("echo") args = parser.parse_args() print (args.echo) 输出结果: XXX>python arg_example.py a a XXX>python arg_example.py echo echo XXX>python arg_example.py usage: arg_example.py [-h] echo arg_example.py: error: the following arguments are required: echo
定义了一个叫echo
的参数,默认必选
echo
参数为空,所以报错,并给出用法(usage)和错误信息中文名叫可选参数,有两种方式:
-
来指定的短参数,如-h
;--
来指定的长参数,如--help
import argparse parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbo", help="verbo usage") args = parser.parse_args() if args.verbo: print ("verbo turned on %s" % args.verbo) 输出结果: XXX>python arg_example.py -v hello verbo turned on hello XXX>python arg_example.py --verbo h ello verbo turned on hello XXX>python arg_example.py -h usage: arg_example.py [-h] [-v VERBO] optional arguments: -h, --help show this help message and exit -v VERBO, --verbo VERBO verbo usage XXX>python arg_example.py a usage: arg_example.py [-h] [-v VERBO] arg_example.py: error: unrecognized arguments: a
定义了可选参数-v
或--verbo
,通过解析后,其值保存在args.verbo
变量中
通过定义参数时指定action="store_true",可以像
-h
那样,不需要指定参数值
import argparse parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbo", help="verbo usage", action="store_true") args = parser.parse_args() if args.verbo: print("verbo turned on %s" % args.verbo) 输出结果: XXX>python arg_example.py -v verbo turned on True XXX>python arg_example.py -h usage: arg_example.py [-h] [-v] optional arguments: -h, --help show this help message and exit -v, --verbo verbo usage XXX>python arg_example.py -v hello usage: arg_example.py [-h] [-v] arg_example.py: error: unrecognized arguments: hello
可以配置的参数有以下:
import argparse parser = argparse.ArgumentParser() parser.add_argument("-v", "--verbo", help="verbo usage" ,default=1) args = parser.parse_args() if args.verbo: print("verbo turned on %s" % args.verbo)
import argparse parser = argparse.ArgumentParser() parser.add_argument(‘-x‘, type=int, help="square") args = parser.parse_args() answer = args.x ** 2 print(answer)
import argparse parser = argparse.ArgumentParser(description="calculate X to the power of Y") parser.add_argument("square", type=int, help="display a square of a given number") parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2], default=1, help="increase output verbosity") args = parser.parse_args() answer = args.square**2 if args.verbosity == 2: print("the square of {} equals {}".format(args.square, answer)) elif args.verbosity == 1: print("{}^2 == {}".format(args.square, answer)) else: print(answer)
输出结果
XXX>python arg_example.py 3 -v 1 3^2 == 9 XXX>python arg_example.py 3 -v 2 the square of 3 equals 9 XXX>python arg_example.py 3 3^2 == 9 XXX>python arg_example.py usage: arg_example.py [-h] [-v {0,1,2}] square arg_example.py: error: the following arguments are required: square XXX>python arg_example.py -h usage: arg_example.py [-h] [-v {0,1,2}] square calculate X to the power of Y positional arguments: square display a square of a given number optional arguments: -h, --help show this help message and exit -v {0,1,2}, --verbosity {0,1,2} increase output verbosity
import argparse parser = argparse.ArgumentParser( description="ftp客户端,使用-S 127.0.0.1 -P 9988 -u hh -p 123") parser.add_argument("-s", "--server", type=str, default=‘127.0.0.1‘, dest="server", help="ftp server ip_addr") parser.add_argument("-P", "--port", type=int, dest="port", help="ftp server port") parser.add_argument("-u", "--username", type=str, choices=[‘alex‘, ‘egon‘, ‘eva‘], dest="username", help="username info") parser.add_argument("-p", "--password", dest="password", help="password info") args = parser.parse_args() print(‘server:{} port:{} username:{} password:{}‘.format(args.server, args.port, args.username, args.password))
输出结果
XXX>python arg_example.py -h usage: arg_example.py [-h] [-s SERVER] [-P SERVER] [-u {alex,egon,eva}] [-p PASSWORD] ftp客户端,使用-S 127.0.0.1 -P 9988 -u hh -p 123 optional arguments: -h, --help show this help message and exit -s SERVER, --server SERVER ftp server ip_addr -P SERVER, --port SERVER ftp server port -u {alex,egon,eva}, --username {alex,egon,eva} username info -p PASSWORD, --password PASSWORD password info XXX>python arg_example.py -s 127.0.0.1 -P 9988 -u en -p 123 usage: arg_example.py [-h] [-s SERVER] [-P PORT] [-u {alex,egon,eva}] [-p PASSWORD] arg_example.py: error: argument -u/--username: invalid choice: ‘en‘ (choose from ‘alex‘, ‘egon‘, ‘eva‘) XXX>python arg_example.py -s 127.0.0.1 -P 9988 -u alex -p 123 server:127.0.0.1 port:9988 username:alex password:123 XXX>python arg_example.py -s 127.0.0.1 -P 9988 -u alex -p 123456 server:127.0.0.1 port:9988 username:alex password:123456
参考http://yarving.historytale.com/
标签:equal zed 变量 pos ack password 要求 action 用法
原文地址:https://www.cnblogs.com/xiao-apple36/p/9302617.html