使用 argparse 隐藏选定的子命令 [英] Hiding selected subcommands using 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)
当我运行帮助命令时,我得到了这个
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屋!