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

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

问题描述

我已经研究了数十个类似的SO问题,但没有找到合适的解决方案,因此,如果出现重复,请原谅我. 我遇到的问题类似于这一个

我想拥有一个解析器+子解析器对,使用--help选项可以在子解析器被激活"的情况下为两者显示帮助.

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

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()

但是随后脚本要求两次输入-c选项(对于解析器和子解析器而言).如果我不使用parents,那么我会得到正常的行为,但是我得到的关于子解析器的帮助仅包含-o描述(我也希望显示-c)

P.S. Python 2.7

解决方案

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

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

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

处理通用参数的一种方法是为所有子解析器定义它.您使用的父代机制可以节省一些打字时间.只需从主parser定义中忽略它即可.这样就不必再提供两次了.

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

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

以前在SO和python bug/issues上都显示了对主解析器和(所有)子解析器的帮助的问题.没有一个简单的解决方案.一些可能有用的工具是:

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

  • add_subparsers()命令使用诸如progtitledescription之类的参数,可用于控制帮助,包括子解析器帮助的usage.

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


寻找上一个问题

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

我意识到,如果不需要-c,则可以为主解析器和子解析器都定义它,并在帮助中按预期显示.但是通过将其设置为必需",两个解析器都必须看到它-但是只有子解析器看到的值才会出现在名称空间中.

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

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

argparse子解析器整体帮助输出


http://bugs.python.org/issue20333 argparse subparser usage message hides main parser usage讨论了如何主解析器的大部分用法应显示在次解析用法行中.当前仅显示位置(在子解析器之前定义).在补丁程序中,我建议也添加必需的可选内容.但是,您始终可以通过为子解析器定义自己的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.

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

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