📝 Add Optional to docs for CLI Arguments and Options with a None default (#131)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
parent
c2235d1bbf
commit
32436f0b1a
12 changed files with 34 additions and 17 deletions
|
@ -3,14 +3,14 @@ By default **Typer** will create a *CLI option* name from the function parameter
|
|||
So, if you have a function with:
|
||||
|
||||
```Python
|
||||
def main(user_name: str = None):
|
||||
def main(user_name: Optional[str] = None):
|
||||
pass
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```Python
|
||||
def main(user_name: str = typer.Option(None)):
|
||||
def main(user_name: Optional[str] = typer.Option(None)):
|
||||
pass
|
||||
```
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ It would show the version of your CLI program and then it would terminate it. Ev
|
|||
|
||||
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!}
|
||||
```
|
||||
|
||||
|
@ -57,7 +57,7 @@ Awesome CLI Version: 0.1.0
|
|||
|
||||
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!}
|
||||
```
|
||||
|
||||
|
@ -89,7 +89,7 @@ For those cases, we can mark a *CLI parameter* (a *CLI option* or *CLI argument*
|
|||
|
||||
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!}
|
||||
```
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ We might want to instead have `--accept` and `--reject`.
|
|||
|
||||
We can do that by passing a single `str` with the 2 names for the `bool` *CLI option* separated by `/`:
|
||||
|
||||
```Python hl_lines="4"
|
||||
```Python hl_lines="6"
|
||||
{!../docs_src/parameter_types/bool/tutorial002.py!}
|
||||
```
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ You can declare a *CLI parameter* to be a standard Python <a href="https://docs.
|
|||
|
||||
This is what you would do for directory paths, file paths, etc:
|
||||
|
||||
```Python hl_lines="1 6"
|
||||
```Python hl_lines="1 7"
|
||||
{!../docs_src/parameter_types/path/tutorial001.py!}
|
||||
```
|
||||
|
||||
|
|
|
@ -105,13 +105,15 @@ Completion will take effect once you restart the terminal.
|
|||
Let's say you have a script that uses **Typer** in `my_custom_script.py`:
|
||||
|
||||
```Python
|
||||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
app = typer.Typer()
|
||||
|
||||
|
||||
@app.command()
|
||||
def hello(name: str = None):
|
||||
def hello(name: Optional[str] = None):
|
||||
if name:
|
||||
typer.echo(f"Hello {name}")
|
||||
else:
|
||||
|
@ -119,7 +121,7 @@ def hello(name: str = None):
|
|||
|
||||
|
||||
@app.command()
|
||||
def bye(name: str = None):
|
||||
def bye(name: Optional[str] = None):
|
||||
if name:
|
||||
typer.echo(f"Bye {name}")
|
||||
else:
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
import typer
|
||||
|
||||
|
||||
def main(user: List[str] = typer.Option(None)):
|
||||
def main(user: Optional[List[str]] = typer.Option(None)):
|
||||
if not user:
|
||||
typer.echo("No provided users")
|
||||
raise typer.Abort()
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
__version__ = "0.1.0"
|
||||
|
@ -11,7 +13,9 @@ def version_callback(value: bool):
|
|||
|
||||
def main(
|
||||
name: str = typer.Option("World"),
|
||||
version: bool = typer.Option(None, "--version", callback=version_callback),
|
||||
version: Optional[bool] = typer.Option(
|
||||
None, "--version", callback=version_callback
|
||||
),
|
||||
):
|
||||
typer.echo(f"Hello {name}")
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
__version__ = "0.1.0"
|
||||
|
@ -16,7 +18,9 @@ def name_callback(name: str):
|
|||
|
||||
def main(
|
||||
name: str = typer.Option(..., callback=name_callback),
|
||||
version: bool = typer.Option(None, "--version", callback=version_callback),
|
||||
version: Optional[bool] = typer.Option(
|
||||
None, "--version", callback=version_callback
|
||||
),
|
||||
):
|
||||
typer.echo(f"Hello {name}")
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
__version__ = "0.1.0"
|
||||
|
@ -17,7 +19,7 @@ def name_callback(name: str):
|
|||
|
||||
def main(
|
||||
name: str = typer.Option(..., callback=name_callback),
|
||||
version: bool = typer.Option(
|
||||
version: Optional[bool] = typer.Option(
|
||||
None, "--version", callback=version_callback, is_eager=True
|
||||
),
|
||||
):
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
|
||||
def main(accept: bool = typer.Option(None, "--accept/--reject")):
|
||||
def main(accept: Optional[bool] = typer.Option(None, "--accept/--reject")):
|
||||
if accept is None:
|
||||
typer.echo("I don't know what you want yet")
|
||||
elif accept:
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
|
||||
def main(config: Path = typer.Option(None)):
|
||||
def main(config: Optional[Path] = typer.Option(None)):
|
||||
if config is None:
|
||||
typer.echo("No config file")
|
||||
raise typer.Abort()
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
from typing import Optional
|
||||
|
||||
import typer
|
||||
|
||||
app = typer.Typer()
|
||||
|
||||
|
||||
@app.command()
|
||||
def main(name: str, city: str = None):
|
||||
def main(name: str, city: Optional[str] = None):
|
||||
typer.echo(f"Hello {name}")
|
||||
if city:
|
||||
typer.echo(f"Let's have a coffee in {city}")
|
||||
|
|
Loading…
Reference in a new issue