pallets / click

Python composable command line interface toolkit
https://click.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
15.82k stars 1.4k forks source link

Return value of CliRunner.invoke is not the return value of the called command #2015

Closed Mitmischer closed 3 years ago

Mitmischer commented 3 years ago

In the documentation to CliRunner::invoke, it says that

The result object has the return_value attribute with the value returned from the invoked command.

However I don't see this reflected, see this example:

import click

from click.testing import CliRunner

@click.command()
@click.argument("arg")
def main(arg):
    return(arg)

runner = CliRunner()
result = runner.invoke(main, "arg")
print(result.return_value)
> python3 main.py
None

I expected result.return_value to be "arg".

The fix should be calling cli.main with standalone_mode == False here. I don't know if that would break other use cases though. So maybe it would also make sense to mimic the standalone flag in CliRunner.invoke.

Environment:

davidism commented 3 years ago

The runner is also used to test the default "standalone mode", so it can't disable it unconditionally. Therefore, if you want to write tests that look at the return value, you have to tell the runner that by passing invoke(standalone_mode=False). We do this in Click's tests too.