使用argparse隐藏选定的子命令 [英] Hiding selected subcommands using argparse

查看:62
本文介绍了使用argparse隐藏选定的子命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用argparse并在我的程序中设置了子命令.我创建了子解析器来定义这些子命令.我有一些管理命令,不应在帮助屏幕上显示给用户.我知道我们可以隐藏子命令的参数,但是我不知道如何隐藏一些子命令以免出现在帮助列表中.

I am using argparse and have set-up subcommands to my program. I have created sub-parsers to define these sub-commands. I have some admin commands that shouldn't be shown to users in the help screen. I know we could hide arguments of a sub-command, but I don't know how we could hide few of the subcommands from showing up in the help list.

这是我的代码段,

parser = argparse.ArgumentParser(prog='myProg',
                                    description=desc,
                                   formatter_class=argparse.RawDescriptionHelpFormatter)

subparsers = parser.add_subparsers(dest='sub_parser_name')


myProg_query.add_subparser(subparsers)
myProg_update.add_subparser(subparsers)
myProg_configure.add_subparser(subparsers)
myProg_result.add_subparser(subparsers)

运行help命令时,我得到了

When I run the help command, I get this

%> myProg --help
usage: myProg [-h] 

positional arguments:
{query,update,configure,result}
query               query information
update              Update 
configure           Configure system
result              tabulate the result

从帮助输出中,我只想向用户显示查询"和结果".我尝试在add_subparser方法中使用argparse.SUPPRESS,但是它将隐藏所有子命令.我搜索的内容仅涉及隐藏每个子命令的单个参数,而不涉及隐藏子命令.我可能必须创建一个自定义格式化程序方法,但想检查是否还有其他方法可以实现此目的.

From the help output, I would want to display only "query" and "result" to the user. I tried to use argparse.SUPPRESS in add_subparser method, but it would hide all the subcommands. Whatever I searched only talked about hiding individual arguments of each sub-command, but not about hiding sub-command. I might have to create a custom formatter method, but wanted to check if there were any other way to achieve this.

推荐答案

metavar可能会解决问题:

import argparse
parser = argparse.ArgumentParser()
sp = parser.add_subparsers(metavar='{cmd1,cmd2}')
sp1 = sp.add_parser('cmd1')
sp2 = sp.add_parser('cmd2')
sp3 = sp.add_parser('cmd3')
parser.parse_args()

使用此cmd3不会出现在用法或帮助中.但是它确实出现在错误消息中

With this cmd3 does not appear in the usage or help. But it does appear in the error message

错误:参数{cmd1,cmd2}:无效选择:"cmd"(从"cmd1","cmd2","cmd3"中选择)

error: argument {cmd1,cmd2}: invalid choice: 'cmd' (choose from 'cmd1', 'cmd2', 'cmd3')


您可能已经发现help=SUPPRESS的这种用法.但这需要自定义用法(可能还有描述)参数:


You may have already discovered this use of help=SUPPRESS. But it requires a custom usage (and possibly description) parameters:

import argparse
parser = argparse.ArgumentParser(usage='%(prog)s [-h] {cmd1,cmd2}')
sp = parser.add_subparsers(help=argparse.SUPPRESS)
sp1 = sp.add_parser('cmd1')
sp2 = sp.add_parser('cmd2')
sp3 = sp.add_parser('cmd3')
parser.parse_args()

对于主解析器,子解析器类似于位置参数的choices.尽我所能告诉我们,没有一种方法可以选择性地抑制choices.

To the main parser, subparsers look like choices of a positional argument. As best I can tell there isn't a way of selectively suppressing choices.

有了这个问题,检查argparse.py代码本身可能比文档提供更多帮助.在这种情况下,我查看了class _SubParsersAction(Action)的代码.如果要自定义格式化程序,那将成倍增加.现有的替代格式化程序仅修改了埋在该类深处的一种或两种方法.

With this level of question, examining the argparse.py code itself can be more help than the docs. In this case I looked at the code for class _SubParsersAction(Action). That's doubly true if you want to customize the formatter. The existing alternative formatters modify just one or two methods buried deep in the class.

此问题已作为错误问题提出, http://bugs.python.org/issue22848.

This issue has been raised as a bug issue, http://bugs.python.org/issue22848.

有一个修补程序,该修补程序将基于help=SUPPRESS修改各个子分析器的选择显示.但是,我至少现在建议使用metavar解决方案.还有其他提议的补丁可用于处理choices.

There is a patch that would modify the choices display based on help=SUPPRESS for individual subparsers. But I'm recommending the metavar solution, at least for now. There are other proposed patches for dealing with choices.

这篇关于使用argparse隐藏选定的子命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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