Python Don';在argparse的print_help()中不显示两次长选项
我有以下代码:Python Don';在argparse的print_help()中不显示两次长选项,python,argparse,Python,Argparse,我有以下代码: parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool', prog='pqa', usage='%(prog)s [-h] [-v,--version]') parser.add_argument('-l', '--list', action='store_true', help='Shows full overview of
parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool',
prog='pqa',
usage='%(prog)s [-h] [-v,--version]')
parser.add_argument('-l', '--list', action='store_true',
help='Shows full overview of all queues')
parser.add_argument('-q', '--queue', action='store', metavar='<queue>', dest='queue',
help='Show information for <queue>')
parser.add_argument('-d', '--domain', action='store', metavar='<domain>', dest='domain',
help='Show information about a specific <domain>')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
args = parser.parse_args()
parser=argparse.ArgumentParser(description='Postfix队列管理工具',
prog='pqa',
用法='%(程序)s[-h][-v,--version]')
parser.add_参数('-l','-list',action='store_true',
help='显示所有队列的完整概览')
parser.add_参数('-q','-queue',action='store',metavar='',dest='queue',
help='Show information for')
parser.add_参数('-d','-domain',action='store',metavar='',dest='domain',
help='Show information on a specific')
parser.add_参数('-v','-version',action='version',version='%(prog)s0.1')
args=parser.parse_args()
这给了我这样的输出:
%./pqa
usage: pqa [-h] [-v,--version]
Postfix Queue Administration Tool
optional arguments:
-h, --help show this help message and exit
-l, --list Shows full overview of all queues
-q <queue>, --queue <queue>
Show information for <queue>
-d <domain>, --domain <domain>
Show information about a specific <domain>
-v, --version show program's version number and exit
%./pqa
用法:pqa[-h][-v,--version]
后缀队列管理工具
可选参数:
-h、 --帮助显示此帮助消息并退出
-l、 --列表显示所有队列的完整概览
-q,--队列
显示的信息
-d,--域
显示有关特定项目的信息
-v、 --版本显示程序的版本号并退出
我非常想知道如何对具有两个版本(即长选项)的命令进行“分组”,每个版本都显示一个metavar
对我来说,这主要是一个美学问题,但我仍然想解决这个问题。我一直在互联网上阅读手册和文本,但要么信息根本不存在,要么我完全遗漏了一些内容:)帮助热线中重复出现了
问题吗
-d <domain>, --domain <domain>
-d,--domain
argparse HelpFormatter不能让用户对显示的这一部分进行太多控制。如图所示,您可以设置用法行、帮助文本和metavar
您必须对HelpFormatter进行子类化,并更改其中一个函数以生成如下内容:
-d, --domain <domain>
-d,--domain
这看起来不像是一个复杂的更改,可能是对HelpFormatter.\u format\u action\u invocation
方法的更改。但您需要更明确地说明您想要什么。另一种解决方案,使用自定义描述
如果设置metavar='
,则帮助行变为:
-q , --queue Show information for <queue>
-q,--队列显示
在这里,我取消显示常规帮助行,并将其替换为组的描述行:
parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool',
prog='pqa',
usage='%(prog)s [-h] [-v,--version]',
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('-l', '--list', action='store_true',
help='Shows full overview of all queues')
g = parser.add_argument_group(title='information options',
description='''-q, --queue <queue> Show information for <queue>
-d, --domain <domain> Show information about a specific <domain>''')
g.add_argument('-q', '--queue', action='store', metavar='', dest='queue',
help=argparse.SUPPRESS)
g.add_argument('-d', '--domain', action='store', metavar='<domain>', dest='domain',
help=argparse.SUPPRESS)
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
parser.print_help()
parser=argparse.ArgumentParser(description='Postfix队列管理工具',
prog='pqa',
用法='%(程序)s[-h][-v,--version]',
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_参数('-l','-list',action='store_true',
help='显示所有队列的完整概览')
g=解析器。添加参数组(title='information options',
description=''-q,--队列显示的信息
-d、 --域显示有关特定“”的信息)
g、 添加参数('-q','-queue',action='store',metavar='',dest='queue',
help=argparse.SUPPRESS)
g、 添加参数('-d','-domain',action='store',metavar='',dest='domain',
help=argparse.SUPPRESS)
parser.add_参数('-v','-version',action='version',version='%(prog)s0.1')
parser.print_help()
用法:pqa[-h][-v,--version]
后缀队列管理工具
可选参数:
-h、 --帮助显示此帮助消息并退出
-l、 --列表显示所有队列的完整概览
-v、 --版本显示程序的版本号并退出
信息选项:
-q、 --队列显示的信息
-d、 --域显示有关特定域的信息
或者你可以把这些信息放在常规描述中。您已经在使用自定义用法行。将hpaulj的答案转换为实际代码,类似于这样:
class CustomHelpFormatter(argparse.HelpFormatter):
def _format_action_invocation(self, action):
if not action.option_strings or action.nargs == 0:
return super()._format_action_invocation(action)
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
return ', '.join(action.option_strings) + ' ' + args_string
fmt = lambda prog: CustomHelpFormatter(prog)
parser = argparse.ArgumentParser(formatter_class=fmt)
要进一步扩展帮助变量的默认列大小,请将构造函数添加到CustomHelpFormatter
:
def __init__(self, prog):
super().__init__(prog, max_help_position=40, width=80)
看到它在行动:
usage: bk set [-h] [-p] [-s r] [-f] [-c] [-b c] [-t x y] [-bs s] [-bc c]
[--crop x1 y1 x2 y2] [-g u r d l]
monitor [path]
positional arguments:
monitor monitor number
path input image path
optional arguments:
-h, --help show this help message and exit
-p, --preview previews the changes without applying them
-s, --scale r scales image by given factor
-f, --fit fits the image within monitor work area
-c, --cover makes the image cover whole monitor work area
-b, --background c selects background color
-t, --translate x y places the image at given position
-bs, --border-size s selects border width
-bc, --border-color c selects border size
--crop x1 y1 x2 y2 selects crop area
-g, --gap, --gaps u r d l keeps "border" around work area
实际上我没有想过创建一个空的metavar,但是您的第二个块实际上更有意义,是一个非常简洁的解决方案!虽然这也满足了我的要求,但我觉得hpaulj在公认答案中的代码更容易阅读,并且完全按照我在问题中提出的要求去做。不过,感谢您的额外阐述!:)接受的答案没有错,但我更喜欢这个,尽管我花了一点时间才知道我的默认python版本是2.7,而且这个版本不太好,当在3.4下运行时,它就像一个魅力。谢谢另外,如果您知道如何使用Python进行建模