子命令中的选项的argparse冲突解析器将关键字参数转换为位置参数

too*_*der 6 python argparse

我有一个Python脚本,它运行两个接受相同选项的子命令--config.我想创建一个第三个子命令,可以按顺序一起运行前两个子命令.

使用argparse,我为每个子命令创建了一个subparser,以及第三个subparser,其父项是两个子命令.只是为了澄清:

subcommand1 = subparsers.add_parser('subcommand1')
subcommand1.add_argument('--config', help="The config")

subcommand2 = subparsers.add_parser('subcommand2')
subcommand2.add_argument('--config', help="The config")

wrappercommand = subparsers.add_parser('wrappercommand', 
                                       parents=[subcommand1, subcommand2], 
                                       conflict_handler='resolve')
Run Code Online (Sandbox Code Playgroud)

当我运行wrappercommand或subcommand2时,一切正常.但是,subcommand1中断,以此为输出:

$ run_command.py subcommand1 --config path_to_config.ini

usage: run_command.py subcommand1 config 

optional arguments:
  help                  show this help message and exit
  config                The config
Run Code Online (Sandbox Code Playgroud)

看起来argparse已将关键字arg(" - config")转换为位置关键字("config").这是argparse解决冲突选项时的预期行为吗?

hpa*_*ulj 4

我认为你正在将这个冲突处理者推入意想不到和未经测试的领域。通常parents是不被使用的独立解析器。它们只是 的来源Actions。并-h用 处理有关的冲突add_help=False

作为背景:使用默认值conflict_handler(错误),您在创建子解析器时会收到错误消息wrappercommand

argparse.ArgumentError: argument -h/--help: conflicting option string(s): -h, --help
Run Code Online (Sandbox Code Playgroud)

添加一些后add_help=False,您仍然会得到:

argparse.ArgumentError: argument --config: conflicting option string(s): --config
Run Code Online (Sandbox Code Playgroud)

冲突处理程序resolve用某种“解决方案”替换错误消息。下面的脚本演示了正在发生的情况。

处理程序resolve删除了option_stringsactions 的subcommand1,同时将 actions 保留在原处。实际上,它将两者都变成了位置。并且自从helphas以来nargs=0,它总是运行。因此,帮助显示。

的目的_handle_conflict_resolve是删除第一个参数的证据,以便可以添加新的参数。add_argument当两个具有相同选项字符串的命令产生冲突时,这种方法效果很好。但这里的冲突是由两位父母的“模仿”行为产生的。但是父操作是通过引用复制的,因此“子”中的更改最终会影响“父”。

一些可能的解决方案:

  • 直接添加参数wrappercommand。这种parents机制只是将父母的论点添加到孩子身上。它不会按顺序“运行”父母。

  • 编写自己的_handle_conflict_...函数来正确解决冲突。

  • 消除冲突,以便您可以parents在不使用resolve处理程序的情况下使用。


我已使用此示例提交了错误报告 http://bugs.python.org/issue22401

parent1 = argparse.ArgumentParser(add_help=False)
parent1.add_argument('--config')
parent2 = argparse.ArgumentParser(add_help=False)
parent2.add_argument('--config')

parser = argparse.ArgumentParser(parents=[parent1,parent2],
    conflict_handler='resolve')

def foo(parser):
    print [(id(a), a.dest, a.option_strings) for a in parser._actions]

foo(parent1)
foo(parent2)
foo(parser)
Run Code Online (Sandbox Code Playgroud)

其产生:

[(3077384012L, 'config', [])]
[(3076863628L, 'config', ['--config'])]
[(3076864428L, 'help', ['-h', '--help']), (3076863628L, 'config', ['--config'])]
Run Code Online (Sandbox Code Playgroud)

请注意,缺少option_strings,并且另一个 2. 的 parent1匹配不能再次使用,无论是作为父级还是解析器。idparent1


argparse - 组合父解析器、子解析器和默认值 是另一种情况,通过引用复制父解析器的操作会产生复杂性(在更改默认值时)。