Skip to main content
 首页 » 编程设计

python之参数解析器 : exclusive groups not enforced when using some argument options

2025年02月15日8wuhuacong

import argparse 
 
parser = argparse.ArgumentParser() 
group = parser.add_mutually_exclusive_group() 
group.add_argument('--foo', nargs='?', default=True, const=True) 
group.add_argument('--bar', dest='foo', action='store_false') 
parser.parse_args(['--foo', '--bar']) # no error 

我有点困惑,尽管 --foo 和 --bar 应该是互斥的,但上面的代码没有产生任何错误。这是预期的行为吗?或者我们不应该在排他组中乱用参数选项吗?

请注意,当 default=True 未作为参数传递给 --foo 时,会发生预期的错误。

我在 Python 2.7.13 和 3.5.3 中观察到这种行为。

请您参考如下方法:

相关代码埋在take_action中:

        argument_values = self._get_values(action, argument_strings) 
 
        # error if this argument is not allowed with other previously 
        # seen arguments, assuming that actions that use the default 
        # value don't really count as "present" 
        if argument_values is not action.default: 
            seen_non_default_actions.add(action) 
            for conflict_action in action_conflicts.get(action, []): 
                if conflict_action in seen_non_default_actions: 
                    msg = _('not allowed with argument %s') 
                    action_name = _get_action_name(conflict_action) 
                    raise ArgumentError(action, msg % action_name) 

特别是 'argument_values is not action.default' 测试。

可选位置 (nargs='?') 始终是“可见的”,因为空列表满足其nargs。在这种情况下,它获得action.default值。这是由 _get_values()` 中的一个特殊情况处理的。

但是对于相互排斥的测试,我们不希望这些操作被“看到”,因此这个额外的测试来设置“seen_non_default_actions”集。

is 测试非常严格。这些项目必须具有相同的 id。

您的示例失败,因为 const=True 中的 True 与 default=True 中的 id 相同。

parser.parse_args(['--foo', '--bar']) 将 foo 设置为 const。但是因为它与 default 相匹配,所以它是看不见的,并且不会触发独占错误。

通常 const 和 default 会有不同的值,利用 '?' 的三向解析。

这个不是测试在另一种情况下失败了。小于 256 的数字是唯一的。有关更多信息,请访问 http://bugs.python.org/issue18943


测试用例:

In [1]: import argparse 
   ...: parser = argparse.ArgumentParser() 
   ...: group = parser.add_mutually_exclusive_group() 
   ...: a1=group.add_argument('--foo', nargs='?', default=True, const=True) 
   ...: a2=group.add_argument('--bar', action='store_false') 
   ...:  
In [2]: parser.parse_args(['--foo', '--bar']) 
Out[2]: Namespace(bar=False, foo=True)      # the True's match 

改变常量:

In [3]: a1.const 
Out[3]: True 
In [4]: a1.const='other' 
In [5]: parser.parse_args(['--foo', '--bar']) 
usage: ipython3 [-h] [--foo [FOO] | --bar] 
ipython3: error: argument --bar: not allowed with argument --foo 
An exception has occurred, use %tb to see the full traceback. 
SystemExit: 2 

在是意义上值匹配的另一种情况:

In [6]: a1.const=None;a1.default=None 
In [7]: parser.parse_args(['--foo', '--bar']) 
Out[7]: Namespace(bar=False, foo=None) 

对于小数字:

In [8]: a1.const=3;a1.default=3 
In [9]: parser.parse_args(['--foo', '--bar']) 
Out[9]: Namespace(bar=False, foo=3) 

但不是大的:

In [10]: a1.const=300;a1.default=300 
In [11]: parser.parse_args(['--foo', '--bar']) 
usage: ipython3 [-h] [--foo [FOO] | --bar] 
ipython3: error: argument --bar: not allowed with argument --foo 
An exception has occurred, use %tb to see the full traceback. 
SystemExit: 2 

字符串可能很棘手。代码中的文字是唯一的,但通过拆分创建的文字不是:

In [12]: a1.default='test' 
In [14]: parser.parse_args(['--foo', 'test', '--bar']) 
Out[14]: Namespace(bar=False, foo='test')   # no error 

这更像是命令行提供字符串的方式:

In [16]: parser.parse_args('--foo test --bar'.split()) 
usage: ipython3 [-h] [--foo [FOO] | --bar] 
ipython3: error: argument --bar: not allowed with argument --foo 
An exception has occurred, use %tb to see the full traceback. 
SystemExit: 2