捕获在其他命令中调用 click 命令时的输出

Capture output of when calling click command inside other command

我在 run 命令中调用 create 命令,但无法捕获 create 的输出。 result 是 None。我错过了什么吗?

调用其他命令并捕获其输出的正确方法是什么?

   @click.command(
        'create')
    @click.pass_context
    def create(ctx):
            ...
            click.echo(click.style(response['name'], fg='green'))
            return response['name']


@click.command(
    'run')
@click.pass_context
def run(ctx):
    result = ctx.invoke(create)

点击上下文链接到父上下文,所以我只是将一个值传递给父上下文。

@click.command('create')
@click.pass_context
    def create(ctx):
        ...
        click.echo(click.style(response['name'], fg='green'))
        if 'invoked_by_subcommand' in ctx.parent.__dict__:
            ctx.parent.return = response['output']


@click.command('run')
@click.pass_context
def run(ctx):
    ctx.invoked_by_subcommand = True
    result = ctx.invoke(create)