What if a Typer command returns a value

148 Views Asked by At

I'm using Typer to implement a CLI tool. For testing purpose, I'd like some commands return some value, but I'm not sure whether it's safe. I didn't find anything related in its user guide.

As Typer uses Click, I found the following statements in Click user guide:

When a Click script is invoked as command line application (through BaseCommand.main()) the return value is ignored unless the standalone_mode is disabled in which case it’s bubbled through.

Does this behavior also apply to Typer?


As a minimum example, I'd like to do change my original code

@app.command()
def my_command():
    do_something()

to

@app.command()
def my_command():
    result = do_something()
    return result

where result is only for testing so that I can test my_command with:

from typer.testing import CliRunner
from myapp import app

def test_my_command():
    result = CliRunner().invoke(app)
    assert result.return_value == expected_value

My question is whether the change (adding return result) will influence the behaviour seen by users.

1

There are 1 best solutions below

3
Alirezaarabi On

Yes, this feature added to the newest version of the typer and it's called Custom Type which is accessable in the below link:

https://typer.tiangolo.com/tutorial/parameter-types/custom-types/