用Python分类帮助输出请点击 [英] Categorize help output in Python Click

查看:75
本文介绍了用Python分类帮助输出请点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在Click中对命令进行分类,使其类似于kubectl在分离命令时所使用的结构.

I am trying to figure out how to categorize commands in Click to resemble something close to the structure that kubectl uses in the way it separate commands out.

例如,在普通的Click帮助输出中,我们有:

For instance, in a vanilla Click help output we have:

Usage: cli.py [OPTIONS] COMMAND [ARGS]...

  A CLI tool

Options:
  -h, --help  Show this message and exit.

Commands:
  command1   This is command1
  command2   This is command2
  command3   This is command3
  command4   This is command4

相反,最适合我的用法是进行分隔以更好地对命令结构进行分类.

Instead, what would be ideal for my usage is to have a separation to better categorize the command structure.

例如:

Usage: cli.py [OPTIONS] COMMAND [ARGS]...

  A CLI tool

Options:
  -h, --help  Show this message and exit.

Specific Commands for X:

  command1   This is command1
  command2   This is command2

Specific Commands for Y:

  command3   This is command3
  command4   This is command4

Global Commands:

  version    Shows version

为此,我还使用了最新的Python和Click的最新版本.

I am using the latest Python and latest version of Click also for this.

我曾尝试研究各种Click类,以更改此行为,但这样做没有成功. 我最近得到的是能够根据优先级来构建命令,但是我无法如上例中那样将它们逻辑上分开.

I have tried looking into hooking into various Click classes to change this behaviour but have been unsuccessful in doing so. Closest I have gotten is being able to structure commands based on priority but I am not able to logically separate them out as in the example above.

任何帮助将不胜感激.

推荐答案

我已经通过创建自己的click.Group:

I've achieved this by creating my own click.Group:

class OrderedGroup(click.Group):
    def __init__(self, name=None, commands=None, **attrs):
        super(OrderedGroup, self).__init__(name, commands, **attrs)
        self.commands = commands or collections.OrderedDict()

    def list_commands(self, ctx):
        return self.commands

    def format_commands(self, ctx, formatter):
        super().get_usage(ctx)

        formatter.write_paragraph()
        with formatter.section("Specific Commands for X:"):
            formatter.write_text(
                f'{self.commands.get("command1").name}\t\t{self.commands.get("command1").get_short_help_str()}')
            formatter.write_text(
                f"{self.commands.get('command2').name}\t\t{self.commands.get('command2').get_short_help_str()}")

        with formatter.section("Specific Commands for Y:"):
            formatter.write_text(
                f'{self.commands.get("command3").name}\t\t{self.commands.get("command3").get_short_help_str()}')
            formatter.write_text(
                f'{self.commands.get("command4").name}\t\t{self.commands.get("command4").get_short_help_str()}')

        with formatter.section("Global Commands"):
            formatter.write_text(
                f'{self.commands.get("version").name}\t\t{self.commands.get("version").get_short_help_str()}')

并这样创建cli组:

@click.group(cls=OrderedGroup)
def cli():
    pass

有帮助吗?

这篇关于用Python分类帮助输出请点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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