Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/tutorial/typer-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,41 @@ Hello Camila

</div>

### Run a package or module with specified function

You can also specify a function directly using the colon syntax:

<div class="termy">

```console
$ typer my_package.main:my_function run --name Camila

Hello Camila
```

</div>

or with the file path

<div class="termy">

```console
$ typer main.py:my_function run --name Camila

Hello Camila
```

</div>

## Options

You can specify one of the following **CLI options**:

* `--app`: the name of the variable with a `Typer()` object to run as the main app.
* `--func`: the name of the variable with a function that would be used with `typer.run()`.

Alternatively, you can specify the function directly using the syntax `module:function`.

### Defaults

When your run a script with the `typer` command it will use the app from the following priority:
Expand Down
58 changes: 58 additions & 0 deletions tests/test_cli/test_multi_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ def test_script_func_not_function():
assert "Not a function:" in result.stderr


def test_script_colon_not_function():
result = subprocess.run(
[
sys.executable,
"-m",
"coverage",
"run",
"-m",
"typer",
"tests/assets/cli/multi_func.py:message",
"run",
"--name",
"Camila",
],
capture_output=True,
encoding="utf-8",
)
assert "Not a function:" in result.stderr


def test_script_func():
result = subprocess.run(
[
Expand All @@ -104,3 +124,41 @@ def test_script_func():
)
assert "Hello" not in result.stdout
assert "Stuff" in result.stdout


def test_script_module_colon_func():
result = subprocess.run(
[
sys.executable,
"-m",
"coverage",
"run",
"-m",
"typer",
"tests.assets.cli.multi_func:say_stuff",
"run",
],
capture_output=True,
encoding="utf-8",
)
assert "Hello" not in result.stdout
assert "Stuff" in result.stdout


def test_script_file_colon_func():
result = subprocess.run(
[
sys.executable,
"-m",
"coverage",
"run",
"-m",
"typer",
"tests/assets/cli/multi_func.py:say_stuff",
"run",
],
capture_output=True,
encoding="utf-8",
)
assert "Hello" not in result.stdout
assert "Stuff" in result.stdout
4 changes: 4 additions & 0 deletions typer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def __init__(self) -> None:
def maybe_update_state(ctx: click.Context) -> None:
path_or_module = ctx.params.get("path_or_module")
if path_or_module:
if ":" in path_or_module:
module_part, func_part = path_or_module.rsplit(":", 1)
path_or_module = module_part
ctx.params.update({"func": func_part})
file_path = Path(path_or_module)
if file_path.exists() and file_path.is_file():
state.file = file_path
Expand Down
Loading