optparse():输入验证
如果已经在某处得到答复,我提前表示歉意;从上个小时起我就一直在 python 网站上。但不太清楚我该怎么做。我的脚本应该采用这样的选项:
myScript.py -f <file-name> -e [/ -d]
myScript.py -s <string> -e [/ -d]
myScript.py -f <file-name> [/ -s <string>] -e -w [<file_name>]
即 -f
/-s
,-e
/-d
是强制选项,但-f
和-s
不能一起使用,与-e
和-d options - 不能一起使用。我怎样才能把支票到位?
另一个问题,如果我可以同时问:我如何使用带有或不带有 oa 值的 -w
选项(使用时)?当未提供任何值时,应采用默认值,否则采用提供的值。 非常感谢任何帮助。干杯!!
My apology in advance if it's already answered somewhere; I've been in the python site since last hr. but didn't quite figure out how I can I do this. My script should take the options like this:
myScript.py -f <file-name> -e [/ -d]
myScript.py -s <string> -e [/ -d]
myScript.py -f <file-name> [/ -s <string>] -e -w [<file_name>]
i.e. -f
/-s
,-e
/-d
are mandatory options but -f
&-s
cannot be used together and the same as with -e
&-d
options - cannot be used together. How can I put the check in place?
Another question, if I may ask at the same time: How can I use -w
option (when used) with or w/o a value? When no value is supplied, it should take the default value otherwise the supplied one.
Any help greatly appreciated. Cheers!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经有一段时间没有使用 optparse 做过任何事情了,但我简要浏览了 docs 和一个旧程序。
“-f/-s、-e/-d 是强制选项,但 -f 和 -s 不能一起使用,并且与 -e 和 -d 选项相同 - 不能一起使用。我怎样才能把检查到位?”
为了相互排他性,您必须自己进行检查,例如:
由于此处的示例选项是布尔值,因此您可以将上面的 if 行替换为:
请参阅 optparse 如何处理错误 了解更多信息。
“如何使用带有或不带 oa 值的 -w 选项(使用时)?”
我从来没有找到一种方法来使用 optparse 选项,而该选项的值是可选的。 AFAIK,您必须将选项设置为具有值或不具有值。我最接近的是为选项指定 默认值它必须有一个值。那么不必在命令行上指定该条目。示例代码:
附带一个(希望有帮助的)建议:
如果您看过文档,您确实看到了关于“强制选项”是如何矛盾的部分,对吧? ;-p 抛开幽默不谈,您可能需要考虑重新设计界面,以便:
这个特定示例是从 处理布尔(标志)选项。对于类似的内容,您可能还需要查看 分组选项 部分;这个功能我没用过,就不多说了。
It's been a while since I did anything with optparse, but I took a brief look through the docs and an old program.
"-f/-s,-e/-d are mandatory options but -f&-s cannot be used together and the same as with -e&-d options - cannot be used together. How can I put the check in place?"
For mutual exclusivity, you have to do the check yourself, for example:
Since the example options here are boolean, you could replace the if line above with:
See the section How optparse handles errors for more.
"How can I use -w option (when used) with or w/o a value?"
I've never figured out a way to have an optparse option for which a value is, well, optional. AFAIK, you have to set the option up to have values or to not have values. The closest I've come is to specify a default value for an option which must have a value. Then that entry doesn't have to be specified on the command line. Sample code :
An aside with a (hopefully helpful) suggestion:
If you saw the docs, you did see the part about how "mandatory options" is an oxymoron, right? ;-p Humor aside, you may want to consider re-designing the interface, so that:
This particular example is borrowed (with slight expansion) from the section Handling boolean (flag) options. For something like this you might also want to check out the Grouping Options section; I've not used this feature, so won't say more about it.
如果您使用的是 2.7+,您应该尝试使用 argparse。
此部分应该是您想要的。
Tl;dr:
使
--foo
和--bar
互斥。有关使用ArgumentParser
的更多信息,请参阅详细的argparse
用法。请记住,
optparse
已被弃用,因此使用argparse
是一种无论如何,这是个好主意。You should try with
argparse
if you are using 2.7+.This section should be what you want.
Tl;dr:
makes
--foo
and--bar
mutually exclusive. See detailedargparse
usage for more informations on usingArgumentParser
sRemember that
optparse
is deprecated, so usingargparse
is a good idea anyway.