: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
Provenance
- Commit
ff37cb8- Tree
fd4c9d0- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
439fb06- Parent 2
be75c9c
14 files changed+536 added−0 removed
added:docs/src/parameter_types/bool/__init__.py
added:docs/src/parameter_types/bool/tutorial001.py+12 added
1+import typer2+3+4+def main(force: bool = typer.Option(False, "--force")):5+ if force:6+ typer.echo("Forcing operation")7+ else:8+ typer.echo("Not forcing")9+10+11+if __name__ == "__main__":12+ typer.run(main)added:docs/src/parameter_types/bool/tutorial002.py+14 added
1+import typer2+3+4+def 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!")11+12+13+if __name__ == "__main__":14+ typer.run(main)added:docs/src/parameter_types/bool/tutorial003.py+12 added
1+import typer2+3+4+def 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")9+10+11+if __name__ == "__main__":12+ typer.run(main)added:docs/src/parameter_types/bool/tutorial004.py+12 added
1+import typer2+3+4+def 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")9+10+11+if __name__ == "__main__":12+ typer.run(main)added:docs/src/parameter_types/number/tutorial003.py+9 added
1+import typer2+3+4+def main(verbose: int = typer.Option(0, "--verbose", "-v", count=True)):5+ typer.echo(f"Verbose level is {verbose}")6+7+8+if __name__ == "__main__":9+ typer.run(main)added:docs/tutorial/parameter-types/bool.md+190 added
1+We have seen some examples of *CLI options* with `bool`, and how **Typer** creates `--something` and `--no-something` automatically.2+3+But we can customize those names.4+5+## Only `--force`6+7+Let's say that we want a `--force` *CLI option* only, we want to discard `--no-force`.8+9+We can do that by specifying the exact name we want:10+11+```Python hl_lines="4"12+{!./src/parameter_types/bool/tutorial001.py!}13+```14+15+Now there's only a `--force` *CLI option*:16+17+<div class="termy">18+19+```console20+// Check the help21+$ python main.py --help22+23+// Notice there's only --force, we no longer have --no-force24+Usage: main.py [OPTIONS]25+26+Options: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.31+32+// Try it:33+$ python main.py34+35+Not forcing36+37+// Now add --force38+$ python main.py --force39+40+Forcing operation41+42+// And --no-force no longer exists ⛔️43+$ python main.py --no-force44+45+Usage: main.py [OPTIONS]46+Try "main.py --help" for help.47+48+Error: no such option: --no-force49+```50+51+</div>52+53+## Alternative names54+55+Now let's imagine we have a *CLI option* `--accept`.56+57+And we want to allow setting `--accept` or the contrary, but `--no-accept` looks ugly.58+59+We might want to instead have `--accept` and `--reject`.60+61+We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:62+63+```Python hl_lines="4"64+{!./src/parameter_types/bool/tutorial002.py!}65+```66+67+Check it:68+69+<div class="termy">70+71+```console72+// Check the help73+$ python main.py --help74+75+// Notice the --accept / --reject76+Usage: main.py [OPTIONS]77+78+Options: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.83+84+// Try it85+$ python main.py86+87+I don't know what you want yet88+89+// Now pass --accept90+$ python main.py --accept91+92+Accepting!93+94+// And --reject95+$ python main.py --reject96+97+Rejecting!98+```99+100+</div>101+102+## Short names103+104+The same way, you can declare short versions of the names for these *CLI options*.105+106+For example, let's say we want `-f` for `--force` and `-F` for `--no-force`:107+108+```Python hl_lines="4"109+{!./src/parameter_types/bool/tutorial003.py!}110+```111+112+Check it:113+114+<div class="termy">115+116+```console117+// Check the help118+$ python main.py --help119+120+// Notice the -f, --force / -F, --no-force121+Usage: main.py [OPTIONS]122+123+Options: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.128+129+// Try with the short name -f130+$ python main.py -f131+132+Forcing operation133+134+// Try with the short name -F135+$ python main.py -F136+137+Not forcing138+```139+140+</div>141+142+## Only names for `False`143+144+If you want to (although it might not be a good idea), you can declare only *CLI option* names to set the `False` value.145+146+To do that, use a space and a single `/` and pass the negative name after:147+148+```Python hl_lines="4"149+{!./src/parameter_types/bool/tutorial004.py!}150+```151+152+!!! tip153+ Have in mind that it's a string with a preceding space and then a `/`.154+ 155+ So, it's `" /-S"` not `"/-S"`.156+157+Check it:158+159+<div class="termy">160+161+```console162+// Check the help163+$ python main.py --help164+165+// Notice the / -d, --demo166+Usage: main.py [OPTIONS]167+168+Options: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.173+174+// Try it175+$ python main.py176+177+Running in production178+179+// Now pass --demo180+$ python main.py --demo181+182+Running demo183+184+// And the short version185+$ python main.py -d186+187+Running demo188+```189+190+</div>modified:docs/tutorial/parameter-types/number.md+48 added
96 unmodified lines97989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714896 unmodified lines--score is 0```
</div>
## Counter *CLI options*
You can make a *CLI option* work as a counter with the `counter` parameter:
```Python hl_lines="4"{!./src/parameter_types/number/tutorial003.py!}```
It means that the *CLI option* will be like a boolean flag, e.g. `--verbose`.
And the value you receive in the function will be the amount of times that `--verbose` was added:
<div class="termy">
```console// Check it$ python main.py
Verbose level is 0
// Now use one --verbose$ python main.py --verbose
Verbose level is 1
// Now 3 --verbose$ python main.py --verbose --verbose --verbose
Verbose level is 3
// And with the short name$ python main.py -v
Verbose level is 1
// And with the short name 3 times$ python main.py -v -v -v
Verbose level is 3
// As short names can be put together, this also works$ python main.py -vvv
Verbose level is 3```
</div>No newline at end of fileadded:tests/test_tutorial/test_parameter_types/test_bool/__init__.py
added:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial001.py+46 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from parameter_types.bool import tutorial001 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def 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.output19+20+21+def test_no_force():22+ result = runner.invoke(app)23+ assert result.exit_code == 024+ assert "Not forcing" in result.output25+26+27+def test_force():28+ result = runner.invoke(app, ["--force"])29+ assert result.exit_code == 030+ assert "Forcing operation" in result.output31+32+33+def 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.output37+38+39+def 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.stdoutadded:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial002.py+52 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from parameter_types.bool import tutorial002 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def 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.output19+20+21+def test_main():22+ result = runner.invoke(app)23+ assert result.exit_code == 024+ assert "I don't know what you want yet" in result.output25+26+27+def test_accept():28+ result = runner.invoke(app, ["--accept"])29+ assert result.exit_code == 030+ assert "Accepting!" in result.output31+32+33+def test_reject():34+ result = runner.invoke(app, ["--reject"])35+ assert result.exit_code == 036+ assert "Rejecting!" in result.output37+38+39+def 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.output43+44+45+def 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.stdoutadded:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial003.py+39 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from parameter_types.bool import tutorial003 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "-f, --force / -F, --no-force" in result.output18+19+20+def test_force():21+ result = runner.invoke(app, ["-f"])22+ assert result.exit_code == 023+ assert "Forcing operation" in result.output24+25+26+def test_no_force():27+ result = runner.invoke(app, ["-F"])28+ assert result.exit_code == 029+ assert "Not forcing" in result.output30+31+32+def 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.stdoutadded:tests/test_tutorial/test_parameter_types/test_bool/test_tutorial004.py+45 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from parameter_types.bool import tutorial004 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_help():15+ result = runner.invoke(app, ["--help"])16+ assert result.exit_code == 017+ assert "/ -d, --demo" in result.output18+19+20+def test_main():21+ result = runner.invoke(app)22+ assert result.exit_code == 023+ assert "Running in production" in result.output24+25+26+def test_demo():27+ result = runner.invoke(app, ["--demo"])28+ assert result.exit_code == 029+ assert "Running demo" in result.output30+31+32+def test_short_demo():33+ result = runner.invoke(app, ["-d"])34+ assert result.exit_code == 035+ assert "Running demo" in result.output36+37+38+def 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.stdoutadded:tests/test_tutorial/test_parameter_types/test_number/test_tutorial003.py+57 added
1+import subprocess2+3+import typer4+from typer.testing import CliRunner5+6+from parameter_types.number import tutorial003 as mod7+8+runner = CliRunner()9+10+app = typer.Typer()11+app.command()(mod.main)12+13+14+def test_main():15+ result = runner.invoke(app)16+ assert result.exit_code == 017+ assert "Verbose level is 0" in result.output18+19+20+def test_verbose_1():21+ result = runner.invoke(app, ["--verbose"])22+ assert result.exit_code == 023+ assert "Verbose level is 1" in result.output24+25+26+def test_verbose_3():27+ result = runner.invoke(app, ["--verbose", "--verbose", "--verbose"])28+ assert result.exit_code == 029+ assert "Verbose level is 3" in result.output30+31+32+def test_verbose_short_1():33+ result = runner.invoke(app, ["-v"])34+ assert result.exit_code == 035+ assert "Verbose level is 1" in result.output36+37+38+def 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.output42+43+44+def test_verbose_short_3_condensed():45+ result = runner.invoke(app, ["-vvv"])46+ assert result.exit_code == 047+ assert "Verbose level is 3" in result.output48+49+50+def 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