ish*_*ht3 1 python command-line-interface python-click
这是我的代码:
@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)
Run Code Online (Sandbox Code Playgroud)
这些命令一起工作应该是这样的。
cli challenge 1 phase 1
Run Code Online (Sandbox Code Playgroud)
通过正确执行,它可以按预期工作。
但是当我使用 --help 或在阶段上定义任何其他标志时,它会抛出
cli challenge 1 phase 1 --help
It throws Error: Missing argument "PHASE".
Run Code Online (Sandbox Code Playgroud)
我搜索了 SOF,发现python click app 失败,并显示“缺少参数”,但无法找到问题这是一个有同样问题的用户,但我不太明白答案。
我不能将参数设置为可选,因为它是 CLI 工作的关键部分。
您的代码面临的根本问题是您尝试像命令一样使用组。至少有两种方法可以改善这种情况:
终端实体通常是 aclick.Command而不是 a click.Group。如果您更改phase为 a,click.Command帮助将按预期工作。
@challenge.command()
@click.argument('PHASE', type=int)
def phase(ctx, phase):
....
Run Code Online (Sandbox Code Playgroud)
no_args_is_help与组一起使用:如果您传递no_args_is_help=True给click.Group构造函数,帮助将按照您的希望显示。但除非你有充分的理由,否则我建议使用click.Command如上所示的 a 。
@challenge.group(invoke_without_command=True, no_args_is_help=True)
@click.argument('PHASE', type=int)
def phase(ctx, phase):
....
Run Code Online (Sandbox Code Playgroud)
add_command():您通常不需要使用:
@click.command()
def my_command():
....
challenge.add_command(my_command)
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
@challenge.command()
def my_command():
....
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4417 次 |
| 最近记录: |