Skip to content
Myr

:memo: Add extra docs and tests for param types

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

authored by Sebastián Ramírez · committed by GitHub · parents 439fb06, be75c9c · Browse files · 14 files+536 added−0 removed
This commit

State

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

Diff stats

14 files+536 added−0 removed

  • tests:+239 added−0 removed
  • other:+297 added−0 removed

14 files+536 added−0 removed

added:docs/src/parameter_types/bool/__init__.py
added:docs/src/parameter_types/bool/tutorial001.py+12 added
1import typer234def main(force: bool = typer.Option(False, "--force")):5 if force:6 typer.echo("Forcing operation")7 else:8 typer.echo("Not forcing")91011if __name__ == "__main__":12 typer.run(main)
added:docs/src/parameter_types/bool/tutorial002.py+14 added
1import typer234def main(accept: bool = typer.Option(None, "--accept/--reject")):5 if accept is None:6 typer.echo("I don't know what you want yet")7 elif accept:8 typer.echo("Accepting!")9 else:10 typer.echo("Rejecting!")111213if __name__ == "__main__":14 typer.run(main)
added:docs/src/parameter_types/bool/tutorial003.py+12 added
1import typer234def main(force: bool = typer.Option(False, "--force/--no-force", "-f/-F")):5 if force:6 typer.echo("Forcing operation")7 else:8 typer.echo("Not forcing")91011if __name__ == "__main__":12 typer.run(main)
added:docs/src/parameter_types/bool/tutorial004.py+12 added
1import typer234def main(in_prod: bool = typer.Option(True, " /--demo", " /-d")):5 if in_prod:6 typer.echo("Running in production")7 else:8 typer.echo("Running demo")91011if __name__ == "__main__":12 typer.run(main)
added:docs/src/parameter_types/number/tutorial003.py+9 added
1import typer234def main(verbose: int = typer.Option(0, "--verbose", "-v", count=True)):5 typer.echo(f"Verbose level is {verbose}")678if __name__ == "__main__":9 typer.run(main)
added:docs/tutorial/parameter-types/bool.md+190 added
1We have seen some examples of *CLI options* with `bool`, and how **Typer** creates `--something` and `--no-something` automatically.23But we can customize those names.45## Only `--force`67Let's say that we want a `--force` *CLI option* only, we want to discard `--no-force`.89We can do that by specifying the exact name we want:1011```Python hl_lines="4"12{!./src/parameter_types/bool/tutorial001.py!}13```1415Now there's only a `--force` *CLI option*:1617<div class="termy">1819```console20// Check the help21$ python main.py --help2223// Notice there's only --force, we no longer have --no-force24Usage: main.py [OPTIONS]2526Options:27 --force28 --install-completion Install completion for the current shell.29 --show-completion Show completion for the current shell, to copy it or customize the installation.30 --help Show this message and exit.3132// Try it:33$ python main.py3435Not forcing3637// Now add --force38$ python main.py --force3940Forcing operation4142// And --no-force no longer exists ⛔️43$ python main.py --no-force4445Usage: main.py [OPTIONS]46Try "main.py --help" for help.4748Error: no such option: --no-force49```5051</div>5253## Alternative names5455Now let's imagine we have a *CLI option* `--accept`.5657And we want to allow setting `--accept` or the contrary, but `--no-accept` looks ugly.5859We might want to instead have `--accept` and `--reject`.6061We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:6263```Python hl_lines="4"64{!./src/parameter_types/bool/tutorial002.py!}65```6667Check it:6869<div class="termy">7071```console72// Check the help73$ python main.py --help7475// Notice the --accept / --reject76Usage: main.py [OPTIONS]7778Options:79 --accept / --reject80 --install-completion Install completion for the current shell.81 --show-completion Show completion for the current shell, to copy it or customize the installation.82 --help Show this message and exit.8384// Try it85$ python main.py8687I don't know what you want yet8889// Now pass --accept90$ python main.py --accept9192Accepting!9394// And --reject95$ python main.py --reject9697Rejecting!98```99100</div>101102## Short names103104The same way, you can declare short versions of the names for these *CLI options*.105106For example, let's say we want `-f` for `--force` and `-F` for `--no-force`:107108```Python hl_lines="4"109{!./src/parameter_types/bool/tutorial003.py!}110```111112Check it:113114<div class="termy">115116```console117// Check the help118$ python main.py --help119120// Notice the -f, --force / -F, --no-force121Usage: main.py [OPTIONS]122123Options:124 -f, --force / -F, --no-force125 --install-completion Install completion for the current shell.126 --show-completion Show completion for the current shell, to copy it or customize the installation.127 --help Show this message and exit.128129// Try with the short name -f130$ python main.py -f131132Forcing operation133134// Try with the short name -F135$ python main.py -F136137Not forcing138```139140</div>141142## Only names for `False`143144If you want to (although it might not be a good idea), you can declare only *CLI option* names to set the `False` value.145146To do that, use a space and a single `/` and pass the negative name after:147148```Python hl_lines="4"149{!./src/parameter_types/bool/tutorial004.py!}150```151152!!! tip153 Have in mind that it's a string with a preceding space and then a `/`.154 155 So, it's `" /-S"` not `"/-S"`.156157Check it:158159<div class="termy">160161```console162// Check the help163$ python main.py --help164165// Notice the / -d, --demo166Usage: main.py [OPTIONS]167168Options:169 / -d, --demo170 --install-completion Install completion for the current shell.171 --show-completion Show completion for the current shell, to copy it or customize the installation.172 --help Show this message and exit.173174// Try it175$ python main.py176177Running in production178179// Now pass --demo180$ python main.py --demo181182Running demo183184// And the short version185$ python main.py -d186187Running demo188```189190</div>
modified:docs/tutorial/parameter-types/number.md+48 added
added:tests/test_tutorial/test_parameter_types/test_bool/__init__.py
added:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial001.py+46 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.bool import tutorial001 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "--force" in result.output18 assert "--no-force" not in result.output192021def test_no_force():22 result = runner.invoke(app)23 assert result.exit_code == 024 assert "Not forcing" in result.output252627def test_force():28 result = runner.invoke(app, ["--force"])29 assert result.exit_code == 030 assert "Forcing operation" in result.output313233def test_invalid_no_force():34 result = runner.invoke(app, ["--no-force"])35 assert result.exit_code != 036 assert "Error: no such option: --no-force" in result.output373839def test_script():40 result = subprocess.run(41 ["coverage", "run", mod.__file__, "--help"],42 stdout=subprocess.PIPE,43 stderr=subprocess.PIPE,44 encoding="utf-8",45 )46 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial002.py+52 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.bool import tutorial002 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "--accept / --reject" in result.output18 assert "--no-accept" not in result.output192021def test_main():22 result = runner.invoke(app)23 assert result.exit_code == 024 assert "I don't know what you want yet" in result.output252627def test_accept():28 result = runner.invoke(app, ["--accept"])29 assert result.exit_code == 030 assert "Accepting!" in result.output313233def test_reject():34 result = runner.invoke(app, ["--reject"])35 assert result.exit_code == 036 assert "Rejecting!" in result.output373839def test_invalid_no_accept():40 result = runner.invoke(app, ["--no-accept"])41 assert result.exit_code != 042 assert "Error: no such option: --no-accept" in result.output434445def test_script():46 result = subprocess.run(47 ["coverage", "run", mod.__file__, "--help"],48 stdout=subprocess.PIPE,49 stderr=subprocess.PIPE,50 encoding="utf-8",51 )52 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial003.py+39 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.bool import tutorial003 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "-f, --force / -F, --no-force" in result.output181920def test_force():21 result = runner.invoke(app, ["-f"])22 assert result.exit_code == 023 assert "Forcing operation" in result.output242526def test_no_force():27 result = runner.invoke(app, ["-F"])28 assert result.exit_code == 029 assert "Not forcing" in result.output303132def test_script():33 result = subprocess.run(34 ["coverage", "run", mod.__file__, "--help"],35 stdout=subprocess.PIPE,36 stderr=subprocess.PIPE,37 encoding="utf-8",38 )39 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial004.py+45 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.bool import tutorial004 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_help():15 result = runner.invoke(app, ["--help"])16 assert result.exit_code == 017 assert "/ -d, --demo" in result.output181920def test_main():21 result = runner.invoke(app)22 assert result.exit_code == 023 assert "Running in production" in result.output242526def test_demo():27 result = runner.invoke(app, ["--demo"])28 assert result.exit_code == 029 assert "Running demo" in result.output303132def test_short_demo():33 result = runner.invoke(app, ["-d"])34 assert result.exit_code == 035 assert "Running demo" in result.output363738def test_script():39 result = subprocess.run(40 ["coverage", "run", mod.__file__, "--help"],41 stdout=subprocess.PIPE,42 stderr=subprocess.PIPE,43 encoding="utf-8",44 )45 assert "Usage" in result.stdout
added:tests/test_tutorial/test_parameter_types/test_number/test_tutorial003.py+57 added
1import subprocess23import typer4from typer.testing import CliRunner56from parameter_types.number import tutorial003 as mod78runner = CliRunner()910app = typer.Typer()11app.command()(mod.main)121314def test_main():15 result = runner.invoke(app)16 assert result.exit_code == 017 assert "Verbose level is 0" in result.output181920def test_verbose_1():21 result = runner.invoke(app, ["--verbose"])22 assert result.exit_code == 023 assert "Verbose level is 1" in result.output242526def test_verbose_3():27 result = runner.invoke(app, ["--verbose", "--verbose", "--verbose"])28 assert result.exit_code == 029 assert "Verbose level is 3" in result.output303132def test_verbose_short_1():33 result = runner.invoke(app, ["-v"])34 assert result.exit_code == 035 assert "Verbose level is 1" in result.output363738def test_verbose_short_3():39 result = runner.invoke(app, ["-v", "-v", "-v"])40 assert result.exit_code == 041 assert "Verbose level is 3" in result.output424344def test_verbose_short_3_condensed():45 result = runner.invoke(app, ["-vvv"])46 assert result.exit_code == 047 assert "Verbose level is 3" in result.output484950def test_script():51 result = subprocess.run(52 ["coverage", "run", mod.__file__, "--help"],53 stdout=subprocess.PIPE,54 stderr=subprocess.PIPE,55 encoding="utf-8",56 )57 assert "Usage" in result.stdout