Skip to content
Myr

📝 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

12 files+34 added−17 removed

modified:docs/tutorial/options/name.md+2 added-2 removed
1 unmodified line
23So, if you have a function with:45```Python6def main(user_name: str = None):6def main(user_name: Optional[str] = None):7 pass8```910or1112```Python13def main(user_name: str = typer.Option(None)):13def main(user_name: Optional[str] = typer.Option(None)):14 pass15```1617**Typer** will create a *CLI option*:
311 unmodified lines
modified:docs/tutorial/options/version.md+3 added-3 removed
modified:docs/tutorial/parameter-types/bool.md+1 added-1 removed
58 unmodified lines
59We 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"63```Python hl_lines="6"64{!../docs_src/parameter_types/bool/tutorial002.py!}65```6667Check it:
123 unmodified lines
modified:docs/tutorial/parameter-types/path.md+1 added-1 removed
1You 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>.23This is what you would do for directory paths, file paths, etc:45```Python hl_lines="1 6"5```Python hl_lines="1 7"6{!../docs_src/parameter_types/path/tutorial001.py!}7```89And 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 lines
modified:docs/typer-cli.md+4 added-2 removed
103 unmodified lines
104105Let's say you have a script that uses **Typer** in `my_custom_script.py`:106107```Python108from typing import Optional109110import typer111112app = typer.Typer()113114115@app.command()114def hello(name: str = None):116def hello(name: Optional[str] = None):117 if name:118 typer.echo(f"Hello {name}")119 else:120 typer.echo("Hello World!")121122123@app.command()122def bye(name: str = None):124def bye(name: Optional[str] = None):125 if name:126 typer.echo(f"Bye {name}")127 else:128 typer.echo("Goodbye!")
305 unmodified lines
modified:docs_src/multiple_values/multiple_options/tutorial001.py+2 added-2 removed
1from typing import List1from typing import List, Optional23import typer456def main(user: List[str] = typer.Option(None)):6def 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 lines
modified:docs_src/options/version/tutorial001.py+5 added-1 removed
1from typing import Optional23import typer45__version__ = "0.1.0"6
5 unmodified lines
121314def 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}")2122
2 unmodified lines
modified:docs_src/options/version/tutorial002.py+5 added-1 removed
1from typing import Optional23import typer45__version__ = "0.1.0"6
10 unmodified lines
171819def 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}")2627
2 unmodified lines
modified:docs_src/options/version/tutorial003.py+3 added-1 removed
1from typing import Optional23import typer45__version__ = "0.1.0"6
11 unmodified lines
181920def 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 lines
modified:docs_src/parameter_types/bool/tutorial002.py+3 added-1 removed
1from typing import Optional23import typer454def main(accept: bool = typer.Option(None, "--accept/--reject")):6def 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 lines
modified:docs_src/parameter_types/path/tutorial001.py+2 added-1 removed
1from pathlib import Path2from typing import Optional34import typer566def main(config: Path = typer.Option(None)):7def 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 lines
modified:docs_src/testing/app01/main.py+3 added-1 removed
1from typing import Optional23import typer45app = typer.Typer()678@app.command()7def main(name: str, city: str = None):9def 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