Python Click: Error: Missing argument while calling the --help flag

Python Click: Error: Missing argument while calling the --help flag

这是我的代码:

@click.group()
@click.pass_context
@click.argument('CHALLENGE', type=int)
def challenge(ctx, challenge):
    ctx.obj = Challenge(challenge=challenge)

@click.group(invoke_without_command=True, cls=PhaseGroup)
@click.pass_obj
@click.argument('PHASE', type=int)
def phase(ctx, phase):
    # Something

challenge.add_command(phase)

这些命令应该一起工作。

cli challenge 1 phase 1

通过正确执行,它按预期工作。

但是当我使用 --help 或在阶段上定义任何其他标志时,它会抛出

cli challenge 1 phase 1 --help

It throws Error: Missing argument "PHASE".

我搜索了 SOF,发现 这是一位遇到同样问题的用户,但我不太明白答案。

我不能将参数设为可选,因为它是使 CLI 正常工作的关键部分。

您的代码面临的根本问题是您正试图像命令一样使用组。至少有两种方法可以改善这种情况:

使用命令:

终端实体通常是 click.Command 而不是 click.Group。如果将 phase 更改为 click.Command,帮助将按预期工作。

@challenge.command()
@click.argument('PHASE', type=int)
def phase(ctx, phase):
    ....    

与群组一起使用 no_args_is_help

如果将 no_args_is_help=True 传递给 click.Group 构造函数,帮助将如您所愿地显示。但是除非你有充分的理由,否则我会建议使用如上所示的 click.Command

@challenge.group(invoke_without_command=True, no_args_is_help=True)
@click.argument('PHASE', type=int)
def phase(ctx, phase):
    ....

旁注,您通常不需要 add_command():

一般不需要使用:

@click.command()
def my_command():
    ....
challenge.add_command(my_command)        

您可以改为:

@challenge.command()
def my_command():
    ....

测试代码:

import click

@click.group()
@click.pass_context
@click.argument('CHALLENGE', type=int)
def challenge(ctx, challenge):
    ctx.obj = challenge

@challenge.command()
@click.pass_obj
@click.argument('PHASE', type=int)
def phase1(ctx, phase):
    """Phase1 Command"""
    click.echo((phase))

@challenge.group(invoke_without_command=True, no_args_is_help=True)
@click.pass_obj
@click.argument('PHASE', type=int)
def phase2(ctx, phase):
    """Phase2 Group"""
    click.echo((phase))


if __name__ == "__main__":
    commands = (
        '--help',
        '1 phase1',
        '1 phase1 --help',
        '1 phase1 2 ',
        '1 phase1 2 --help',
        '1 phase2',
        '1 phase2 --help',
        '1 phase2 2 ',
        '1 phase2 2 --help',
        '--help',
    )

    import sys, time

    time.sleep(1)
    print('Click Version: {}'.format(click.__version__))
    print('Python Version: {}'.format(sys.version))
    for cmd in commands:
        try:
            time.sleep(0.1)
            print('-----------')
            print('> ' + cmd)
            time.sleep(0.1)
            challenge(cmd.split())

        except BaseException as exc:
            if str(exc) != '0' and \
                    not isinstance(exc, (click.ClickException, SystemExit)):
                raise

测试结果:

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)]
-----------
> --help
Usage: test.py [OPTIONS] CHALLENGE COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  phase1  Phase1 Command
  phase2  Phase2 Group
-----------
> 1 phase1
Usage: test.py phase1 [OPTIONS] PHASE

Error: Missing argument "PHASE".
-----------
> 1 phase1 --help
Usage: test.py phase1 [OPTIONS] PHASE

  Phase1 Command

Options:
  --help  Show this message and exit.
-----------
> 1 phase1 2 
2
-----------
> 1 phase1 2 --help
Usage: test.py phase1 [OPTIONS] PHASE

  Phase1 Command

Options:
  --help  Show this message and exit.
-----------
> 1 phase2
Usage: test.py phase2 [OPTIONS] PHASE COMMAND [ARGS]...

  Phase2 Group

Options:
  --help  Show this message and exit.
-----------
> 1 phase2 --help
Usage: test.py phase2 [OPTIONS] PHASE COMMAND [ARGS]...

  Phase2 Group

Options:
  --help  Show this message and exit.
-----------
> 1 phase2 2 
2
-----------
> 1 phase2 2 --help
Usage: test.py phase2 [OPTIONS] PHASE COMMAND [ARGS]...

  Phase2 Group

Options:
  --help  Show this message and exit.
-----------
> --help
Usage: test.py [OPTIONS] CHALLENGE COMMAND [ARGS]...

Options:
  --help  Show this message and exit.

Commands:
  phase1  Phase1 Command
  phase2  Phase2 Group