第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何處理子組幫助消息

如何處理子組幫助消息

動漫人物 2021-11-23 19:14:35
我試圖了解有關(guān) Click 的一些實現(xiàn)細節(jié)。我有以下示例代碼:#cli.pyimport click@click.group()def cli():    pass@cli.group()def show():    """ Define the environment of the product """    pass@show.command()def name():    click.echo("run show name command")@show.command()def height():    click.echo("run show height command")if __name__ == "__main__":    cli()與此代碼,name并且height是子命令的的show基團。但是,從語義上講,這并沒有什么意義。他們越是這樣的論點一個“show”命令的。我知道我可以有一個帶有“屬性”參數(shù)的命令,我可以根據(jù)“屬性”的字符串值從中調(diào)用不同的函數(shù)。但是,我覺得一旦“屬性”有幾種可能性,維護起來會很乏味。我相信如果我能夠編輯幫助消息,我仍然可以使用上述結(jié)構(gòu)。當我運行時cli.py show --help,我會收到命令組的默認幫助消息:Usage: cli.py show [OPTIONS] COMMAND [ARGS]...  Define the environment of the productOptions:  --help  Show this message and exit.Commands:  height  name有沒有辦法編輯幫助消息以將“命令”更改為“參數(shù)”?我知道如何更改 click.group() 裝飾器中的使用聲明,但我不確定如何修改幫助消息本身。我想實現(xiàn)的幫助信息如下:Usage: cli.py show [OPTIONS] ARG  Define the environment of the productOptions:  --help  Show this message and exit.Arguments:  height  name這樣的事情可能嗎?
查看完整描述

1 回答

?
楊__羊羊

TA貢獻1943條經(jīng)驗 獲得超7個贊

您可以通過使用組的自定義類來更改子命令 help 給出的消息??梢酝ㄟ^繼承click.Group和更改format_commands()方法來創(chuàng)建自定義類,例如:


自定義類:

class HelpAsArgs(click.Group):

    # change the section head of sub commands to "Arguments"


    def format_commands(self, ctx, formatter):

        rows = []

        for subcommand in self.list_commands(ctx):

            cmd = self.get_command(ctx, subcommand)

            if cmd is None:

                continue


            help = cmd.short_help or ''

            rows.append((subcommand, help))


        if rows:

            with formatter.section('Arguments'):

                formatter.write_dl(rows)

測試代碼:

import click


@click.group()

def cli():

    pass


@cli.group(cls=HelpAsArgs)

def show():

    """ Define the environment of the product """

    pass


@show.command()

def name():

    click.echo("run show name command")


@show.command()

def height():

    click.echo("run show height command")


if __name__ == "__main__":

    commands = (

        'show',

        'show --help',

        '--help',

    )


    import sys, time


    time.sleep(1)

    print('Click Version: {}'.format(click.__version__))

    print('Python Version: {}'.format(sys.version))

    for command in commands:

        try:

            time.sleep(0.1)

            print('-----------')

            print('> ' + command)

            time.sleep(0.1)

            cli(command.split())


        except BaseException as exc:

            if str(exc) != '0' and \

                    not isinstance(exc, (click.ClickException, SystemExit)):

                raise

結(jié)果:

Click Version: 6.7

Python Version: 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]

-----------

> show

Usage: test.py show [OPTIONS] COMMAND [ARGS]...


  Define the environment of the product


Options:

  --help  Show this message and exit.


Arguments:

  height

  name

-----------

> show --help

Usage: test.py show [OPTIONS] COMMAND [ARGS]...


  Define the environment of the product


Options:

  --help  Show this message and exit.


Arguments:

  height

  name

-----------

> --help

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


Options:

  --help  Show this message and exit.


Commands:

  show  Define the environment of the product


查看完整回答
反對 回復 2021-11-23
  • 1 回答
  • 0 關(guān)注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號