- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当使用 argparse 时,我得到了这个奇怪的 ValueError 帮助选项。所有其他选项都可以正常工作。只有帮助选项不起作用,这很奇怪。
这是错误信息:
chaudhary@localhost:~/code/python/logmein$ ./logmein.py --bla
Usage: ./logmein.py [filename <Default: .login.txt>] || [password] || [username password]
chaudhary@localhost:~/code/python/logmein$ ./logmein.py --boo
Usage: ./logmein.py [filename <Default: .login.txt>] || [password] || [username password]
chaudhary@localhost:~/code/python/logmein$ ./logmein.py -b
Usage: ./logmein.py [filename <Default: .login.txt>] || [password] || [username password]
chaudhary@localhost:~/code/python/logmein$ ./logmein.py -h
Unexpected Error: <class 'ValueError'>
Details: unsupported format character 'p' (0x70) at index 1
Traceback (most recent call last):
File "./logmein.py", line 222, in <module>
main(sys.argv)
File "./logmein.py", line 158, in main
args, otherthings = parser.parse_known_args()
File "/usr/lib64/python3.3/argparse.py", line 1737, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/lib64/python3.3/argparse.py", line 1943, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib64/python3.3/argparse.py", line 1883, in consume_optional
take_action(action, args, option_string)
File "/usr/lib64/python3.3/argparse.py", line 1811, in take_action
action(self, namespace, argument_values, option_string)
File "/usr/lib64/python3.3/argparse.py", line 1015, in __call__
parser.print_help()
File "/usr/lib64/python3.3/argparse.py", line 2339, in print_help
self._print_message(self.format_help(), file)
File "/usr/lib64/python3.3/argparse.py", line 2323, in format_help
return formatter.format_help()
File "/usr/lib64/python3.3/argparse.py", line 276, in format_help
help = self._root_section.format_help()
File "/usr/lib64/python3.3/argparse.py", line 206, in format_help
func(*args)
File "/usr/lib64/python3.3/argparse.py", line 293, in _format_usage
usage = usage % dict(prog=self._prog)
ValueError: unsupported format character 'p' (0x70) at index 1
这是我的代码
# Parse command line arguments
from argparse import ArgumentParser
usage = "%prog [-f credential_file]"
parser = ArgumentParser(usage=usage)
parser.add_argument("-f", "--file", type=str, dest="file",
help="Use the specified file")
parser.add_argument("-o", "--logout", action='store_true', dest="logout",
help="Logout")
#parser.add_argument('otherthings', nargs='*')
#args = parser.parse_args()
args, otherthings = parser.parse_known_args()
argc = len(otherthings)
if args.file != None:
username, password = parse_file_for_credential(args.file)
现在我找不到为什么会出现这个 ValueError,而且也只是针对 --help 选项。
最佳答案
问题是,当 argparse 尝试插入您的 usage
字符串时,Python 会看到以下字符序列:%p
。因为 p
不是 a valid conversion flag , Python 引发了 ValueError
。
您可以自己在 REPL 中生成此错误:
>>> "This is valid: %s" % "magic"
"This is valid: magic"
>>> "This will raise a ValueError: %p" % "magic"
ValueError: unsupported format character 'p' (0x70)
发生这种情况的原因是因为您使用了 %prog
而不是正确的 %(prog)s
(请注意 %s 中键周围的括号
)。按照 Rob 的建议将 usage = "%prog [-f credential_file]"
更改为 usage = "%(prog)s [-f credential_file]"
一切都会正常进行。
argparse 通过named 格式参数提供程序名称等元数据,以允许参数以任何顺序出现,并允许重复。位置参数不能重复使用,必须按照提供的顺序出现:
>>> "If I want another %s then I need to add another %s" % ("param", "placeholder")
"If I want another param then I need to add another placeholder"
# There is no way to change the order around to be
# "If I want another placeholder then I need to add another param"
# without changing the order of the parameters passed to `%`
虽然命名参数可以重复并以任何顺序使用:
>>> s = "Any order, %(two)s / %(one)s. Repeats %(one)s / %(two)s are possible"
>>> s % {"one": "first", "two": "last"}
"Any order, last / first. Repeats first / last are possible"
关于python - 参数解析 : ValueError when using --help or -h option with nargs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26247719/
我一直在使用 AJAX 从我正在创建的网络服务中解析 JSON 数组时遇到问题。我的前端是一个简单的 ajax 和 jquery 组合,用于显示从我正在创建的网络服务返回的结果。 尽管知道我的数据库查
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我在尝试运行 Android 应用程序时遇到问题并收到以下错误 java.lang.NoClassDefFoundError: com.parse.Parse 当我尝试运行该应用时。 最佳答案 在这
有什么办法可以防止etree在解析HTML内容时解析HTML实体吗? html = etree.HTML('&') html.find('.//body').text 这给了我 '&' 但我想
我有一个有点疯狂的例子,但对于那些 JavaScript 函数作用域专家来说,它看起来是一个很好的练习: (function (global) { // our module number one
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我需要编写一个脚本来获取链接并解析链接页面的 HTML 以提取标题和其他一些数据,例如可能是简短的描述,就像您链接到 Facebook 上的内容一样。 当用户向站点添加链接时将调用它,因此在客户端启动
在 VS Code 中本地开发时,包解析为 C:/Users//AppData/Local/Microsoft/TypeScript/3.5/node_modules/@types//index而不是
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我被赋予了将一种语言“翻译”成另一种语言的工作。对于使用正则表达式的简单逐行方法来说,源代码过于灵活(复杂)。我在哪里可以了解更多关于词法分析和解析器的信息? 最佳答案 如果你想对这个主题产生“情绪化
您好,我在解析此文本时遇到问题 { { { {[system1];1;1;0.612509325}; {[system2];1;
我正在为 adobe after effects 在 extendscript 中编写一些代码,最终变成了 javascript。 我有一个数组,我想只搜索单词“assemble”并返回整个 jc3_
我有这段代码: $(document).ready(function() { // }); 问题:FB_RequireFeatures block 外部的代码先于其内部的代码执行。因此 who
背景: netcore项目中有些服务是在通过中间件来通信的,比如orleans组件。它里面服务和客户端会指定网关和端口,我们只需要开放客户端给外界,服务端关闭端口。相当于去掉host,这样省掉了些
1.首先贴上我试验成功的代码 复制代码 代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。 你可以通过本站学习 X
【PHP代码】 复制代码 代码如下: $stmt = mssql_init('P__Global_Test', $conn) or die("initialize sto
在SQL查询分析器执行以下代码就可以了。 复制代码代码如下: declare @t varchar(255),@c varchar(255) declare table_cursor curs
前言 最近练习了一些前端算法题,现在做个总结,以下题目都是个人写法,并不是标准答案,如有错误欢迎指出,有对某道题有新的想法的友友也可以在评论区发表想法,互相学习🤭 题目 题目一: 二维数组中的
我是一名优秀的程序员,十分优秀!