带有 argparse 和多个 -v 选项的详细级别
我希望能够通过向命令行添加更多 -v 选项来指定不同的详细级别。例如:
$ myprogram.py
$ myprogram.py -v
$ myprogram.py -vv
$ myprogram.py -v -v -v
将分别导致 verbose=0、verbose=1、verbose=2 和 verbose=3。我如何使用 argparse 来实现这一点?
或者,如果能够像这样指定它可能会很棒
$ myprogram -v 2
I'd like to be able to specify different verbose level, by adding more -v options to the command line. For example:
$ myprogram.py
$ myprogram.py -v
$ myprogram.py -vv
$ myprogram.py -v -v -v
would lead to verbose=0, verbose=1, verbose=2, and verbose=3 respectively. How can I achieve that using argparse?
Optionally, it could be great to also be able to specify it like
$ myprogram -v 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
argparse 支持
action='count'
:输出:
唯一的小问题是,如果您不希望
-v
参数为您提供 0 而不是的详细级别,则必须显式设置
。default=0
无argparse supports
action='count'
:Output:
The only very minor niggle is you have to explicitly set
default=0
if you want no-v
arguments to give you a verbosity level of 0 rather thanNone
.您可以使用
nargs='?'
(在-v
标志后接受 0 或 1 个参数)和自定义操作(处理 0 或 1 个参数)来完成此操作:从命令行运行 script.py -v -v 会产生取消
注释 print 语句以更好地查看 VAction 正在执行的操作。
You could do this with
nargs='?'
(to accept 0 or 1 arguments after the-v
flag) and a custom action (to process the 0 or 1 arguments):Running
script.py -v -v
from the command line yieldsUncomment the print statement to see better what the
VAction
is doing.您可以使用
append_const
处理问题的第一部分。否则,您可能会陷入编写自定义操作的困境,如细 unutbu 回答。输出:
You could handle the first part of your question with
append_const
. Otherwise, you're probably stuck writing a custom action, as suggested in the fine answer by unutbu.Output:
这是我对此的看法,它不使用任何新类,适用于 Python 2 和 3,并支持使用“-v”/“--verbose”和“-q”/“--quiet”从默认值进行相对调整,但它不支持使用数字,例如“-v 2”:
示例:
Here's my take on this that doesn't use any new classes, works in both Python 2 and 3 and supports relative adjustments from the default using "-v"/"--verbose" and "-q"/"--quiet", but it doesn't support using numbers e.g. "-v 2":
Example:
扩展 unutbu 的答案,这是一个自定义操作,包括处理 --quiet/-q 组合。这是在Python3中测试的。在Python >=2.7 中使用它应该没什么大不了的。
有一个方便的类方法,可用于设置
--verbose
、-v
、-q
、的所有四个选项处理程序>--安静
。像这样使用它:当使用具有这些参数的脚本时,您可以执行以下操作:
使用此命令行
args.verbose
将为4
。-v/-q/--verbose/--quiet
都是该给定数字的硬性、绝对的args.verbose
集(=详细级别)。-v/--verbose
都是该级别的增量。-q/--quiet
都是该级别的递减。-v/-q
可能会立即跟随更多v/q
字母,结果级别为旧级别 + sum(count('v' )) - sum(count('q'))
如果您想要不同的行为,自定义操作应该相当容易修改。例如,有些人喜欢任何
--quiet
将级别重置为 0,甚至重置为 -1。为此,请从-q
和--quiet
的 add_argument 中删除nargs
,并硬编码以设置value = 0< /code>
if option_string[0] == 'q'
。如果使用错误,正确的解析器错误会被很好地打印出来:
Expanding on unutbu's answer, here's a custom action including handling of a --quiet/-q combination. This is tested in Python3. Using it in Python >=2.7 should be no big deal.
There's a convenience class method which can be used to set up all four option handlers for
--verbose
,-v
,-q
,--quiet
. Use it like this:When using a script having these arguments you can do:
With this command line
args.verbose
would be4
.-v/-q/--verbose/--quiet
with a given number is a hard, absolute set ofargs.verbose
to that given number (=verbosity level).-v/--verbose
without a number is an increment of that level.-q/--quiet
without a number is a decrement of that level.-v/-q
may immediately be followed up with morev/q
letters, the resulting level isthe old level + sum(count('v')) - sum(count('q'))
The custom action should be fairly easy to modify in case you want a different behaviour. For example, some people prefer that any
--quiet
resets the level to 0, or even to -1. For this, dremove thenargs
from the add_argument of-q
and--quiet
, and also hardcode to setvalue = 0
if option_string[0] == 'q'
.Proper parser errors are nicely printed if usage is wrong:
argparse
支持append
操作,可让您指定多个参数。检查 http://docs.python.org/library/argparse.html,搜索“追加
”。argparse
supports theappend
action which lets you specify multiple arguments. Check http://docs.python.org/library/argparse.html, search for "append
".您提出的第一个方法更可能会造成混乱。不同详细级别的不同选项名称,或者一个可选地后跟详细级别的数字指示符的详细标志不太可能使用户感到困惑,并且将允许在分配详细级别时具有更大的灵活性。
Your first proposed method would be more likely to confuse. Different option names for different levels of verbosity, or one verbose flag optionally followed by a numeric indicator of the level of verbosity is less likely to confuse a user and would allow more flexibility in assigning verbosity levels.
我想出了一个替代方案;虽然它并不完全符合OP的要求,但它满足了我的要求,我认为值得分享。
使用互斥组来计算短选项的数量或存储长选项的整数值。
正如您所看到的,它允许您“堆叠”单个字符选项,并允许您使用长选项名称来显式设置值。缺点是您不能使用长选项作为开关(最后一个示例会生成异常。)
I've come up with an alternative; while it doesn't exactly match OP's request, it fulfilled my requirements and I thought it worth sharing.
Use a mutually exclusive group to either count the number of short options or store the integer value of a long option.
As you can see, it allows you to "stack" single char options and allows you to use the long option name to set the value explicitly. The downside is that you cannot use the long option as a switch (the last example generates an exception.)