[英]Processing command-line arguments in prefix notation in Python
我正在尝试解析Python中的命令行,如下所示:
$ ./command -o option1 arg1 -o option2 arg2 arg3
换句话说,该命令采用无限数量的参数,并且每个参数可以选择在前面加上-o
选项,该选项与该参数具体相关。 我认为这被称为“前缀表示法”。
在Bourne shell中,我会做类似以下的事情:
while test -n "$1"
do
if test "$1" = '-o'
then
option="$2"
shift 2
fi
# Work with $1 (the argument) and $option (the option)
# ...
shift
done
看看Bash教程等,这似乎是公认的习惯用法,所以我猜测Bash已经过优化,可以通过这种方式使用命令行参数。
试图在Python中实现这种模式,我的第一个猜测是使用pop()
,因为这基本上是一个堆栈操作。 但我猜这在Python上不会有效,因为sys.argv
中的参数列表顺序错误,必须像队列一样处理(即从左侧弹出)。 我已经读过,列表没有优化用作Python中的队列。
所以,我的想法是:将argv
转换为collections.deque
并使用popleft()
,使用reverse()
反向argv
并使用pop()
,或者只使用int list索引本身。
有没有人知道更好的方法来做到这一点,否则我的哪些想法将是最好的Python实践?
p = argparse.ArgumentParser()
p.add_argument('-o', action='append')
for i in range(1, 4): p.add_argument('arg%d' % i)
args = p.parse_args('-o option1 arg1 -o option2 arg2 arg3'.split())
print args
# -> Namespace(arg1='arg1', arg2='arg2', arg3='arg3', o=['option1', 'option2'])
可以通过导入sys,然后将sys.argv [1:]分配给变量(例如“args”)来获得与Bourne / Bash中的“shift”等效的Python功能。 然后你可以使用args = args [1:]向左移动一次,或使用更高的数字移动多次。 参数索引将从0开始,而不是1.上面的示例看起来像:
import sys
args = sys.argv[1:]
while len(args):
if args[0] == '-o':
option = args[1]
args = args[2:] # shift 2
# Work with args[0] (the argument) and option (the option)
# ...
args = args[1:] # shift
你可以做
argv.pop(0)
拉出第一个元素并返回它。 但这可能效率低下。 也许。 我不确定argv
是如何实现的。 (再说,如果效率是重要的,为什么你使用Python是谁?)
但是,更多的Pythonic解决方案是迭代列表而不会弹出元素。 像这样:
o_flag = False
for a in argv:
if a == '-o':
o_flag = True
continue
# do whatever
o_flag = False
另外,我认为optparse
模块值得一提; 它是处理Python程序中的选项和参数的标准,尽管它可能对此任务有点过分,因为您已经拥有几个功能完善的解决方案。
就像是:
for arg in sys.argv[1:]:
# do something with arg
应该适合你。 除非你期望有大量的参数,否则我会选择最简单的代码(并且不要过于担心性能)。 argv [1:]忽略第一个argv值,它将是脚本的名称。
在python del sys.argv[1]
中执行与bash
shift
相同的操作。 在python中, sys.argv
是一个列表。 删除索引1
上的元素与bash
shift
相同。
下面的示例python脚本将有助于跳过参数中的-o
并考虑所有其他参数。
#!/usr/bin/python
import sys
if __name__ == '__main__':
while len(sys.argv) > 1:
if sys.argv[1] != "-o":
print sys.argv[1]
del sys.argv[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.