python 命令行参数解析 argparse简单分析

在python 2.7 后,不推荐使用 optparse, 而推荐使用 argparse.

其它的不多说,简单的分析下我遇到的问题:我是想用 argparse 来解析不定长的命令行参数

例如:

import argparseimport sysparser = argparse.ArgumentParser(description='test parsing arguments')parser.add_argument('pos1', nargs='*')parser.add_argument('pos2')parser.add_argument('-o1')parser.add_argument('-o2')parser.add_argument('pos3', nargs='*')print sys.argv# arg = parser.parse_args(sys.argv[1:])arg = parser.parse_known_args(sys.argv[1:])print arg# print parser.print_help()假设将上面的代码保存在 test.py 文件中,在命令行中做如下测试:

input: python test.py a b c -o1 d e -o2 f g h

output:

[‘test.py’, ‘a’, ‘b’, ‘c’, ‘-o1’, ‘d’, ‘e’, ‘-o2’, ‘f’, ‘g’, ‘h’](Namespace(o1=’d’, o2=’f’, pos1=[‘a’, ‘b’], pos2=’c’, pos3=[]), [‘e’, ‘g’, ‘h’])

input: python test.py a -o1 b c -o2 d e f g houtput:

[‘test.py’, ‘a’, ‘-o1’, ‘b’, ‘c’, ‘-o2’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’](Namespace(o1=’b’, o2=’d’, pos1=[], pos2=’a’, pos3=[]), [‘c’, ‘e’, ‘f’, ‘g’, ‘h’])

input: python test.py -o1 a b c -o2 d e f g h

output:

[‘test.py’, ‘-o1’, ‘a’, ‘b’, ‘c’, ‘-o2’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’](Namespace(o1=’a’, o2=’d’, pos1=[‘b’], pos2=’c’, pos3=[]), [‘e’, ‘f’, ‘g’, ‘h’])

input: python test.py -o1 a -o2 b c d e f g h

output:

[‘test.py’, ‘-o1’, ‘a’, ‘-o2’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’](Namespace(o1=’a’, o2=’b’, pos1=[‘c’, ‘d’, ‘e’, ‘f’, ‘g’], pos2=’h’, pos3=[]), [])

做如上四组测试,传入的参数都一样(唯一不一样的就是位置),, 但是得到了不同的结果。

究其原因,这就是 argparse 模块的实现原理。 采用了正则匹配的方式 来解析参数(正则的说法属于个人理解,没有关注源码,如果有误,望更正)

在 add_argument 添加参数规则的时候,我们把带 ‘-‘ 的叫做选项参数,不带 ‘-‘ 的叫做位置参数。 下面我们以位置参数为例

在上面的代码中 通过 add_argument 添加了 pos1,pos2,pos3 三个位置参数,他们构成的 正则表达式为 A*AA*,其中A*代表匹配0个或多个值。

在测试例子1中的参数列表中,首先遇到位置参数 a b c ,这正好与 A*AA*的模式匹配,即 pos1=[a,b] pos2=c, pos3=[],因为A*AA*是贪婪匹配模式,即尽可能多的得到值,所以pos1=[a,b]而pos3=[]

在测试例子4中的参数列表中,A*AA* 匹配到参数列表中的 c d e f g h, 在贪婪匹配模式下,即可得到 pos1=[c,d,e,f,g], pos2=h, pos3=[]

通过分析上面两个例子,我们只需要记住 argparse 解析参数是根据 add_argument 添加的规则来进行模式匹配。就可以较好的理解解析的结果了!

最后,感谢博文 的分享

不求与人相比,但求超越自己,要哭就哭出激动的泪水,要笑就笑出成长的性格。

python 命令行参数解析 argparse简单分析

相关文章:

你感兴趣的文章:

标签云: