显示带有argparse子栏的命令子组

2024-10-02 16:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在用Python开发一个程序,其中包含大约40个子命令。解析器是使用argparse完成的。随着子命令数量的增加,搜索感兴趣的命令变得越来越复杂。目前显示如下。你知道吗

$ pgrm -h
  Usage: pgrm [-h] [-v]  ...

  Blabla bla.

Main command arguments:
 -h, --help          show this help message and exit
 -v, --version       show program's version number and exit

Available sub-commands:

  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…


Usage example:
 'pgrm sub-command -h' for more information.

我想更改显示以显示类别(例如更新/插入/选择)和关联的子命令。你知道吗

$ pgrm -h
  Usage: pgrm [-h] [-v]  ...

  Blabla bla.

Main command arguments:
 -h, --help          show this help message and exit
 -v, --version       show program's version number and exit

Available sub-commands:

Updating
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
Selecting
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
Inserting
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…
  sub_cmd        Some description about the command…  

在argparser中有没有什么解决方案可以用来实现这样的CLI?你知道吗

谢谢


Tags: andthe命令cmdversionshowexithelp
1条回答
网友
1楼 · 发布于 2024-10-02 16:33:13

我几年前就探讨过这个问题

http://bugs.python.org/issue9341 允许对argparse子命令进行分组

如果我正确地阅读了建议的修补程序,它只需要更改_SubParsersAction类,而不需要进一步更改HelpFormatterArgumentParser类。你知道吗

相关问题 更多 >