: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
Provenance
- Commit
fb8622b- Tree
08987f4- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
Sebastián Ramírez <tiangolo@gmail.com>- Parent
ff37cb8
8 files changed+247 added−0 removed
added:docs/src/multiple_values/arguments_with_multiple_values/tutorial001.py+15 added
1+from pathlib import Path2+from typing import List3+4+import typer5+6+7+def 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)12+13+14+if __name__ == "__main__":15+ typer.run(main)added:docs/src/multiple_values/multiple_options/tutorial001.py+15 added
1+from typing import List2+3+import typer4+5+6+def 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}")12+13+14+if __name__ == "__main__":15+ typer.run(main)added:docs/src/multiple_values/multiple_options/tutorial002.py+11 added
1+from typing import List2+3+import typer4+5+6+def main(number: List[float] = typer.Option([])):7+ typer.echo(f"The sum is {sum(number)}")8+9+10+if __name__ == "__main__":11+ typer.run(main)added:docs/src/multiple_values/options_with_multiple_values/tutorial001.py+17 added
1+from typing import Tuple2+3+import typer4+5+6+def 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!")14+15+16+if __name__ == "__main__":17+ typer.run(main)added:docs/tutorial/multiple-values/arguments-with-multiple-values.md+28 added
12345678910111213141516171819202122232425262728*CLI arguments* can also receive multiple values.
You can define the type of a *CLI argument* using `typing.List`.
```Python hl_lines="7"{!./src/multiple_values/arguments_with_multiple_values/tutorial001.py!}```
And then you can pass it as many *CLI arguments* of that type as you want:
<div class="termy">
```console$ python main.py ./index.md ./first-steps.md woohoo!
This file exists: index.mdwoohoo!This file exists: first-steps.mdwoohoo!```
</div>
!!! tip We also declared a final *CLI argument* `celebration`, and it's correctly used even if we pass an arbitrary number of `files` first.
!!! info A `List` can only be used in the last command (if there are subcommands), as this will take anything to the right and assume it's part of the expected *CLI arguments*.added:docs/tutorial/multiple-values/index.md+3 added
123There are several ways to declare multiple values for *CLI options* and *CLI arguments*.
We'll see them in the next short sections.added:docs/tutorial/multiple-values/multiple-options.md+66 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566You can declare a *CLI option* that can be used multiple times, and then get all the values.
For example, let's say you want to accept several users in a single execution.
For this, use the standard Python `typing.List` to declare it as a `list` of `str`:
```Python hl_lines="1 6"{!./src/multiple_values/multiple_options/tutorial001.py!}```
You will receive the values as you declared them, as a `list` of `str`.
Check it:
<div class="termy">
```console$ python main.py
No provided usersAborted!
// Now pass a user$ python main.py --user Camila
Processing user: Camila
// And now try with several users$ python main.py --user Camila --user Rick --user Morty
Processing user: CamilaProcessing user: RickProcessing user: Morty```
</div>
## Multiple `float`
The same way, you can use other types and they will be converted by **Typer** to their declared type:
```Python hl_lines="6"{!./src/multiple_values/multiple_options/tutorial002.py!}```
Check it:
<div class="termy">
```console$ python main.py
The sum is 0
// Try with some numbers$ python main.py --number 2
The sum is 2.0
// Try with some numbers$ python main.py --number 2 --number 3 --number 4.5
The sum is 9.5```
</div>added:docs/tutorial/multiple-values/options-with-multiple-values.md+92 added
1+You can also declare a *CLI option* that takes several values of different types.2+3+You can set the number of values and types to anything you want, but it has to be a fixed number of values.4+5+For this, use the standard Python `typing.Tuple`:6+7+```Python hl_lines="1 6"8+{!./src/multiple_values/options_with_multiple_values/tutorial001.py!}9+```10+11+Each of the internal types defines the type of each value in the tuple.12+13+So:14+15+```Python16+user: Tuple[str, int, bool]17+```18+19+means that the parameter `user` is a tuple of 3 values.20+21+* The first value is a `str`.22+* The second value is an `int`.23+* The third value is a `bool`.24+25+Later we do:26+27+```Python28+username, coins, is_wizard = user29+```30+31+If 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:32+33+* 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`.36+37+So, this:38+39+```Python40+username, coins, is_wizard = user41+```42+43+is equivalent to this:44+45+```Python46+username = user[0]47+coins = user[1]48+is_wizard = user[2]49+```50+51+!!! tip52+ Notice that the default is a tuple with `(None, None, None)`.53+54+ 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>.55+56+## Check it57+58+Now let's see how this works in the terminal:59+60+<div class="termy">61+62+```console63+// check the help64+$ python main.py --help65+66+// Notice the <TEXT INTEGER BOOLEAN>67+Usage: main.py [OPTIONS]68+69+Options:70+ --user <TEXT INTEGER BOOLEAN>...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.74+75+// Now try it76+$ python main.py --user Camila 50 yes77+78+The username Camila has 50 coins79+And this user is a wizard!80+81+// With other values82+$ python main.py --user Morty 3 no83+84+The username Morty has 3 coins85+86+// Try with invalid values (not enough)87+$ python main.py --user Camila 5088+89+Error: --user option requires 3 arguments90+```91+92+</div>