这是我的代码:
@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,发现python click app is failing with indication of "missing argument" but cannot locate the issue这是一个存在相同问题的用户,但是我不太理解答案。
我不能将参数设为可选,因为它是使CLI正常工作的关键部分。
答案 0 :(得分:2)
您的代码面临的基本问题是您试图像命令一样使用组。至少有两种方法可以改善这种情况:
终端实体通常是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