【问题标题】:Display subgroups of commands with argparse subparser使用 argparse subparser 显示命令子组
【发布时间】:2017-11-01 16:27:18
【问题描述】:

我目前正在用 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?

谢谢

【问题讨论】:

    标签: python argparse subparsers


    【解决方案1】:

    几年前我探讨过这个问题

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

    如果我正确阅读了我提议的补丁,它只需要更改_SubParsersAction 类,而无需进一步更改HelpFormatterArgumentParser 类。

    【讨论】:

    • 非常感谢 hpaulj。我会尽快看看。
    • 工作就像一个魅力。非常感谢您的帮助。
    猜你喜欢
    • 2019-12-25
    • 1970-01-01
    • 2019-02-11
    • 2015-07-30
    • 2011-09-15
    • 2019-06-14
    • 2018-06-24
    • 2014-11-30
    相关资源
    最近更新 更多