帮助屏幕中忽略了 argparse 的 "required"?
argparse's "required" ignored in the help screen?
考虑以下代码:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-o', required=True, nargs=1)
parser.parse_args(['-h'])
即使我说 -o
是必需的,为什么我会得到以下输出?
usage: test.py [-h] -o O
optional arguments:
-h, --help show this help message and exit
-o O
我希望帮助文本说 -o
是必需的。
通常 argparse
的范例是按位置传递所需的参数,作为选项传递的参数应该是 可选的 。考虑到这个 'bad form',docs 甚至不鼓励设置 required
标志。
我只能建议生成的误导性帮助消息是 argparse 中的一个小缺点,您可以在 python 的错误跟踪器上为它创建一个票证。
如果必需选项。
对您来说至关重要,您可以覆盖默认用法消息
这是 issue #9694 on the Python bug tracker,尚未修复。你可以在那里阅读更多。请注意,用法行确实正确指示需要开关(如果不需要,用法行将显示为 usage: test.py [-h] [-o O]
而不是 usage: test.py [-h] -o O
)。
为了解决这个问题,您可以 use argument groups,它可以被赋予 title
。如链接页面中的示例所示,这允许您使用您选择的名称而不是默认的 positional arguments
和 optional arguments
分组来创建组。
简单的答案是没有 'required arguments' 组。 2 个默认组已标记(无论好坏)
positional arguments
optional arguments
实际上有 4 'requirement' 种选择 - 普通可选、普通位置、必需可选、可选位置。如果名称令人困惑,请坚持使用前两个。 :)
parser = argparse.ArgumentParser()
parser.add_argument('foo', help='a positional argument')
reqgroup=parser.add_argument_group('required arguments')
reqgroup.add_argument('-o', required=True, help='required optional')
parser.add_argument('-n', help='normal optional')
parser.add_argument('bar',nargs='?', help='optional positional argument')
parser.print_help()
产生:
usage: ipython3.5 [-h] -o O [-n N] foo [bar]
positional arguments:
foo a positional argument
bar optional positional argument
optional arguments:
-h, --help show this help message and exit
-n N normal optional
required arguments:
-o O required optional
这些备选方案在用法行中象征性地显示,没有(可能)令人困惑的标签。
就其价值而言,'optionals' 和 'positionals' 之间的区别深深地嵌入在 argparse
代码中。一种由标志字符串 ('-f','--foo') 标识,另一种由位置标识。一个或另一个是 'required' 的事实是肤浅的。唯一让一些 optionals
'required' 的是在解析结束时进行一些错误检查。
考虑以下代码:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-o', required=True, nargs=1)
parser.parse_args(['-h'])
即使我说 -o
是必需的,为什么我会得到以下输出?
usage: test.py [-h] -o O
optional arguments:
-h, --help show this help message and exit
-o O
我希望帮助文本说 -o
是必需的。
通常 argparse
的范例是按位置传递所需的参数,作为选项传递的参数应该是 可选的 。考虑到这个 'bad form',docs 甚至不鼓励设置 required
标志。
我只能建议生成的误导性帮助消息是 argparse 中的一个小缺点,您可以在 python 的错误跟踪器上为它创建一个票证。
如果必需选项。
对您来说至关重要,您可以覆盖默认用法消息这是 issue #9694 on the Python bug tracker,尚未修复。你可以在那里阅读更多。请注意,用法行确实正确指示需要开关(如果不需要,用法行将显示为 usage: test.py [-h] [-o O]
而不是 usage: test.py [-h] -o O
)。
为了解决这个问题,您可以 use argument groups,它可以被赋予 title
。如链接页面中的示例所示,这允许您使用您选择的名称而不是默认的 positional arguments
和 optional arguments
分组来创建组。
简单的答案是没有 'required arguments' 组。 2 个默认组已标记(无论好坏)
positional arguments
optional arguments
实际上有 4 'requirement' 种选择 - 普通可选、普通位置、必需可选、可选位置。如果名称令人困惑,请坚持使用前两个。 :)
parser = argparse.ArgumentParser()
parser.add_argument('foo', help='a positional argument')
reqgroup=parser.add_argument_group('required arguments')
reqgroup.add_argument('-o', required=True, help='required optional')
parser.add_argument('-n', help='normal optional')
parser.add_argument('bar',nargs='?', help='optional positional argument')
parser.print_help()
产生:
usage: ipython3.5 [-h] -o O [-n N] foo [bar]
positional arguments:
foo a positional argument
bar optional positional argument
optional arguments:
-h, --help show this help message and exit
-n N normal optional
required arguments:
-o O required optional
这些备选方案在用法行中象征性地显示,没有(可能)令人困惑的标签。
就其价值而言,'optionals' 和 'positionals' 之间的区别深深地嵌入在 argparse
代码中。一种由标志字符串 ('-f','--foo') 标识,另一种由位置标识。一个或另一个是 'required' 的事实是肤浅的。唯一让一些 optionals
'required' 的是在解析结束时进行一些错误检查。