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

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

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

例如:

import argparse
import sys

parser = 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 h
output:

['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 添加的规则来进行模式匹配。就可以较好的理解解析的结果了!


最后,感谢博文 http://4byte.cn/question/347481/argparse-how-to-handle-variable-number-of-arguments-nargs.html 的分享

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值