是否可以在 argparse 子解析器中继承所需的选项? [英] Is it possible to inherit required options in argparse subparsers?

查看:18
本文介绍了是否可以在 argparse 子解析器中继承所需的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个具有多种运行模式的命令行应用程序(类似于具有 clonepullgit, 等等.).我的每个子命令都有自己的选项,但我也希望它们共享一组必需的选项,因此我尝试使用父解析器来实现这一点.但是,似乎继承一个必需的选项会导致子解析器不断请求它.这是重新创建行为的示例:

I am trying to write a command line application that has several modes in which it can run (similar to git having clone, pull, etc.). Each of my subcommands have their own options, but I also wanted them to share a set of required options, so I tried using a parent parser to implement this. However, it seems that inheriting a required option is causing the subparser to keep asking for it. Here is an example recreating the behavior:

import argparse

parent_parser = argparse.ArgumentParser(description="The parent parser")

parent_parser.add_argument("-p", type=int, required=True,
                           help="set the parent required parameter")

subparsers = parent_parser.add_subparsers(title="actions", required=True, dest='command')
parser_command1 = subparsers.add_parser("command1", parents=[parent_parser],
                                      add_help=False,
                                      description="The command1 parser",
                                      help="Do command1")
parser_command1.add_argument("--option1", help="Run with option1")

parser_command2 = subparsers.add_parser("command2", parents=[parent_parser],
                                      add_help=False,
                                      description="The command2 parser",
                                      help="Do command2")

args = parent_parser.parse_args()

所以现在如果我运行 python test.py 我得到:

So now if I run python test.py I get:

usage: test.py [-h] -p P {command1,command2} ...
test.py: error: the following arguments are required: -p, command

好的,到目前为止一切顺利.然后,如果我尝试使用 python test.py -p 3 仅指定 -p 选项,我会得到:

Ok, so far so good. Then if I try to specify just the -p option with python test.py -p 3 I get:

usage: test.py [-h] -p P {command1,command2} ...
test.py: error: the following arguments are required: command

但是如果我运行 python test.py -p 3 command1我得到:

But then if I run python test.py -p 3 command1 I get:

usage: test.py command1 [-h] -p P [--option1 OPTION1] {command1,command2} ...
test.py command1: error: the following arguments are required: -p, command

如果我添加另一个 -p 3 它仍然要求再次指定 command,然后如果我再次添加它会要求另一个 -p 3

If I add another -p 3 it still asks to specify command again, and then if I add that again it asks for another -p 3 etc.

如果我不需要 -p 选项,问题就解决了,但是有没有办法在多个子解析器之间共享所需的选项,而无需在每个子解析器中复制粘贴它们?还是我完全用错了方法?

If I don't make the -p option required the problem is fixed, but is there a way to share required options among multiple subparsers without just copy pasting them within each subparser? Or am I going about this entirely the wrong way?

推荐答案

分离主解析器和父解析器功能:

Separate the main parser and parent parser functionality:

import argparse

parent_parser = argparse.ArgumentParser(description="The parent parser", add_help=False)

parent_parser.add_argument("-p", type=int, required=True,
                           help="set the parent required parameter")


main_parser = argparse.ArgumentParser()
subparsers = main_parser.add_subparsers(title="actions", required=True, dest='command')
parser_command1 = subparsers.add_parser("command1", parents=[parent_parser],
                                      description="The command1 parser",
                                      help="Do command1")
parser_command1.add_argument("--option1", help="Run with option1")

parser_command2 = subparsers.add_parser("command2", parents=[parent_parser],
                                      description="The command2 parser",
                                      help="Do command2")

args = main_parser.parse_args()

主解析器和子解析器都写入相同的 args 命名空间.如果两者都定义了一个 'p' 参数,则子解析器的值(或默认值)将覆盖由 main 设置的任何值.因此,可以在两者中使用相同的dest",但这通常不是一个好主意.每个解析器都必须满足自己的必需"规范.没有共享".

The main parser and subparser both write to the same args namespace. If both define a 'p' argument, the subparser's value (or default) will overwrite any value set by the main. So it's possible to use the same 'dest' in both, but it is generally not a good idea. And each parser has to meet its own 'required' specifications. There's no 'sharing'.

parents 机制将参数从父级复制到子级.所以它可以节省打字或复制粘贴,但其他的很少.当父项在别处定义并导入时,它最有用.实际上它是通过引用复制的,这有时会引起问题.一般来说,同时运行父进程和子进程并不是一个好主意.使用虚拟"父级.

The parents mechanism copies arguments from the parent to the child. So it saves typing or copy-n-paste, but little else. It's most useful when the parent is defined elsewhere and imported. Actually it copies by reference, which sometimes raises problems. In general it isn't a good idea to run both the parent and child. Use a 'dummy' parent.

这篇关于是否可以在 argparse 子解析器中继承所需的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆