本文介绍了不要在argparse的print_help()中两次显示长选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

哪个给我这样的输出:

%./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

我非常想知道如何对具有两个版本(即长选项)的命令进行分组",每个版本还显示一个metavar.

I would very much like to know how I can 'group' commands that have two versions (ie. long options) which each also show a metavar.

这在我这主要是一个美学问题,但我仍然想解决此问题.我一直在阅读互联网上的手册和文本,但要么信息不存在,要么我在这里完全没什么东西:)

This is mostly an aesthetic issue on my side, but I would still like to fix this. I have been reading manuals and texts on the internet, but either the information just isn't there or I am totally missing something here :)

推荐答案

使用自定义描述的另一种解决方案

如果设置了metavar='',则帮助行将变为:

Another solution, using custom descriptions

if you set the metavar='', the help line becomes:

-q , --queue          Show information for <queue>


在这里,我取消常规帮助行,并用组的描述行替换它们:


Here I suppress the regular help lines, and replace them with the description lines for a group:

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


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
  -v, --version  show program's version number and exit

information options:
  -q, --queue <queue>     Show information for <queue>
  -d, --domain <domain>   Show information about a specific <domain>

或者您可以将该信息放入常规描述中.您已经在使用自定义用法行.

Or you could put that information in the regular description. You already are using a custom usage line.

这篇关于不要在argparse的print_help()中两次显示长选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 17:50