Skip to content
Myr

📝 Add docs and test for `no_args_is_help` feature (#751)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent 434bcdf · Browse files · 3 files+90 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Diff stats

3 files+90 added−0 removed

  • tests:+41 added−0 removed
  • other:+49 added−0 removed

3 files+90 added−0 removed

modified:docs/tutorial/commands/index.md+32 added
209 unmodified lines
210211!!! tip212 By default, the names of the commands are generated from the function name.213214## Show the help message if no command is given215216By default, we need to specify `--help` to get the command's help page.217218However, by setting `no_args_is_help=True` when defining the `typer.Typer()` application, the help function will be shown whenever no argument is given:219220```Python hl_lines="3"221{!../docs_src/commands/index/tutorial003.py!}222```223224Now we can run this:225226<div class="termy">227228```console229// Check the help without having to type --help230$ python main.py231232Usage: main.py [OPTIONS] COMMAND [ARGS]...233234Options:235 --install-completion Install completion for the current shell.236 --show-completion Show completion for the current shell, to copy it or customize the installation.237 --help Show this message and exit.238239Commands:240 create241 delete242```243244</div>245246## Click Group247248If you come from Click, a `typer.Typer` app with subcommands is more or less the equivalent of a <a href="https://click.palletsprojects.com/en/7.x/quickstart/#nesting-commands" class="external-link" target="_blank">Click Group</a>.249
17 unmodified lines
added:docs_src/commands/index/tutorial003.py+17 added
1import typer23app = typer.Typer(no_args_is_help=True)456@app.command()7def create():8 print("Creating user: Hiro Hamada")91011@app.command()12def delete():13 print("Deleting user: Hiro Hamada")141516if __name__ == "__main__":17 app()
added:tests/test_tutorial/test_commands/test_index/test_tutorial003.py+41 added
1import subprocess2import sys34from typer.testing import CliRunner56from docs_src.commands.index import tutorial003 as mod78app = mod.app910runner = CliRunner()111213def test_no_arg():14 result = runner.invoke(app)15 assert result.exit_code == 016 assert "[OPTIONS] COMMAND [ARGS]..." in result.output17 assert "Commands" in result.output18 assert "create" in result.output19 assert "delete" in result.output202122def test_create():23 result = runner.invoke(app, ["create"])24 assert result.exit_code == 025 assert "Creating user: Hiro Hamada" in result.output262728def test_delete():29 result = runner.invoke(app, ["delete"])30 assert result.exit_code == 031 assert "Deleting user: Hiro Hamada" in result.output323334def test_script():35 result = subprocess.run(36 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"],37 stdout=subprocess.PIPE,38 stderr=subprocess.PIPE,39 encoding="utf-8",40 )41 assert "Usage" in result.stdout