使用自定义formatter_class在Python 2.7.5 argparse.add_argument()中出现神秘故障
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用自定义formatter_class在Python 2.7.5 argparse.add_argument()中出现神秘故障,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2746字,纯文字阅读大概需要4分钟。
内容图文
![使用自定义formatter_class在Python 2.7.5 argparse.add_argument()中出现神秘故障](/upload/InfoBanner/zyjiaocheng/779/ca10433e8fb7447799b2ff59b492d2af.jpg)
我正在尝试编写一个HelpFormatter子类来与argparse一起使用.格式化程序很简单;将它作为子类集成不是.在Anthon的回答中,我在stackoverflow.com/questions/3853722/找到了一个非常有用的例子.
在Mac OS X 10.9.4上使用Python 2.7.5.当我尝试子类化HelpFormatter时,我不断得到:
./testBlankLineHelpFormatter.py -q
******* LOADING MY CLASS
Instantiating argparse.ArgumentParser
Traceback (most recent call last):
File "./testBlankLineHelpFormatter.py", line 15, in <module>
formatter_class=BlankLineHelpFormatter
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1600, in __init__
help=_('show this help message and exit'))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1293, in add_argument
raise ValueError("length of metavar tuple does not match nargs")
ValueError: length of metavar tuple does not match nargs
*请注意,错误是在实例化我的子类时,当标准类尝试添加“–help”项时 – 它永远不会到达我的任何add_argument()调用.我把我的子类修剪成这个,它仍然失败:
class BlankLineHelpFormatter(argparse.HelpFormatter):
"""
A formatter for argparse that just respects blank lines (as in, doesn't
wrap across them).
See also: http://bugs.python.org/issue12806
"""
sys.stderr.write("******* LOADING MY CLASS\n")
def __init__(self, *args, **kw):
sys.stderr.write("******* IN MY INIT\n")
super(BlankLineHelpFormatter, self).__init__(*args, **kw)
*我正在使用一个驱动程序运行它我也调整了下来,对此:
#!/usr/bin/python
#
from __future__ import print_function
import argparse
import BlankLineHelpFormatter
print("Instantiating argparse.ArgumentParser")
parser = argparse.ArgumentParser(
description="""
This work is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, look it up.
""",
formatter_class=BlankLineHelpFormatter
)
print("Adding --quiet\n")
parser.add_argument(
"--quiet", "-q", action='store_true',
help='Suppress most messages.')
print("Instantiated, now trying parse_args")
args = parser.parse_args()
print("Back.")
print("You might want to try '-h'...")
sys.exit(0)
我查看了argparse库源代码,即使在上下文中,问题对我也没有意义.这是一个阻止子类化HelpFormatter的错误,还是我在剩余的代码中遗漏了一些东西?
非常感谢您的帮助!
解决方法:
您将BlankLineHelpFormatter模块作为格式化程序而不是BlankLineHelpFormatter类传递.错误消息来自this part of the argparse source:
# raise an error if the metavar does not match the type
if hasattr(self, "_get_formatter"):
try:
self._get_formatter()._format_args(action, None)
except TypeError:
raise ValueError("length of metavar tuple does not match nargs")
_get_formatter()尝试调用模块来创建格式化程序,然后将生成的TypeError误解为其他内容.
修复应该是指定
formatter_class=BlankLineHelpFormatter.BlankLineHelpFormatter
内容总结
以上是互联网集市为您收集整理的使用自定义formatter_class在Python 2.7.5 argparse.add_argument()中出现神秘故障全部内容,希望文章能够帮你解决使用自定义formatter_class在Python 2.7.5 argparse.add_argument()中出现神秘故障所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。