📝 Tweak docs, Custom Types path, main page and READAME colors, broken links (#588)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* 📝 Tweak name for Custom Types * ✏️ Fix broken internal links in CLI Option autocompletion * 📝 Update main page docs, remove colors as that renders badly in README * 📝 Update README, sync with index.md, remove colors for GitHub and PyPI
authored by Sebastián Ramírez · committed by GitHub · parent 27d0fde · Browse files · 6 files·+131 added−149 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+131 added−149 removed
- other:+131 added−149 removed
Provenance
- Commit
e7c3467- Tree
1102fc5- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
27d0fde
6 files changed+131 added−149 removed
deleted:docs/tutorial/parameter-types/custom_types.md-27 removed
123456789101112131415161718192021222324252627You can easily use your own custom types in your **Typer** applications.
The way to do it is by providing a way to <abbr title="convert from some plain format, like the input text in the CLI, into Python objects">parse</abbr> input into your own types.
There are two ways to achieve this:
* Adding a type `parser`* Expanding Click's custom types
## Type Parser
`typer.Argument` and `typer.Option` can create custom parameter types with a `parser` <abbr title="something that can be called like a function">callable</abbr>.
```Python hl_lines="12-13 17 18"{!../docs_src/parameter_types/custom_types/tutorial001.py!}```
The function (or callable) that you pass to the parameter `parser` will receive the input value as a string and should return the parsed value with your own custom type.
## Click Custom Type
If you already have a <a href="https://click.palletsprojects.com/en/8.1.x/parameters/#implementing-custom-types" class="external-link" target="_blank">Click Custom Type</a>, you can use it in `typer.Argument()` and `typer.Option()` with the `click_type` parameter.
```Python hl_lines="13-17 21 22"{!../docs_src/parameter_types/custom_types/tutorial002.py!}```added:docs/tutorial/parameter-types/custom-types.md+27 added
123456789101112131415161718192021222324252627You can easily use your own custom types in your **Typer** applications.
The way to do it is by providing a way to <abbr title="convert from some plain format, like the input text in the CLI, into Python objects">parse</abbr> input into your own types.
There are two ways to achieve this:
* Adding a type `parser`* Expanding Click's custom types
## Type Parser
`typer.Argument` and `typer.Option` can create custom parameter types with a `parser` <abbr title="something that can be called like a function">callable</abbr>.
```Python hl_lines="12-13 17 18"{!../docs_src/parameter_types/custom_types/tutorial001.py!}```
The function (or callable) that you pass to the parameter `parser` will receive the input value as a string and should return the parsed value with your own custom type.
## Click Custom Type
If you already have a <a href="https://click.palletsprojects.com/en/8.1.x/parameters/#implementing-custom-types" class="external-link" target="_blank">Click Custom Type</a>, you can use it in `typer.Argument()` and `typer.Option()` with the `click_type` parameter.
```Python hl_lines="13-17 21 22"{!../docs_src/parameter_types/custom_types/tutorial002.py!}```modified:docs/tutorial/options-autocompletion.md+3 added-3 removed
8 unmodified lines9 Before checking how to provide custom completions, let's check again how it works.10 11 After installing completion (for your own Python package or for **Typer CLI**), when you use your CLI program and start adding a *CLI option* with `--` an then hit <kbd>TAB</kbd>, your shell will show you the available *CLI options* (the same for *CLI arguments*, etc).12 13-To check it quickly without creating a new Python package, install [Typer CLI](../../typer-cli.md){.internal-link target=_blank}.13+To check it quickly without creating a new Python package, install [Typer CLI](../typer-cli.md){.internal-link target=_blank}.14 15 Then let's create small example program:16 17 ```Python29 unmodified lines47 ## Custom completion for values48 49 Right now we get completion for the *CLI option* names, but not for the values.50 51-We can provide completion for the values creating an `autocompletion` function, similar to the `callback` functions from [CLI Option Callback and Context](./callback-and-context.md){.internal-link target=_blank}:51+We can provide completion for the values creating an `autocompletion` function, similar to the `callback` functions from [CLI Option Callback and Context](./options/callback-and-context.md){.internal-link target=_blank}:52 53 ```Python hl_lines="4-5 14"54 {!../docs_src/options_autocompletion/tutorial002.py!}55 ```216 unmodified lines272 273 ### Printing to "standard error"274 275 !!! tip276- If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](../printing.md#standard-output-and-standard-error){.internal-link target=_blank}.276+ If you need a refresher about what is "standard output" and "standard error" check the section in [Printing and Colors: "Standard Output" and "Standard Error"](./printing.md#standard-output-and-standard-error){.internal-link target=_blank}.277 278 The completion system only reads from "standard output", so, printing to "standard error" won't break completion. 🚀279 280 You can print to "standard error" with a **Rich** `Console(stderr=True)`.86 unmodified linesmodified:docs/index.md+50 added-59 removed
91 unmodified lines92 // Run your application93 $ python main.py94 95 // You get a nice error, you are missing NAME96-<font color="#F4BF75">Usage: </font>main.py [OPTIONS] NAME97-<font color="#A5A5A1">Try </font><font color="#44919F">'main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">'</font><font color="#A5A5A1"> for help.</font>98-<font color="#F92672">╭─ Error ───────────────────────────────────────────╮</font>99-<font color="#F92672">│</font> Missing argument 'NAME'. <font color="#F92672">│</font>100-<font color="#F92672">╰───────────────────────────────────────────────────╯</font>96+Usage: main.py [OPTIONS] NAME97+Try 'main.py --help' for help.98+╭─ Error ───────────────────────────────────────────╮99+│ Missing argument 'NAME'. │100+╰───────────────────────────────────────────────────╯101 102 103 // You get a --help for free104 $ python main.py --help105 106-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] NAME </b>107-<b> </b>108-<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>109-<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │110-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>111-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>112-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font> Install completion │113-<font color="#A5A5A1">│ for the current │</font>114-<font color="#A5A5A1">│ shell. │</font>115-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for │116-<font color="#A5A5A1">│ the current shell, │</font>117-<font color="#A5A5A1">│ to copy it or │</font>118-<font color="#A5A5A1">│ customize the │</font>119-<font color="#A5A5A1">│ installation. │</font>120-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │121-<font color="#A5A5A1">│ and exit. │</font>122-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>106+Usage: main.py [OPTIONS] NAME107 124-// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion108+╭─ Arguments ───────────────────────────────────────╮109+│ * name TEXT [default: None] [required] |110+╰───────────────────────────────────────────────────╯111+╭─ Options ─────────────────────────────────────────╮112+│ --help Show this message and exit. │113+╰───────────────────────────────────────────────────╯114 115 // Now pass the NAME argument116 $ python main.py Camila117 56 unmodified lines174 175 ```console176 $ python main.py --help177 189-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] COMMAND [ARGS]... </b>190-<b> </b>191-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>192-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font> Install completion │193-<font color="#A5A5A1">│ for the current │</font>194-<font color="#A5A5A1">│ shell. │</font>195-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for │196-<font color="#A5A5A1">│ the current shell, │</font>197-<font color="#A5A5A1">│ to copy it or │</font>198-<font color="#A5A5A1">│ customize the │</font>199-<font color="#A5A5A1">│ installation. │</font>200-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │201-<font color="#A5A5A1">│ and exit. │</font>202-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>203-<font color="#A5A5A1">╭─ Commands ────────────────────────────────────────╮</font>204-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>goodbye </b></font> │205-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>hello </b></font> │206-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>178+ Usage: main.py [OPTIONS] COMMAND [ARGS]...179 180+╭─ Options ─────────────────────────────────────────╮181+│ --install-completion Install completion │182+│ for the current │183+│ shell. │184+│ --show-completion Show completion for │185+│ the current shell, │186+│ to copy it or │187+│ customize the │188+│ installation. │189+│ --help Show this message │190+│ and exit. │191+╰───────────────────────────────────────────────────╯192+╭─ Commands ────────────────────────────────────────╮193+│ goodbye │194+│ hello │195+╰───────────────────────────────────────────────────╯196+197+// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion198+199 // You have 2 subcommands (the 2 functions): goodbye and hello200 ```201 202 </div>4 unmodified lines207 208 ```console209 $ python main.py hello --help210 220-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py hello [OPTIONS] NAME </b>221-<b> </b>222-<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>223-<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │224-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>225-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>226-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │227-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>211+ Usage: main.py hello [OPTIONS] NAME212+213+╭─ Arguments ───────────────────────────────────────╮214+│ * name TEXT [default: None] [required] │215+╰───────────────────────────────────────────────────╯216+╭─ Options ─────────────────────────────────────────╮217+│ --help Show this message and exit. │218+╰───────────────────────────────────────────────────╯219 ```220 221 </div>222 3 unmodified lines226 227 ```console228 $ python main.py goodbye --help229 239-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py goodbye [OPTIONS] NAME </b>240-<b> </b>241-<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>242-<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │243-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>244-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>245-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--formal</b></font> <font color="#AE81FF"><b>--no-formal</b></font> [default: no-formal] │246-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │247-<font color="#A5A5A1">│ and exit. │</font>248-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>230+ Usage: main.py goodbye [OPTIONS] NAME231 232+╭─ Arguments ───────────────────────────────────────╮233+│ * name TEXT [default: None] [required] │234+╰───────────────────────────────────────────────────╯235+╭─ Options ─────────────────────────────────────────╮236+│ --formal --no-formal [default: no-formal] │237+│ --help Show this message │238+│ and exit. │239+╰───────────────────────────────────────────────────╯240+241 // Automatic --formal and --no-formal for the bool option 🎉242 ```243 244 </div>73 unmodified linesmodified:mkdocs.yml+1 added-1 removed
58 unmodified lines5960616263636465666766 unmodified lines58 unmodified lines - DateTime: tutorial/parameter-types/datetime.md - Enum - Choices: tutorial/parameter-types/enum.md - Path: tutorial/parameter-types/path.md - File: tutorial/parameter-types/file.md - Custom Types: tutorial/parameter-types/custom_types.md - Custom Types: tutorial/parameter-types/custom-types.md - SubCommands - Command Groups: - SubCommands - Command Groups - Intro: tutorial/subcommands/index.md - Add Typer: tutorial/subcommands/add-typer.md - SubCommands in a Single File: tutorial/subcommands/single-file.md66 unmodified linesmodified:README.md+50 added-59 removed
91 unmodified lines92 // Run your application93 $ python main.py94 95 // You get a nice error, you are missing NAME96-<font color="#F4BF75">Usage: </font>main.py [OPTIONS] NAME97-<font color="#A5A5A1">Try </font><font color="#44919F">'main.py </font><font color="#44919F"><b>--help</b></font><font color="#44919F">'</font><font color="#A5A5A1"> for help.</font>98-<font color="#F92672">╭─ Error ───────────────────────────────────────────╮</font>99-<font color="#F92672">│</font> Missing argument 'NAME'. <font color="#F92672">│</font>100-<font color="#F92672">╰───────────────────────────────────────────────────╯</font>96+Usage: main.py [OPTIONS] NAME97+Try 'main.py --help' for help.98+╭─ Error ───────────────────────────────────────────╮99+│ Missing argument 'NAME'. │100+╰───────────────────────────────────────────────────╯101 102 103 // You get a --help for free104 $ python main.py --help105 106-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] NAME </b>107-<b> </b>108-<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>109-<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │110-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>111-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>112-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font> Install completion │113-<font color="#A5A5A1">│ for the current │</font>114-<font color="#A5A5A1">│ shell. │</font>115-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for │116-<font color="#A5A5A1">│ the current shell, │</font>117-<font color="#A5A5A1">│ to copy it or │</font>118-<font color="#A5A5A1">│ customize the │</font>119-<font color="#A5A5A1">│ installation. │</font>120-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │121-<font color="#A5A5A1">│ and exit. │</font>122-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>106+Usage: main.py [OPTIONS] NAME107 124-// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion108+╭─ Arguments ───────────────────────────────────────╮109+│ * name TEXT [default: None] [required] |110+╰───────────────────────────────────────────────────╯111+╭─ Options ─────────────────────────────────────────╮112+│ --help Show this message and exit. │113+╰───────────────────────────────────────────────────╯114 115 // Now pass the NAME argument116 $ python main.py Camila117 56 unmodified lines174 175 ```console176 $ python main.py --help177 189-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] COMMAND [ARGS]... </b>190-<b> </b>191-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>192-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--install-completion</b></font> Install completion │193-<font color="#A5A5A1">│ for the current │</font>194-<font color="#A5A5A1">│ shell. │</font>195-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--show-completion</b></font> Show completion for │196-<font color="#A5A5A1">│ the current shell, │</font>197-<font color="#A5A5A1">│ to copy it or │</font>198-<font color="#A5A5A1">│ customize the │</font>199-<font color="#A5A5A1">│ installation. │</font>200-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │201-<font color="#A5A5A1">│ and exit. │</font>202-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>203-<font color="#A5A5A1">╭─ Commands ────────────────────────────────────────╮</font>204-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>goodbye </b></font> │205-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>hello </b></font> │206-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>178+ Usage: main.py [OPTIONS] COMMAND [ARGS]...179 180+╭─ Options ─────────────────────────────────────────╮181+│ --install-completion Install completion │182+│ for the current │183+│ shell. │184+│ --show-completion Show completion for │185+│ the current shell, │186+│ to copy it or │187+│ customize the │188+│ installation. │189+│ --help Show this message │190+│ and exit. │191+╰───────────────────────────────────────────────────╯192+╭─ Commands ────────────────────────────────────────╮193+│ goodbye │194+│ hello │195+╰───────────────────────────────────────────────────╯196+197+// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion198+199 // You have 2 subcommands (the 2 functions): goodbye and hello200 ```201 202 </div>4 unmodified lines207 208 ```console209 $ python main.py hello --help210 220-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py hello [OPTIONS] NAME </b>221-<b> </b>222-<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>223-<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │224-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>225-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>226-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. │227-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>211+ Usage: main.py hello [OPTIONS] NAME212+213+╭─ Arguments ───────────────────────────────────────╮214+│ * name TEXT [default: None] [required] │215+╰───────────────────────────────────────────────────╯216+╭─ Options ─────────────────────────────────────────╮217+│ --help Show this message and exit. │218+╰───────────────────────────────────────────────────╯219 ```220 221 </div>222 3 unmodified lines226 227 ```console228 $ python main.py goodbye --help229 239-<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py goodbye [OPTIONS] NAME </b>240-<b> </b>241-<font color="#A5A5A1">╭─ Arguments ───────────────────────────────────────╮</font>242-<font color="#A5A5A1">│ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> [default: None] <font color="#A6194C">[required]</font> │243-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>244-<font color="#A5A5A1">╭─ Options ─────────────────────────────────────────╮</font>245-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--formal</b></font> <font color="#AE81FF"><b>--no-formal</b></font> [default: no-formal] │246-<font color="#A5A5A1">│ </font><font color="#A1EFE4"><b>--help</b></font> Show this message │247-<font color="#A5A5A1">│ and exit. │</font>248-<font color="#A5A5A1">╰───────────────────────────────────────────────────╯</font>230+ Usage: main.py goodbye [OPTIONS] NAME231 232+╭─ Arguments ───────────────────────────────────────╮233+│ * name TEXT [default: None] [required] │234+╰───────────────────────────────────────────────────╯235+╭─ Options ─────────────────────────────────────────╮236+│ --formal --no-formal [default: no-formal] │237+│ --help Show this message │238+│ and exit. │239+╰───────────────────────────────────────────────────╯240+241 // Automatic --formal and --no-formal for the bool option 🎉242 ```243 244 </div>73 unmodified lines