📝 Add Optional to docs for CLI Arguments and Options with a None default (#131)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rupsi Kaushik · committed by GitHub · parent c2235d1 · Browse files · 12 files·+34 added−17 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: Sebastián Ramírez <tiangolo@gmail.com>
Diff stats
12 files+34 added−17 removed
- other:+34 added−17 removed
Provenance
- Commit
32436f0- Tree
a408319- Author
Rupsi Kaushik <rkaus053@uottawa.ca>- Committer
GitHub <noreply@github.com>- Parent
c2235d1
12 files changed+34 added−17 removed
modified:docs/tutorial/options/name.md+2 added-2 removed
1 unmodified line2 3 So, if you have a function with:4 5 ```Python6-def main(user_name: str = None):6+def main(user_name: Optional[str] = None):7 pass8 ```9 10 or11 12 ```Python13-def main(user_name: str = typer.Option(None)):13+def main(user_name: Optional[str] = typer.Option(None)):14 pass15 ```16 17 **Typer** will create a *CLI option*:311 unmodified linesmodified:docs/tutorial/options/version.md+3 added-3 removed
4 unmodified lines5678991011121342 unmodified lines5657585960606162636423 unmodified lines8889909192929394959611 unmodified lines4 unmodified lines## First version of `--version`
Let's see a first version of how it could look like:
```Python hl_lines="6 7 8 9 14"```Python hl_lines="8-11 16-18"{!../docs_src/options/version/tutorial001.py!}```
!!! tip42 unmodified lines## Previous parameters and `is_eager`
But now let's say that the `--name` *CLI option* that we declared before `--version` is required, and it has a callback that could exit the program:
```Python hl_lines="12 13 14 18"```Python hl_lines="14-16 21-23"{!../docs_src/options/version/tutorial002.py!}```
Then our CLI program could not work as expected in some cases as it is *right now*, because if we use `--version` after `--name` then the callback for `--name` will be processed before and we can get its error:23 unmodified linesFor those cases, we can mark a *CLI parameter* (a *CLI option* or *CLI argument*) with `is_eager=True`.
That will tell **Typer** (actually Click) that it should process this *CLI parameter* before the others:
```Python hl_lines="21"```Python hl_lines="22-24"{!../docs_src/options/version/tutorial003.py!}```
Check it:11 unmodified linesmodified:docs/tutorial/parameter-types/bool.md+1 added-1 removed
58 unmodified lines59 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"63+```Python hl_lines="6"64 {!../docs_src/parameter_types/bool/tutorial002.py!}65 ```66 67 Check it:123 unmodified linesmodified:docs/tutorial/parameter-types/path.md+1 added-1 removed
1 You can declare a *CLI parameter* to be a standard Python <a href="https://docs.python.org/3/library/pathlib.html#basic-use" class="external-link" target="_blank">`pathlib.Path`</a>.2 3 This is what you would do for directory paths, file paths, etc:4 5-```Python hl_lines="1 6"5+```Python hl_lines="1 7"6 {!../docs_src/parameter_types/path/tutorial001.py!}7 ```8 9 And again, as you receive a standard Python `Path` object the same as the type annotation, your editor will give you autocompletion for all its attributes and methods.96 unmodified linesmodified:docs/typer-cli.md+4 added-2 removed
103 unmodified lines104 105 Let's say you have a script that uses **Typer** in `my_custom_script.py`:106 107 ```Python108+from typing import Optional109+110 import typer111 112 app = typer.Typer()113 114 115 @app.command()114-def hello(name: str = None):116+def hello(name: Optional[str] = None):117 if name:118 typer.echo(f"Hello {name}")119 else:120 typer.echo("Hello World!")121 122 123 @app.command()122-def bye(name: str = None):124+def bye(name: Optional[str] = None):125 if name:126 typer.echo(f"Bye {name}")127 else:128 typer.echo("Goodbye!")305 unmodified linesmodified:docs_src/multiple_values/multiple_options/tutorial001.py+2 added-2 removed
1-from typing import List1+from typing import List, Optional2 3 import typer4 5 6-def main(user: List[str] = typer.Option(None)):6+def main(user: Optional[List[str]] = typer.Option(None)):7 if not user:8 typer.echo("No provided users")9 raise typer.Abort()10 for u in user:5 unmodified linesmodified:docs_src/options/version/tutorial001.py+5 added-1 removed
1+from typing import Optional2+3 import typer4 5 __version__ = "0.1.0"6 5 unmodified lines12 13 14 def main(15 name: str = typer.Option("World"),14- version: bool = typer.Option(None, "--version", callback=version_callback),16+ version: Optional[bool] = typer.Option(17+ None, "--version", callback=version_callback18+ ),19 ):20 typer.echo(f"Hello {name}")21 22 2 unmodified linesmodified:docs_src/options/version/tutorial002.py+5 added-1 removed
1+from typing import Optional2+3 import typer4 5 __version__ = "0.1.0"6 10 unmodified lines17 18 19 def main(20 name: str = typer.Option(..., callback=name_callback),19- version: bool = typer.Option(None, "--version", callback=version_callback),21+ version: Optional[bool] = typer.Option(22+ None, "--version", callback=version_callback23+ ),24 ):25 typer.echo(f"Hello {name}")26 27 2 unmodified linesmodified:docs_src/options/version/tutorial003.py+3 added-1 removed
1+from typing import Optional2+3 import typer4 5 __version__ = "0.1.0"6 11 unmodified lines18 19 20 def main(21 name: str = typer.Option(..., callback=name_callback),20- version: bool = typer.Option(22+ version: Optional[bool] = typer.Option(23 None, "--version", callback=version_callback, is_eager=True24 ),25 ):26 typer.echo(f"Hello {name}")4 unmodified linesmodified:docs_src/parameter_types/bool/tutorial002.py+3 added-1 removed
1+from typing import Optional2+3 import typer4 5 4-def main(accept: bool = typer.Option(None, "--accept/--reject")):6+def main(accept: Optional[bool] = typer.Option(None, "--accept/--reject")):7 if accept is None:8 typer.echo("I don't know what you want yet")9 elif accept:10 typer.echo("Accepting!")6 unmodified linesmodified:docs_src/parameter_types/path/tutorial001.py+2 added-1 removed
1 from pathlib import Path2+from typing import Optional3 4 import typer5 6 6-def main(config: Path = typer.Option(None)):7+def main(config: Optional[Path] = typer.Option(None)):8 if config is None:9 typer.echo("No config file")10 raise typer.Abort()11 if config.is_file():10 unmodified linesmodified:docs_src/testing/app01/main.py+3 added-1 removed
1+from typing import Optional2+3 import typer4 5 app = typer.Typer()6 7 8 @app.command()7-def main(name: str, city: str = None):9+def main(name: str, city: Optional[str] = None):10 typer.echo(f"Hello {name}")11 if city:12 typer.echo(f"Let's have a coffee in {city}")13 3 unmodified lines