argparse子解析器--help输出不显示子解析器的描述 [英] argparse subparser --help output doesn't show the subparser's description

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

问题描述

如果我创建具有特定帮助字符串的子解析器,则当用户运行myprog command --help时不会显示此字符串:

If I create a subparser with a specific help string, this string is not displayed when the user runs myprog command --help:

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help="sub-command help")

parser_command = subparsers.add_parser("command", help="Issue a command")
parser.parse_args()

顶级帮助显示此command子命令,并在其旁边带有发出命令"的描述:

The top-level help shows this command subcommand with the description "Issue a command" alongside:

$ python prog.py --help
usage: prog.py [-h] {command} ...

positional arguments:
  {command}   sub-command help
    command   Issue a command

optional arguments:
  -h, --help  show this help message and exit

但是,子命令的帮助未显示此描述:

However the subcommand's help doesn't show this description:

$ python prog.py command --help
usage: prog.py command [-h]

optional arguments:
  -h, --help  show this help message and exit

我希望得到子命令的帮助,以打印出该子命令的实际用途. IE.我希望在python prog.py command --help的输出中的某处看到文本发出命令".

What I would expect is for the subcommand's help to print out what the subcommand is actually for. I.e. I expected to see the text "Issue a command" somewhere in the output to python prog.py command --help.

是否可以在子命令的帮助输出中包含此文本?是否有另一个subparser属性可用于提供子命令的描述?

Is there a way to include this text in the subcommand's help output? Is there another subparser attribute that can be used to provide a description of the subcommand?

推荐答案

add_parser方法接受(大多数)ArgumentParser构造函数执行的参数.

The add_parser method accepts (most) of the parameters that a ArgumentParser constructor does.

https://docs.python.org/3/library/argparse.html#sub-commands

很容易在add_subparsers段落中忽略此句子:

It's easy to overlook this sentence in the add_subparsers paragraph:

此对象具有单个方法add_parser(),该方法采用命令名称和任何ArgumentParser构造函数参数,并返回可以照常修改的ArgumentParser对象.

This object has a single method, add_parser(), which takes a command name and any ArgumentParser constructor arguments, and returns an ArgumentParser object that can be modified as usual.

In [93]: parser=argparse.ArgumentParser()

In [94]: sp = parser.add_subparsers(dest='cmd',description='subparses description')

In [95]: p1 = sp.add_parser('foo',help='foo help', description='subparser description')
In [96]: p1.add_argument('--bar');

主解析器帮助:

In [97]: parser.parse_args('-h'.split())
usage: ipython3 [-h] {foo} ...

optional arguments:
  -h, --help  show this help message and exit

subcommands:
  subparses description

  {foo}
    foo       foo help
...

有关子解析器的帮助:

In [98]: parser.parse_args('foo -h'.split())
usage: ipython3 foo [-h] [--bar BAR]

subparser description

optional arguments:
  -h, --help  show this help message and exit
  --bar BAR
...

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

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