python 无论如何说服python的getopt处理选项的可选参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1532737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there anyway to persuade python's getopt to handle optional parameters to options?
提问by stsquad
According to the documentation on python's getopt
(I think) the options fields should behave as the getopt()
function. However I can't seem to enable optional parameters to my code:
根据关于 python 的文档getopt
(我认为),选项字段的行为应与getopt()
函数相同。但是我似乎无法为我的代码启用可选参数:
#!/usr/bin/python
import sys,getopt
if __name__ == "__main__":
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "v::", ["verbose="])
except getopt.GetoptError, err:
print str(err)
sys.exit(1)
for o,a in opts:
if o in ("-v", "--verbose"):
if a:
verbose=int(a)
else:
verbose=1
print "verbosity is %d" % (verbose)
Results in:
结果是:
$ ./testopt.py -v
option -v requires argument
$ ./testopt.py -v 1
verbosity is 1
回答by SilentGhost
回答by Isaiah
Unfortunately, there is no way. From the optparse docs:
不幸的是,没有办法。从optparse 文档:
Typically, a given option either takes an argument or it doesn't. Lots of people want an “optional option arguments” feature, meaning that some options will take an argument if they see it, and won't if they don't. This is somewhat controversial, because it makes parsing ambiguous: if "-a" takes an optional argument and "-b" is another option entirely, how do we interpret "-ab"? Because of this ambiguity, optparse does not support this feature.
通常,给定的选项要么接受一个参数,要么不接受。很多人想要一个“可选的选项参数”功能,这意味着如果他们看到一些选项会接受一个参数,如果他们没有则不会。这有点有争议,因为它使解析模棱两可:如果“-a”接受一个可选参数而“-b”完全是另一个选项,我们如何解释“-ab”?由于这种歧义,optparse 不支持此功能。
EDIT:oops, that is for the optparse module not the getopt module, but the reasoning why neither module has "optional option arguments" is the same for both.
编辑:哎呀,这适用于 optparse 模块而不是 getopt 模块,但是这两个模块都没有“可选选项参数”的原因对于两者都是相同的。
回答by tponthieux
You can do an optional parameter with getopt like this:
你可以像这样使用 getopt 做一个可选参数:
import getopt
import sys
longopts, shortopts = getopt.getopt(sys.argv[1:], shortopts='', longopts=['env='])
argDict = dict(longopts)
if argDict.has_key('--env') and argDict['--env'] == 'prod':
print "production"
else:
print "sandbox"
Usage:
用法:
$ python scratch.py --env=prod
production
$ python scratch.py --env=dev
sandbox
$ python scratch.py
sandbox
回答by pixelbeat
python's getopt should really support optional args, like GNU getopt by requiring '=' be used when specifying a parameter. Now you can simulate it quite easily though, with this constraint by implicitly changing --option to --option=
python 的 getopt 应该真正支持可选的 args,就像 GNU getopt 通过要求在指定参数时使用 '=' 。现在你可以很容易地模拟它,通过隐式改变 --option 到 --option=
I.E. you can specify that --option requires an argument, and then adjust --option to --option= as follows:
IE 你可以指定 --option 需要一个参数,然后将 --option 调整为 --option= 如下:
for i, opt in enumerate(sys.argv):
if opt == '--option':
sys.argv[i] = '--option='
elif opt == '--':
break
回答by PTBNL
If you're using version 2.3 or later, you may want to try the optparsemodule instead, as it is "more convenient, flexible, and powerful ...", as well as newer. Alas, as Pynt answered, it doesn't seem possible to get exactly what you want.
如果您使用的是 2.3 或更高版本,您可能想尝试使用optparse模块,因为它“更方便、灵活和强大......”,以及更新。唉,正如 Pynt 回答的那样,似乎不可能得到你想要的东西。