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

时间:2018-08-14 18:23:04

标签: python python-click

我正在使用Click来构建Python CLI,并且遇到了有关如何处理Click中异常的问题。

我不确定这里的用语(“子命令”,“父命令”),但是从我的示例中,您将得到我希望的想法。让我们假设这段代码:

@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块捕获,并显示一条消息并引发异常。因为我们没有其他try / except块,这将导致脚本终止。永远不会调用“子”命令mycommand,因为程序在运行“父”命令mycli时已经终止。

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

我该如何处理?基本上,我只想仅在mycommand中的所有命令都正常的情况下才执行子命令mycli

1 个答案:

答案 0 :(得分:1)

要处理异常,但不继续执行子命令,可以像下面这样简单地调用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.