在 Python 3.2 上设置 argparse
我正在尝试在我的 python 程序上设置 argparse 但它不起作用。 我尝试处理的参数如下:
第一个参数为“--interactive”或整数,其中一个是必需的
“--xml”或“--html”或“--”中的任何一个文本'或'--控制台'。同样,它可以是其中任何一个,但第二个参数需要其中之一
,最后对于第三个参数,“--verbose”标志是可选的。
除了第一个参数的整数之外,所有这些参数都将变量更改为 True。
这是我目前的代码:
import argparse
parser = argparse.ArgumentParser(description='Python Historical Event Calculator.',
prog='tempus.py')
inputs = parser.add_mutually_exclusive_group(required=True)
exports = parser.add_mutually_exclusive_group(required=True)
inputs.add_argument('integer', metavar='I', type=float,
help='percentage to use')
inputs.add_argument('-i','--interactive', dest='bool_interactive',
action='store_true', help='enter interactive mode')
exports.add_argument('-x','--xml', dest='bool_xml', action='store_true',
help='export output as xml')
exports.add_argument('--html', dest='bool_html', action='store_true',
help='export output as html')
exports.add_argument('-t','--text', dest='bool_text', action='store_true',
help='export output as plaintext')
exports.add_argument('-c','--console', dest='bool_con', action='store_true',
help='export output to console')
parser.add_argument('-v','--verbose', dest='verbose', action='store_true',
help='enter verbose/debug mode', required=False)
args = parser.parse_args()
但我不知道我是否走在正确的轨道上,有人可以帮忙吗?这看起来是对的还是我完全错了?
编辑
当我向它传递任何标志时,我会得到这个回溯:
Traceback (most recent call last):
File "C:\Users\Callum\Dropbox\Tempus\Feature Tests\argparsetest.py", line 9, in <module>
help='percentage to use')
File "C:\Python32\lib\argparse.py", line 1305, in add_argument
return self._add_action(action)
File "C:\Python32\lib\argparse.py", line 1528, in _add_action
raise ValueError(msg)
ValueError: mutually exclusive arguments must be optional
I'm trying to set up argparse on my python program but it's not working.
The arguments I'm trying to process are as follows:
Either '--interactive' OR an integer for the first argument, one of these is required
Any one of either '--xml' OR '--html' OR '--text' OR '--console'. Again, it can be any one of these but one of them is required for the second argument
And finally for the third argument, a '--verbose' flag which is optional.
All of these arguments change variables to True, apart from the integer on the first argument.
This is the code I have at the minute:
import argparse
parser = argparse.ArgumentParser(description='Python Historical Event Calculator.',
prog='tempus.py')
inputs = parser.add_mutually_exclusive_group(required=True)
exports = parser.add_mutually_exclusive_group(required=True)
inputs.add_argument('integer', metavar='I', type=float,
help='percentage to use')
inputs.add_argument('-i','--interactive', dest='bool_interactive',
action='store_true', help='enter interactive mode')
exports.add_argument('-x','--xml', dest='bool_xml', action='store_true',
help='export output as xml')
exports.add_argument('--html', dest='bool_html', action='store_true',
help='export output as html')
exports.add_argument('-t','--text', dest='bool_text', action='store_true',
help='export output as plaintext')
exports.add_argument('-c','--console', dest='bool_con', action='store_true',
help='export output to console')
parser.add_argument('-v','--verbose', dest='verbose', action='store_true',
help='enter verbose/debug mode', required=False)
args = parser.parse_args()
But I have no idea if I'm on the right track with this though, can anyone help? Does this look about right or have I done it completely wrong?
Edit
I get this traceback when I pass any flag to it:
Traceback (most recent call last):
File "C:\Users\Callum\Dropbox\Tempus\Feature Tests\argparsetest.py", line 9, in <module>
help='percentage to use')
File "C:\Python32\lib\argparse.py", line 1305, in add_argument
return self._add_action(action)
File "C:\Python32\lib\argparse.py", line 1528, in _add_action
raise ValueError(msg)
ValueError: mutually exclusive arguments must be optional
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的错误,
ValueError:互斥参数必须是可选的
,是因为您将整数
(位置参数)添加到互斥组中而发生的。互斥组仅适用于可选参数,而位置参数始终是必需的。一种解决方案是使交互式参数和整数可选参数都成为可选参数,并使它们互斥。我最初错过了这样一个事实:您在模式上使用了
mutually_exclusive_group
,因此仅指定了 xml、html、控制台或文本,但如果您确实喜欢这个想法,我确实对其进行了更改。这个解析器可以工作,它使您的
interactive
和integer
参数互斥,并使模式成为 选择列表。示例运行:
Your error,
ValueError: mutually exclusive arguments must be optional
, happened because you are addinginteger
(a positional argument), to a mutually exclusive group. Mutually exclusive groups are only for optional arguments, whereas positional arguments are always required. One solution is to make bothinteractive
andinteger
optional arguments, and mutually exclusive them.I originally missed the fact that you used a
mutually_exclusive_group
on your modes, so that only xml, html, console, or text were specified, but I did change it up if you do like that idea.This parser would work, it makes your
interactive
andinteger
arguments mutually exclusive, and makes mode a choice list.Sample run:
将
integer
更改为可选的positional
这样,
-i
和33
都会被接受>inputs
组,-i 33
不会。Change
integer
to be an optionalpositional
With this,
-i
and33
would both be accepted by theinputs
group,-i 33
would not.