gpt4 book ai didi

Python Click - 仅在父命令成功执行时才执行子命令

转载 作者:太空狗 更新时间:2023-10-30 01:31:21 26 4
gpt4 key购买 nike

我正在使用 Click构建 Python CLI 时遇到了一个问题,即如何在 Click 中处理异常。

我不确定这里的措辞 ("subcommand", "parentcommand") 但从我的例子中你会明白我希望的想法。让我们假设这段代码:

@click.group()
@click.option("--something")
def mycli(something):
try:
#do something with "something" and set ctx
ctx.obj = {}
ctx.obj["somevar"] = some_result
except:
print("Something went wrong")
raise

#only if everything went fine call mycommand

@click.group()
@click.pass_context
def mygroup(ctx):
pass

@mygroup.command(name="mycommand")
@click.pass_context
def mycommand(ctx):
#this only works if somevar is set in ctx so don't call this if setting went wrong in mycli

当应用程序启动时调用:

if __name__ == "__main__":
mycli.add_command(mygroup)
mycli()

然后我像这样启动程序:

python myapp --something somevalue mycommand

预期行为:第一个mycli被调用并执行其中的代码。如果抛出异常,它会被 except block 捕获,打印一条消息并引发异常。因为我们没有其他 try/except block ,所以这将导致脚本终止。 “子”命令 mycommand永远不会被调用,因为程序在运行“父”命令时已经终止 mycli .

实际行为:捕获异常并打印消息,但是 mycommand仍然被称为。然后它失败并显示另一条异常消息,因为未设置所需的上下文变量。

我该如何处理这样的事情?基本上我只想调用子命令 mycommand只有在 mycli 中的所有内容时才会执行一切顺利。

最佳答案

要处理异常,但不继续执行子命令,您可以简单地调用 exit(),例如:

代码:

import click

@click.group()
@click.option("--something")
@click.pass_context
def mycli(ctx, something):
ctx.obj = dict(a_var=something)
try:
if something != '1':
raise IndexError('An Error')
except Exception as exc:
click.echo('Exception: {}'.format(exc))
exit()

测试代码:

@mycli.group()
@click.pass_context
def mygroup(ctx):
click.echo('mygroup: {}'.format(ctx.obj['a_var']))
pass


@mygroup.command()
@click.pass_context
def mycommand(ctx):
click.echo('mycommand: {}'.format(ctx.obj['a_var']))


if __name__ == "__main__":
commands = (
'mygroup mycommand',
'--something 1 mygroup mycommand',
'--something 2 mygroup mycommand',
'--help',
'--something 1 mygroup --help',
'--something 1 mygroup mycommand --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)
mycli(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)]
-----------
> mygroup mycommand
Exception: An Error
-----------
> --something 1 mygroup mycommand
mygroup: 1
mycommand: 1
-----------
> --something 2 mygroup mycommand
Exception: An Error
-----------
> --help
Usage: test.py [OPTIONS] COMMAND [ARGS]...

Options:
--something TEXT
--help Show this message and exit.

Commands:
mygroup
-----------
> --something 1 mygroup --help
Usage: test.py mygroup [OPTIONS] COMMAND [ARGS]...

Options:
--help Show this message and exit.

Commands:
mycommand
-----------
> --something 1 mygroup mycommand --help
mygroup: 1
Usage: test.py mygroup mycommand [OPTIONS]

Options:
--help Show this message and exit.

关于Python Click - 仅在父命令成功执行时才执行子命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51847639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com