Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用argparse更改subparser的格式_Python_Python 3.x_Argparse - Fatal编程技术网

Python 如何使用argparse更改subparser的格式

Python 如何使用argparse更改subparser的格式,python,python-3.x,argparse,Python,Python 3.x,Argparse,我知道使用构造函数创建的主解析器有max_help_位置,但是如何更改子解析器格式,因为我有时有很长的命令名,我希望帮助消息与之对齐 #!/usr/bin/env python import argparse import subprocess parser = argparse.ArgumentParser(description="example", formatter_class=argparse.RawDescript

我知道使用构造函数创建的主解析器有max_help_位置,但是如何更改子解析器格式,因为我有时有很长的命令名,我希望帮助消息与之对齐

#!/usr/bin/env python

import argparse
import subprocess

parser = argparse.ArgumentParser(description="example",
                                 formatter_class=argparse.RawDescriptionHelpFormatter)

subparsers = parser.add_subparsers(title="available commands",metavar="command [options ...]")

parser1 = subparsers.add_parser('short-cmd', help='help message')
parser1.add_argument('--path')

parser2 = subparsers.add_parser('very-long-long-name-for-a-command', help='help message')
parser2.add_argument('--path')

args = parser.parse_args()
输出:

usage: test.py [-h] command [options ...] ...

example

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

available commands:
  command [options ...]
    short-cmd           create a directory for database
    very-long-long-name-for-a-command
                        create a directory for database
我想要的是:

usage: test.py [-h] command [options ...] ...

example

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

available commands:
  command [options ...]
    short-cmd                          create a directory for database
    very-long-long-name-for-a-command  create a directory for database

您正确地注意到有一个
max\u help\u位置
控制
help
行的缩进。
帮助格式化程序中的默认值为
24

但关键是要改变这一点

一种方法是子类化
HelpFormatter
(如果需要,也可以使用原始的…)

理论上,您可以使用以下工具创建或修改
格式化程序

formatter = argparse.HelpFormatter(`prog`, max_help_position=40)

但要做到这一点,您必须调整此调用树中的某些位置:

`-h`
parser.print_help()
    parser.format_help()
        parser._get_formatter()
            parser.formatter_class(prog=self.prog)

(如果您认为我删除了一些有用的内容,请查看我的编辑历史记录)。

您的解释有效,但您知道为什么它在35处停止,而不在后面(即使是120或任何大的数字)?实际的帮助位置是
min(self.\u action\u max\u length+2,self.\u max\u help\u position)
。我猜33是你最长的
动作的长度。
formatter = argparse.HelpFormatter('prog')
formatter._max_help_position = 45
`-h`
parser.print_help()
    parser.format_help()
        parser._get_formatter()
            parser.formatter_class(prog=self.prog)