Subparser -- 显示解析器和子解析器的帮助 [英] Subparser -- show help for both parser and subparser

查看:23
本文介绍了Subparser -- 显示解析器和子解析器的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了几十个类似的 SO 问题,但还没有找到合适的解决方案,所以如果出现重复,请原谅我.我有一个类似于这个

我想要一个解析器+子解析器对,如果子解析器被激活",则使用 --help 选项会导致显示两者的帮助.

我能够获得完整(解析器 + 子解析器)帮助的唯一方法是:

common_parser = argparse.ArgumentParser(add_help=False)common_parser.add_argument('-c', required = True)解析器 = argparse.ArgumentParser(parents=[common_parser])subparsers = parser.add_subparsers(dest="sub")subparser = subparsers.add_parser("sub_option", parents=[common_parser])subparser.add_argument('-o', required = False)设置 = parser.parse_args()

但是脚本需要输入两次 -c 选项(显然是针对解析器和子解析器).如果我不使用 parents 那么我会得到正常的行为,但我不会为 subparser 获得的帮助只包含 -o 描述(我也想要 -c 显示)

附言Python 2.7

解决方案

首先让我们区分解析行为和帮助显示.

我假设您有多个子解析器(否则为什么要使用子解析器机制).-o 是特定于 sub_option 的参数,大概其他子解析器有自己的参数.

-c 的目的是什么?它是所有子解析器共有的东西吗?由于需要子解析器,因此讨论仅对主解析器重要的参数没有意义.

处理公共参数的一种方法是为所有子解析器定义它.您使用的父母机制可以为您节省一些打字时间.只需从主要的 parser 定义中省略它.这样就解决了必须提供两次的问题.

如果为所有子解析器定义了 -c,那么就不需要在主解析器帮助中显示它,是吗?

当子解析器命令是第一个参数字符串时,整个子解析器机制是最干净的,它的所有参数、位置和选项都在后面.可以为主解析器定义参数,但这通常会使使用和帮助变得复杂.

显示主解析器和(所有)子解析器的帮助问题之前已经出现过,无论是在 SO 上,还是在 python 错误/问题上.没有简单的解决方案.一些可能有帮助的工具是:

  • 使用 parser.print_help()subparse.print_help() 在程序控制下生成帮助.

  • add_subparsers() 命令采用 progtitledescription 等参数来控制帮助,包括子解析器帮助的用法.

  • add_subparser() 采用与 ArgumentParser 相同类型的参数(因为它定义了一个解析器),descriptionusage 可以有用.

<小时>

看上一个问题

如何在 argparse 中显示所有子解析器的帮助?

我意识到如果 -c 不是必需的,它可以为 main 和 subparser 定义,并在帮助中按预期显示.但是通过使其成为必需",两个解析器都必须看到它 - 但只有子解析器看到的值出现在命名空间中.

主解析器中定义的位置也出现在子解析器用法中.

另一个子解析器帮助显示问题

argparse 子解析器整体帮助输出

<小时>

http://bugs.python.org/issue20333 argparse 子解析器使用消息隐藏主要解析器用法 讨论了在子解析用法行中应显示多少主要解析器用法的问题.目前只显示位置(在子解析器之前定义).在补丁中,我建议也添加必需的选项.但是你总是可以通过为子解析器定义你自己的 prog 来捏造这一点.

I've looked through dozens of similar SO questions but haven't find suitable solutions so please forgive me in case of a dublicate. I have a problem similar to this one

I want to have a parser+subparser pair, with --help option causing help being shown for the both if subparser is "activated".

The only way I was able to get full (parser + subparser) help is:

common_parser = argparse.ArgumentParser(add_help=False)
common_parser.add_argument('-c', required = True)
parser = argparse.ArgumentParser(parents=[common_parser])
subparsers = parser.add_subparsers(dest="sub")
subparser = subparsers.add_parser("sub_option", parents=[common_parser])
subparser.add_argument('-o', required = False)
settings = parser.parse_args()

But then script requires the -c option to be entered twice (apparently for parser and subparser). If I don't use parents then I get normal behaviour but I don't the help I get for subparser contains only -o description (I want also -c to be shown)

P.S. Python 2.7

解决方案

For a start let's distinguish between parsing behavior and help display.

I assume you have multiple subparsers (otherwise why use the subparser mechanism). -o is an argument specific to sub_option, and presumably the other subparsers have their own arguments.

What is the purpose of -c? Is it something that is common to all subparsers? Since subparsers are required, it doesn't make sense to talk about an argument that only matters to the main parser.

One way to deal with a common argument is to define it for all subparsers. The parents mechanism that you use saves you a bit of typing. Just omit it from the main parser definition. That gets rid of the problem with having to supply it twice.

If -c is defined for all the subparsers, then there isn't a need to show it in the main parser help, is there?

The whole subparser mechanism is cleanest when the subparser command is the 1st argument string, with all of its arguments, positionals and options following. It's possible to define arguments for the main parser, but it often complicates both use and help.

The issue of display the help for both the main parser and (all) the subparsers has come up before, both on SO, and on the python bug/issues. There isn't a simple solution. Some tools that may help are:

  • generate help under program control with parser.print_help(), and subparse.print_help().

  • add_subparsers() command takes parameters like prog, title and description which can be used to control the help, including the usage of the subparser help.

  • add_subparser() takes the same sort of parameters as ArgumentParser (since it defines a parser), description and usage may be useful.


Looking a previous question

How to show help for all subparsers in argparse?

I realized that if -c is not required, it can be defined for both the main and subparser, and appear as expected in the helps. But by making it 'required', both parsers have to see it - but only the value seen by the subparser appears in the namespace.

Also positionals defined in the main parser appear in the subparser usage.

Another subparsers help display question

argparse subparser monolithic help output


http://bugs.python.org/issue20333 argparse subparser usage message hides main parser usage discusses the question of how much of main parser usage should show up in the subparse usage line. Currently just positionals (defined before the subparsers) show up. In the patch I suggest adding required optionals as well. But you can always fudge this by defining your own prog for subparsers.

这篇关于Subparser -- 显示解析器和子解析器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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