CONTENTS
使用最基本的方法:
import sys for arg in sys.argv[0:]: print(arg);
测试:
C:\Python33>python test.py test.py C:\Python33>python test.py -1 2 --a test.py -1 2 --a
使用【 getopt 】库:
import sys import getopt agrv = sys.argv[1:]; optlist, args = getopt.getopt(agrv, "ab:c:"); print(optlist); print(args);
测试:
C:\Python33>python test.py [] [] C:\Python33>python test.py -a -b2 [('-a', ''), ('-b', '2')] [] C:\Python33>python test.py -a -b2 -c3 [('-a', ''), ('-b', '2'), ('-c', '3')] [] C:\Python33>python test.py -a -b 2 -c 3 [('-a', ''), ('-b', '2'), ('-c', '3')] [] C:\Python33>python test.py -a -b2 -c3 4 5 [('-a', ''), ('-b', '2'), ('-c', '3')] ['4', '5'] C:\Python33>python test.py -a -b2 -c3 -d Traceback (most recent call last): File "test.py", line 4, in <module> optlist, args = getopt.getopt(agrv, "ab:c:"); File "C:\Python33\lib\getopt.py", line 95, in getopt opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:]) File "C:\Python33\lib\getopt.py", line 195, in do_shorts if short_has_arg(opt, shortopts): File "C:\Python33\lib\getopt.py", line 211, in short_has_arg raise GetoptError(_('option -%s not recognized') % opt, opt) getopt.GetoptError: option -d not recognized
可以看到,其扩展性不太好,如果输入错了,就容易出现exception,代码处理起来比较麻烦。官方在文档的最后也推荐了另一个库【 argparse 】。
argparse:
Argparse Tutorial: https://docs.python.org/3/howto/argparse.html#id1
argparse:https://docs.python.org/3/library/argparse.html#module-argparse
【 argparse 】库功能很强大,根据官方的例子,可以直接对输入参数进行加减乘除。。这里作者根据官方的Tutorial,只学习最基本的功能。
空壳:
import argparse parser = argparse.ArgumentParser(); args = parser.parse_args();
测试:
C:\Python33>python test.py C:\Python33>python test.py -h usage: test.py [-h] optional arguments: -h, --help show this help message and exit
可以看到,-h选项已经自动生成了。
必选参数:
import argparse parser = argparse.ArgumentParser(); parser.add_argument("yourname",help="input your name"); args = parser.parse_args(); print("Hello " + args.yourname);
测试:
无参数,报错并提示。 C:\Python33>python test.py usage: test.py [-h] yourname test.py: error: the following arguments are required: yourname 无效参数,报错并提示。 C:\Python33>python test.py -x usage: test.py [-h] yourname test.py: error: the following arguments are required: yourname 自动生成了提示信息。 C:\Python33>python test.py -h usage: test.py [-h] yourname positional arguments: yourname input your name optional arguments: -h, --help show this help message and exit 正确参数。 C:\Python33>python test.py cstriker1407 Hello cstriker1407
必选int参数:
import argparse parser = argparse.ArgumentParser(); parser.add_argument("yourname",help="input your name"); parser.add_argument("age",help="input your age",type=int);#有了type args = parser.parse_args(); print("Hello " + args.yourname + ".Double of your age is " + str(2 * args.age));
测试:
C:\Python33>python test.py cstriker1407 100 Hello cstriker1407.Double of your age is 200
可选输入参数:
import argparse parser = argparse.ArgumentParser(); parser.add_argument("yourname",help="input your name"); parser.add_argument("--home",help="input your home"); args = parser.parse_args(); print("Hello " + args.yourname); if args.home:#如果设置了可选参数,这里的home就不是空 print("Your Home:" + args.home);
测试:
自动生成了帮助文档 C:\Python33>python test.py -h usage: test.py [-h] [--home HOME] yourname positional arguments: yourname input your name optional arguments: -h, --help show this help message and exit --home HOME input your home 输入顺序是没有关系的 C:\Python33>python test.py --home China cstriker1407 Hello cstriker1407 Your Home:China 输入顺序是没有关系的 C:\Python33>python test.py cstriker1407 --home China Hello cstriker1407 Your Home:China 可选参数,不输入是没有关系的 C:\Python33>python test.py cstriker1407 Hello cstriker1407
可选标志参数:
import argparse parser = argparse.ArgumentParser(); parser.add_argument("yourname",help="input your name"); parser.add_argument("--verbose",help="print all Info",action="store_true");#注意action args = parser.parse_args(); print("Hello " + args.yourname); if args.verbose: print("HI, THIS IS A VERBOSE INFO");
测试:
帮助信息已经改变了 C:\Python33>python test.py -h usage: test.py [-h] [--verbose] yourname positional arguments: yourname input your name optional arguments: -h, --help show this help message and exit --verbose print all Info C:\Python33>python test.py cstriker1407 Hello cstriker1407 C:\Python33>python test.py cstriker1407 --verbose Hello cstriker1407 HI, THIS IS A VERBOSE INFO
短字符串:
import argparse parser = argparse.ArgumentParser(); parser.add_argument("yourname",help="input your name"); parser.add_argument("-v","--verbose",help="print all Info",action="store_true"); args = parser.parse_args(); print("Hello " + args.yourname); if args.verbose: print("HI, THIS IS A VERBOSE INFO");
测试:
C:\Python33>python test.py -h usage: test.py [-h] [-v] yourname positional arguments: yourname input your name optional arguments: -h, --help show this help message and exit -v, --verbose print all Info C:\Python33>python test.py cstriker1407 -v Hello cstriker1407 HI, THIS IS A VERBOSE INFO
限制可选输入参数:
import argparse parser = argparse.ArgumentParser(); parser.add_argument("yourname",help="input your name"); parser.add_argument("--gender",help="input your gender",choices=["male", "female"]);#choice选项 args = parser.parse_args(); print("Hello " + args.yourname + " , your gender is:" + args.gender);
测试:
C:\Python33>python test.py -h usage: test.py [-h] [--gender {male,female}] yourname positional arguments: yourname input your name optional arguments: -h, --help show this help message and exit --gender {male,female} input your gender C:\Python33>python test.py cstriker1407 --gender male Hello cstriker1407 , your gender is:male C:\Python33>python test.py cstriker1407 --gender xxx usage: test.py [-h] [--gender {male,female}] yourname test.py: error: argument --gender: invalid choice: 'xxx' (choose from 'male', 'female')
这里只是简单的笔记下argparse最简单的使用方法,复杂的以后用到了再笔记吧。
【 optionparser 】库
该库已经deprecated了,这里就不笔记了。
发表评论