Skip to content
Myr

:memo: Add docs and examples for CLI parameters with multiple values

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 Sebastián Ramírez · parent ff37cb8 · Browse files · 8 files+247 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

8 files+247 added−0 removed

  • other:+247 added−0 removed

8 files+247 added−0 removed

added:docs/src/multiple_values/arguments_with_multiple_values/tutorial001.py+15 added
1from pathlib import Path2from typing import List34import typer567def main(files: List[Path], celebration: str):8 for path in files:9 if path.is_file():10 typer.echo(f"This file exists: {path.name}")11 typer.echo(celebration)121314if __name__ == "__main__":15 typer.run(main)
added:docs/src/multiple_values/multiple_options/tutorial001.py+15 added
1from typing import List23import typer456def main(user: List[str] = typer.Option(None)):7 if not user:8 typer.echo("No provided users")9 raise typer.Abort()10 for u in user:11 typer.echo(f"Processing user: {u}")121314if __name__ == "__main__":15 typer.run(main)
added:docs/src/multiple_values/multiple_options/tutorial002.py+11 added
1from typing import List23import typer456def main(number: List[float] = typer.Option([])):7 typer.echo(f"The sum is {sum(number)}")8910if __name__ == "__main__":11 typer.run(main)
added:docs/src/multiple_values/options_with_multiple_values/tutorial001.py+17 added
1from typing import Tuple23import typer456def main(user: Tuple[str, int, bool] = typer.Option((None, None, None))):7 username, coins, is_wizard = user8 if not username:9 typer.echo("No user provided")10 raise typer.Abort()11 typer.echo(f"The username {username} has {coins} coins")12 if is_wizard:13 typer.echo("And this user is a wizard!")141516if __name__ == "__main__":17 typer.run(main)
added:docs/tutorial/multiple-values/arguments-with-multiple-values.md+28 added
added:docs/tutorial/multiple-values/index.md+3 added
added:docs/tutorial/multiple-values/multiple-options.md+66 added
added:docs/tutorial/multiple-values/options-with-multiple-values.md+92 added
1You can also declare a *CLI option* that takes several values of different types.23You can set the number of values and types to anything you want, but it has to be a fixed number of values.45For this, use the standard Python `typing.Tuple`:67```Python hl_lines="1 6"8{!./src/multiple_values/options_with_multiple_values/tutorial001.py!}9```1011Each of the internal types defines the type of each value in the tuple.1213So:1415```Python16user: Tuple[str, int, bool]17```1819means that the parameter `user` is a tuple of 3 values.2021* The first value is a `str`.22* The second value is an `int`.23* The third value is a `bool`.2425Later we do:2627```Python28username, coins, is_wizard = user29```3031If you hadn't seen that, it means that `user` is a tuple with 3 values, and we are assigning each of the values to a new variable:3233* The first value in the tuple `user` (a `str`) goes to the variable `username`.34* The second value in the tuple `user` (an `int`) goes to the variable `coins`.35* The third value in the tuple `user` (a `bool`) goes to the variable `is_wizard`.3637So, this:3839```Python40username, coins, is_wizard = user41```4243is equivalent to this:4445```Python46username = user[0]47coins = user[1]48is_wizard = user[2]49```5051!!! tip52 Notice that the default is a tuple with `(None, None, None)`.5354 You cannot simply use `None` here as the default because <a href="https://github.com/pallets/click/issues/472" target="_blank">Click doesn't support it</a>.5556## Check it5758Now let's see how this works in the terminal:5960<div class="termy">6162```console63// check the help64$ python main.py --help6566// Notice the &lt;TEXT INTEGER BOOLEAN&gt;67Usage: main.py [OPTIONS]6869Options:70 --user &lt;TEXT INTEGER BOOLEAN&gt;...71 --install-completion Install completion for the current shell.72 --show-completion Show completion for the current shell, to copy it or customize the installation.73 --help Show this message and exit.7475// Now try it76$ python main.py --user Camila 50 yes7778The username Camila has 50 coins79And this user is a wizard!8081// With other values82$ python main.py --user Morty 3 no8384The username Morty has 3 coins8586// Try with invalid values (not enough)87$ python main.py --user Camila 508889Error: --user option requires 3 arguments90```9192</div>