这是我的代码:
@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 点击应用程序失败并指示"缺少参数",但找不到问题这是一个有相同问题的用户,但我不太明白答案。
我不能使参数可选,因为它是使 CLI 工作的关键部分。
你的代码面临的根本问题是你试图像命令一样使用一个组。 至少有两种方法可以改善这种情况:
使用命令:
终端 enties 通常是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