Skip to content
Myr

✨ Make `typer.run()` not add completion scripts by default, it only makes sense in installed apps (#488)

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 GitHub · parent bd7dd4c · Browse files · 41 files+927 added−863 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

41 files+927 added−863 removed

  • tests:+262 added−286 removed
  • other:+665 added−577 removed

41 files+927 added−863 removed

modified:docs/tutorial/commands/index.md+39 added-1 removed
74 unmodified lines
75 A "decorator" takes the function below and does something with it.7677 In our case, this decorator tells **Typer** that the function below is a "`command`".7879Both ways, with `typer.run()` and creating the explicit application, achieve the same.79Both ways, with `typer.run()` and creating the explicit application, achieve almost the same.8081!!! tip82 If your use case is solved with just `typer.run()`, that's fine, you don't have to create the explicit `app` and use `@app.command()`, etc.83
29 unmodified lines
113```114115</div>116117## CLI application completion118119There's a little detail that is worth noting here.120121To get shell/tab completion, it's necessary to build a package that you and your users can install and **call directly**.122123So instead of running a Python script like:124125<div class="termy">126127```console128$ python main.py129130✨ Some magic here ✨131```132133</div>134135...It would be called like:136137<div class="termy">138139```console140$ magic-app141142✨ Some magic here ✨143```144145</div>146147Having a standalone program like that allows setting up shell/tab completion.148149The first step to be able to create an installable package like that is to use an explicit `typer.Typer()` app.150151Later you can learn all the process to create a standalone CLI application and [Build a Package](../package.md){.internal-link target=_blank}.152153But for now, it's just good to know that you are on that path. 😎154155## A CLI application with multiple commands156157Coming back to the CLI applications with multiple commands/subcommands, **Typer** allows creating CLI applications with multiple of them.158
71 unmodified lines
deleted:docs/tutorial/options/autocompletion.md-366 removed
1As you have seen, apps built with **Typer** have completion in your shell that works when you create a Python package or using **Typer CLI**.23It normally completes *CLI options*, *CLI arguments*, and subcommands (that you will learn about later).45But you can also provide auto completion for the **values** of *CLI options* and *CLI arguments*. We will learn about that here.67## Review completion89Before checking how to provide custom completions, let's check again how it works.1011After 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).1213To check it quickly without creating a new Python package, install [Typer CLI](../../typer-cli.md){.internal-link target=_blank}.1415Then let's create small example script:1617```Python18{!../docs_src/options/autocompletion/tutorial001.py!}19```2021And let's try it with **Typer CLI** to get completion:2223<div class="termy">2425```console26// Hit the TAB key in your keyboard below where you see the: [TAB]27$ typer ./main.py [TAB][TAB]2829// Depending on your terminal/shell you will get some completion like this ✨30run -- Run the provided Typer app.31utils -- Extra utility commands for Typer apps.3233// Then try with "run" and --34$ typer ./main.py run --[TAB][TAB]3536// You will get completion for --name, depending on your terminal it will look something like this37--name -- The name to say hi to.3839// And you can run it as if it was with Python directly40$ typer ./main.py run --name Camila4142Hello Camila43```4445</div>4647## Custom completion for values4849Right now we get completion for the *CLI option* names, but not for the values.5051We 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}:5253```Python hl_lines="4 5 10"54{!../docs_src/options/autocompletion/tutorial002.py!}55```5657We return a `list` of strings from the `complete_name()` function.5859And then we get those values when using completion:6061<div class="termy">6263```console64$ typer ./main.py run --name [TAB][TAB]6566// We get the values returned from the function 🎉67Camila Carlos Sebastian68```6970</div>7172We got the basics working. Now let's improve it.7374## Check the incomplete value7576Right now, we always return those values, even if users start typing `Sebast` and then hit <kbd>TAB</kbd>, they will also get the completion for `Camila` and `Carlos` (depending on the shell), while we should only get completion for `Sebastian`.7778But we can fix that so that it always works correctly.7980Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.8182Then we can check and return only the values that start with the incomplete value from the command line:8384```Python hl_lines="6 7 8 9 10 11"85{!../docs_src/options/autocompletion/tutorial003.py!}86```8788Now let's try it:8990<div class="termy">9192```console93$ typer ./main.py run --name Ca[TAB][TAB]9495// We get the values returned from the function that start with Ca 🎉96Camila Carlos97```9899</div>100101Now we are only returning the valid values, that start with `Ca`, we are no longer returning `Sebastian` as a completion option.102103!!! tip104 You have to declare the incomplete value of type `str` and that's what you will receive in the function.105106 No matter if the actual value will be an `int`, or something else, when doing completion, you will only get a `str` as the incomplete value.107108 And the same way, you can only return `str`, not `int`, etc.109110## Add help to completions111112Right now we are returning a `list` of `str`.113114But some shells (Zsh, Fish, PowerShell) are capable of showing extra help text for completion.115116We can provide that extra help text so that those shells can show it.117118In the `complete_name()` function, instead of providing one `str` per completion element, we provide a `tuple` with 2 items. The first item is the actual completion string, and the second item is the help text.119120So, in the end, we return a `list` of `tuples` of `str`:121122```Python hl_lines="3 4 5 6 7 10 11 12 13 14 15 16"123{!../docs_src/options/autocompletion/tutorial004.py!}124```125126!!! tip127 If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.128129 Click checks specifically for a `tuple` when extracting the help text.130131 So in the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.132133!!! info134 The help text will be visible in Zsh, Fish, and PowerShell.135136 Bash doesn't support showing the help text, but completion will still work the same.137138If you have a shell like Zsh, it would look like:139140<div class="termy">141142```console143$ typer ./main.py run --name [TAB][TAB]144145// We get the completion items with their help text 🎉146Camila -- The reader of books.147Carlos -- The writer of scripts.148Sebastian -- The type hints guy.149```150151</div>152153## Simplify with `yield`154155Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.156157That way our function will be a <a href="https://docs.python.org/3.8/glossary.html#index-19" class="external-link" target="_blank">generator</a> that **Typer** (actually Click) can iterate:158159```Python hl_lines="10 11 12 13"160{!../docs_src/options/autocompletion/tutorial005.py!}161```162163That simplifies our code a bit and works the same.164165!!! tip166 If all the `yield` part seems complex for you, don't worry, you can just use the version with the `list` above.167168 In the end, that's just to save us a couple of lines of code.169170!!! info171 The function can use `yield`, so it doesn't have to return strictly a `list`, it just has to be <a href="https://docs.python.org/3.8/glossary.html#term-iterable" class="external-link" target="_blank">iterable</a>.172173 But each of the elements for completion has to be a `str` or a `tuple` (when containing a help text).174175## Access other *CLI parameters* with the Context176177Let's say that now we want to modify the program to be able to "say hi" to multiple people at the same time.178179So, we will allow multiple `--name` *CLI options*.180181!!! tip182 You will learn more about *CLI parameters* with multiple values later in the tutorial.183184 So, for now, take this as a sneak peek 😉.185186For this we use a `List` of `str`:187188```Python hl_lines="6 7 8"189{!../docs_src/options/autocompletion/tutorial006.py!}190```191192And then we can use it like:193194<div class="termy">195196```console197$ typer ./main.py run --name Camila --name Sebastian198199Hello Camila200Hello Sebastian201```202203</div>204205### Getting completion for multiple values206207And the same way as before, we want to provide **completion** for those names. But we don't want to provide the **same names** for completion if they were already given in previous parameters.208209For that, we will access and use the "Context". When you create a **Typer** application it uses Click underneath. And every Click application has a special object called a <a href="https://click.palletsprojects.com/en/7.x/commands/#nested-handling-and-contexts" class="external-link" target="_blank">"Context"</a> that is normally hidden.210211But you can access the context by declaring a function parameter of type `typer.Context`.212213And from that context you can get the current values for each parameter.214215```Python hl_lines="12 13 15"216{!../docs_src/options/autocompletion/tutorial007.py!}217```218219We are getting the `names` already provided with `--name` in the command line before this completion was triggered.220221If there's no `--name` in the command line, it will be `None`, so we use `or []` to make sure we have a `list` (even if empty) to check its contents later.222223Then, when we have a completion candidate, we check if each `name` was already provided with `--name` by checking if it's in that list of `names` with `name not in names`.224225And then we `yield` each item that has not been used yet.226227Check it:228229<div class="termy">230231```console232$ typer ./main.py run --name [TAB][TAB]233234// The first time we trigger completion, we get all the names235Camila -- The reader of books.236Carlos -- The writer of scripts.237Sebastian -- The type hints guy.238239// Add a name and trigger completion again240$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]241242// Now we get completion only for the names we haven't used 🎉243Camila -- The reader of books.244Carlos -- The writer of scripts.245246// And if we add another of the available names:247$ typer ./main.py run --name Sebastian --name Camila --name [TAB][TAB]248249// We get completion for the only available one250Carlos -- The writer of scripts.251```252253</div>254255!!! tip256 It's quite possible that if there's only one option left, your shell will complete it right away instead of showing the option with the help text, to save you more typing.257258## Getting the raw *CLI parameters*259260You can also get the raw *CLI parameters*, just a `list` of `str` with everything passed in the command line before the incomplete value.261262For example, something like `["typer", "main.py", "run", "--name"]`.263264!!! tip265 This would be for advanced scenarios, in most use cases you would be better off using the context.266267 But it's still possible if you need it.268269As a simple example, let's show it on the screen before completion.270271Because completion is based on the output printed by your program (handled internally by **Typer**), during completion we can't just print something else as we normally do.272273### Printing to "standard error"274275!!! 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}.277278The completion system only reads from "standard output", so, printing to "standard error" won't break completion. 🚀279280You can print to "standard error" with a **Rich** `Console(stderr=True)`.281282Using `stderr=True` tells **Rich** that the output should be shown in "standard error".283284```Python hl_lines="12 15-16"285{!../docs_src/options/autocompletion/tutorial008.py!}286```287288!!! info289 If you can't install and use Rich, you can also use `print(lastname, file=sys.stderr)` or `typer.echo("some text", err=True)` instead.290291We get all the *CLI parameters* as a raw `list` of `str` by declaring a parameter with type `List[str]`, here it's named `args`.292293!!! tip294 Here we name the list of all the raw *CLI parameters* `args` because that's the convention with Click.295296 But it doesn't contain only *CLI arguments*, it has everything, including *CLI options* and values, as a raw `list` of `str`.297298And then we just print it to "standard error".299300<div class="termy">301302```console303$ typer ./main.py run --name [TAB][TAB]304305// First we see the raw CLI parameters306['./main.py', 'run', '--name']307308// And then we see the actual completion309Camila -- The reader of books.310Carlos -- The writer of scripts.311Sebastian -- The type hints guy.312```313314</div>315316!!! tip317 This is a very simple (and quite useless) example, just so you know how it works and that you can use it.318319 But it's probably useful only in very advanced use cases.320321## Getting the Context and the raw *CLI parameters*322323Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:324325```Python hl_lines="15"326{!../docs_src/options/autocompletion/tutorial009.py!}327```328329Check it:330331<div class="termy">332333```console334$ typer ./main.py run --name [TAB][TAB]335336// First we see the raw CLI parameters337['./main.py', 'run', '--name']338339// And then we see the actual completion340Camila -- The reader of books.341Carlos -- The writer of scripts.342Sebastian -- The type hints guy.343344$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]345346// Again, we see the raw CLI parameters347['./main.py', 'run', '--name', 'Sebastian', '--name']348349// And then we see the rest of the valid completion items350Camila -- The reader of books.351Carlos -- The writer of scripts.352```353354</div>355356## Types, types everywhere357358**Typer** uses the type declarations to detect what it has to provide to your `autocompletion` function.359360You can declare function parameters of these types:361362* `str`: for the incomplete value.363* `typer.Context`: for the current context.364* `List[str]`: for the raw *CLI parameters*.365366It doesn't matter how you name them, in which order, or which ones of the 3 options you declare. It will all "**just work**" ✨
added:docs/tutorial/options-autocompletion.md+366 added
1As you have seen, apps built with **Typer** have completion in your shell that works when you create a Python package or using **Typer CLI**.23It normally completes *CLI options*, *CLI arguments*, and subcommands (that you will learn about later).45But you can also provide auto completion for the **values** of *CLI options* and *CLI arguments*. We will learn about that here.67## Review completion89Before checking how to provide custom completions, let's check again how it works.1011After 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).1213To check it quickly without creating a new Python package, install [Typer CLI](../../typer-cli.md){.internal-link target=_blank}.1415Then let's create small example program:1617```Python18{!../docs_src/options_autocompletion/tutorial001.py!}19```2021And let's try it with **Typer CLI** to get completion:2223<div class="termy">2425```console26// Hit the TAB key in your keyboard below where you see the: [TAB]27$ typer ./main.py [TAB][TAB]2829// Depending on your terminal/shell you will get some completion like this ✨30run -- Run the provided Typer app.31utils -- Extra utility commands for Typer apps.3233// Then try with "run" and --34$ typer ./main.py run --[TAB][TAB]3536// You will get completion for --name, depending on your terminal it will look something like this37--name -- The name to say hi to.3839// And you can run it as if it was with Python directly40$ typer ./main.py run --name Camila4142Hello Camila43```4445</div>4647## Custom completion for values4849Right now we get completion for the *CLI option* names, but not for the values.5051We 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}:5253```Python hl_lines="4-5 14"54{!../docs_src/options_autocompletion/tutorial002.py!}55```5657We return a `list` of strings from the `complete_name()` function.5859And then we get those values when using completion:6061<div class="termy">6263```console64$ typer ./main.py run --name [TAB][TAB]6566// We get the values returned from the function 🎉67Camila Carlos Sebastian68```6970</div>7172We got the basics working. Now let's improve it.7374## Check the incomplete value7576Right now, we always return those values, even if users start typing `Sebast` and then hit <kbd>TAB</kbd>, they will also get the completion for `Camila` and `Carlos` (depending on the shell), while we should only get completion for `Sebastian`.7778But we can fix that so that it always works correctly.7980Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.8182Then we can check and return only the values that start with the incomplete value from the command line:8384```Python hl_lines="6-11"85{!../docs_src/options_autocompletion/tutorial003.py!}86```8788Now let's try it:8990<div class="termy">9192```console93$ typer ./main.py run --name Ca[TAB][TAB]9495// We get the values returned from the function that start with Ca 🎉96Camila Carlos97```9899</div>100101Now we are only returning the valid values, that start with `Ca`, we are no longer returning `Sebastian` as a completion option.102103!!! tip104 You have to declare the incomplete value of type `str` and that's what you will receive in the function.105106 No matter if the actual value will be an `int`, or something else, when doing completion, you will only get a `str` as the incomplete value.107108 And the same way, you can only return `str`, not `int`, etc.109110## Add help to completions111112Right now we are returning a `list` of `str`.113114But some shells (Zsh, Fish, PowerShell) are capable of showing extra help text for completion.115116We can provide that extra help text so that those shells can show it.117118In the `complete_name()` function, instead of providing one `str` per completion element, we provide a `tuple` with 2 items. The first item is the actual completion string, and the second item is the help text.119120So, in the end, we return a `list` of `tuples` of `str`:121122```Python hl_lines="3 4 5 6 7 10 11 12 13 14 15 16"123{!../docs_src/options_autocompletion/tutorial004.py!}124```125126!!! tip127 If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.128129 Click checks specifically for a `tuple` when extracting the help text.130131 So in the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.132133!!! info134 The help text will be visible in Zsh, Fish, and PowerShell.135136 Bash doesn't support showing the help text, but completion will still work the same.137138If you have a shell like Zsh, it would look like:139140<div class="termy">141142```console143$ typer ./main.py run --name [TAB][TAB]144145// We get the completion items with their help text 🎉146Camila -- The reader of books.147Carlos -- The writer of scripts.148Sebastian -- The type hints guy.149```150151</div>152153## Simplify with `yield`154155Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.156157That way our function will be a <a href="https://docs.python.org/3.8/glossary.html#index-19" class="external-link" target="_blank">generator</a> that **Typer** (actually Click) can iterate:158159```Python hl_lines="10 11 12 13"160{!../docs_src/options_autocompletion/tutorial005.py!}161```162163That simplifies our code a bit and works the same.164165!!! tip166 If all the `yield` part seems complex for you, don't worry, you can just use the version with the `list` above.167168 In the end, that's just to save us a couple of lines of code.169170!!! info171 The function can use `yield`, so it doesn't have to return strictly a `list`, it just has to be <a href="https://docs.python.org/3.8/glossary.html#term-iterable" class="external-link" target="_blank">iterable</a>.172173 But each of the elements for completion has to be a `str` or a `tuple` (when containing a help text).174175## Access other *CLI parameters* with the Context176177Let's say that now we want to modify the program to be able to "say hi" to multiple people at the same time.178179So, we will allow multiple `--name` *CLI options*.180181!!! tip182 You will learn more about *CLI parameters* with multiple values later in the tutorial.183184 So, for now, take this as a sneak peek 😉.185186For this we use a `List` of `str`:187188```Python hl_lines="8-11"189{!../docs_src/options_autocompletion/tutorial006.py!}190```191192And then we can use it like:193194<div class="termy">195196```console197$ typer ./main.py run --name Camila --name Sebastian198199Hello Camila200Hello Sebastian201```202203</div>204205### Getting completion for multiple values206207And the same way as before, we want to provide **completion** for those names. But we don't want to provide the **same names** for completion if they were already given in previous parameters.208209For that, we will access and use the "Context". When you create a **Typer** application it uses Click underneath. And every Click application has a special object called a <a href="https://click.palletsprojects.com/en/7.x/commands/#nested-handling-and-contexts" class="external-link" target="_blank">"Context"</a> that is normally hidden.210211But you can access the context by declaring a function parameter of type `typer.Context`.212213And from that context you can get the current values for each parameter.214215```Python hl_lines="12 13 15"216{!../docs_src/options_autocompletion/tutorial007.py!}217```218219We are getting the `names` already provided with `--name` in the command line before this completion was triggered.220221If there's no `--name` in the command line, it will be `None`, so we use `or []` to make sure we have a `list` (even if empty) to check its contents later.222223Then, when we have a completion candidate, we check if each `name` was already provided with `--name` by checking if it's in that list of `names` with `name not in names`.224225And then we `yield` each item that has not been used yet.226227Check it:228229<div class="termy">230231```console232$ typer ./main.py run --name [TAB][TAB]233234// The first time we trigger completion, we get all the names235Camila -- The reader of books.236Carlos -- The writer of scripts.237Sebastian -- The type hints guy.238239// Add a name and trigger completion again240$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]241242// Now we get completion only for the names we haven't used 🎉243Camila -- The reader of books.244Carlos -- The writer of scripts.245246// And if we add another of the available names:247$ typer ./main.py run --name Sebastian --name Camila --name [TAB][TAB]248249// We get completion for the only available one250Carlos -- The writer of scripts.251```252253</div>254255!!! tip256 It's quite possible that if there's only one option left, your shell will complete it right away instead of showing the option with the help text, to save you more typing.257258## Getting the raw *CLI parameters*259260You can also get the raw *CLI parameters*, just a `list` of `str` with everything passed in the command line before the incomplete value.261262For example, something like `["typer", "main.py", "run", "--name"]`.263264!!! tip265 This would be for advanced scenarios, in most use cases you would be better off using the context.266267 But it's still possible if you need it.268269As a simple example, let's show it on the screen before completion.270271Because completion is based on the output printed by your program (handled internally by **Typer**), during completion we can't just print something else as we normally do.272273### Printing to "standard error"274275!!! 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}.277278The completion system only reads from "standard output", so, printing to "standard error" won't break completion. 🚀279280You can print to "standard error" with a **Rich** `Console(stderr=True)`.281282Using `stderr=True` tells **Rich** that the output should be shown in "standard error".283284```Python hl_lines="12 15-16"285{!../docs_src/options_autocompletion/tutorial008.py!}286```287288!!! info289 If you can't install and use Rich, you can also use `print(lastname, file=sys.stderr)` or `typer.echo("some text", err=True)` instead.290291We get all the *CLI parameters* as a raw `list` of `str` by declaring a parameter with type `List[str]`, here it's named `args`.292293!!! tip294 Here we name the list of all the raw *CLI parameters* `args` because that's the convention with Click.295296 But it doesn't contain only *CLI arguments*, it has everything, including *CLI options* and values, as a raw `list` of `str`.297298And then we just print it to "standard error".299300<div class="termy">301302```console303$ typer ./main.py run --name [TAB][TAB]304305// First we see the raw CLI parameters306['./main.py', 'run', '--name']307308// And then we see the actual completion309Camila -- The reader of books.310Carlos -- The writer of scripts.311Sebastian -- The type hints guy.312```313314</div>315316!!! tip317 This is a very simple (and quite useless) example, just so you know how it works and that you can use it.318319 But it's probably useful only in very advanced use cases.320321## Getting the Context and the raw *CLI parameters*322323Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:324325```Python hl_lines="15"326{!../docs_src/options_autocompletion/tutorial009.py!}327```328329Check it:330331<div class="termy">332333```console334$ typer ./main.py run --name [TAB][TAB]335336// First we see the raw CLI parameters337['./main.py', 'run', '--name']338339// And then we see the actual completion340Camila -- The reader of books.341Carlos -- The writer of scripts.342Sebastian -- The type hints guy.343344$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]345346// Again, we see the raw CLI parameters347['./main.py', 'run', '--name', 'Sebastian', '--name']348349// And then we see the rest of the valid completion items350Camila -- The reader of books.351Carlos -- The writer of scripts.352```353354</div>355356## Types, types everywhere357358**Typer** uses the type declarations to detect what it has to provide to your `autocompletion` function.359360You can declare function parameters of these types:361362* `str`: for the incomplete value.363* `typer.Context`: for the current context.364* `List[str]`: for the raw *CLI parameters*.365366It doesn't matter how you name them, in which order, or which ones of the 3 options you declare. It will all "**just work**" ✨
deleted:docs_src/options/autocompletion/tutorial001.py-9 removed
1import typer234def main(name: str = typer.Option("World", help="The name to say hi to.")):5 print(f"Hello {name}")678if __name__ == "__main__":9 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial002.py-17 removed
1import typer234def complete_name():5 return ["Camila", "Carlos", "Sebastian"]678def main(9 name: str = typer.Option(10 "World", help="The name to say hi to.", autocompletion=complete_name11 )12):13 print(f"Hello {name}")141516if __name__ == "__main__":17 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial003.py-23 removed
1import typer23valid_names = ["Camila", "Carlos", "Sebastian"]456def complete_name(incomplete: str):7 completion = []8 for name in valid_names:9 if name.startswith(incomplete):10 completion.append(name)11 return completion121314def main(15 name: str = typer.Option(16 "World", help="The name to say hi to.", autocompletion=complete_name17 )18):19 print(f"Hello {name}")202122if __name__ == "__main__":23 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial004.py-28 removed
1import typer23valid_completion_items = [4 ("Camila", "The reader of books."),5 ("Carlos", "The writer of scripts."),6 ("Sebastian", "The type hints guy."),7]8910def complete_name(incomplete: str):11 completion = []12 for name, help_text in valid_completion_items:13 if name.startswith(incomplete):14 completion_item = (name, help_text)15 completion.append(completion_item)16 return completion171819def main(20 name: str = typer.Option(21 "World", help="The name to say hi to.", autocompletion=complete_name22 )23):24 print(f"Hello {name}")252627if __name__ == "__main__":28 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial005.py-25 removed
1import typer23valid_completion_items = [4 ("Camila", "The reader of books."),5 ("Carlos", "The writer of scripts."),6 ("Sebastian", "The type hints guy."),7]8910def complete_name(incomplete: str):11 for name, help_text in valid_completion_items:12 if name.startswith(incomplete):13 yield (name, help_text)141516def main(17 name: str = typer.Option(18 "World", help="The name to say hi to.", autocompletion=complete_name19 )20):21 print(f"Hello {name}")222324if __name__ == "__main__":25 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial006.py-12 removed
1from typing import List23import typer456def main(name: List[str] = typer.Option(["World"], help="The name to say hi to.")):7 for each_name in name:8 print(f"Hello {each_name}")91011if __name__ == "__main__":12 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial007.py-29 removed
1from typing import List23import typer45valid_completion_items = [6 ("Camila", "The reader of books."),7 ("Carlos", "The writer of scripts."),8 ("Sebastian", "The type hints guy."),9]101112def complete_name(ctx: typer.Context, incomplete: str):13 names = ctx.params.get("name") or []14 for name, help_text in valid_completion_items:15 if name.startswith(incomplete) and name not in names:16 yield (name, help_text)171819def main(20 name: List[str] = typer.Option(21 ["World"], help="The name to say hi to.", autocompletion=complete_name22 )23):24 for n in name:25 print(f"Hello {n}")262728if __name__ == "__main__":29 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial008.py-32 removed
1from typing import List23import typer4from rich.console import Console56valid_completion_items = [7 ("Camila", "The reader of books."),8 ("Carlos", "The writer of scripts."),9 ("Sebastian", "The type hints guy."),10]1112err_console = Console(stderr=True)131415def complete_name(args: List[str], incomplete: str):16 err_console.print(f"{args}")17 for name, help_text in valid_completion_items:18 if name.startswith(incomplete):19 yield (name, help_text)202122def main(23 name: List[str] = typer.Option(24 ["World"], help="The name to say hi to.", autocompletion=complete_name25 )26):27 for n in name:28 print(f"Hello {n}")293031if __name__ == "__main__":32 typer.run(main)
deleted:docs_src/options/autocompletion/tutorial009.py-33 removed
1from typing import List23import typer4from rich.console import Console56valid_completion_items = [7 ("Camila", "The reader of books."),8 ("Carlos", "The writer of scripts."),9 ("Sebastian", "The type hints guy."),10]1112err_console = Console(stderr=True)131415def complete_name(ctx: typer.Context, args: List[str], incomplete: str):16 err_console.print(f"{args}")17 names = ctx.params.get("name") or []18 for name, help_text in valid_completion_items:19 if name.startswith(incomplete) and name not in names:20 yield (name, help_text)212223def main(24 name: List[str] = typer.Option(25 ["World"], help="The name to say hi to.", autocompletion=complete_name26 )27):28 for n in name:29 print(f"Hello {n}")303132if __name__ == "__main__":33 typer.run(main)
added:docs_src/options_autocompletion/tutorial001.py+12 added
1import typer23app = typer.Typer()456@app.command()7def main(name: str = typer.Option("World", help="The name to say hi to.")):8 print(f"Hello {name}")91011if __name__ == "__main__":12 app()
added:docs_src/options_autocompletion/tutorial002.py+21 added
1import typer234def complete_name():5 return ["Camila", "Carlos", "Sebastian"]678app = typer.Typer()91011@app.command()12def main(13 name: str = typer.Option(14 "World", help="The name to say hi to.", autocompletion=complete_name15 )16):17 print(f"Hello {name}")181920if __name__ == "__main__":21 app()
added:docs_src/options_autocompletion/tutorial003.py+27 added
1import typer23valid_names = ["Camila", "Carlos", "Sebastian"]456def complete_name(incomplete: str):7 completion = []8 for name in valid_names:9 if name.startswith(incomplete):10 completion.append(name)11 return completion121314app = typer.Typer()151617@app.command()18def main(19 name: str = typer.Option(20 "World", help="The name to say hi to.", autocompletion=complete_name21 )22):23 print(f"Hello {name}")242526if __name__ == "__main__":27 app()
added:docs_src/options_autocompletion/tutorial004.py+32 added
1import typer23valid_completion_items = [4 ("Camila", "The reader of books."),5 ("Carlos", "The writer of scripts."),6 ("Sebastian", "The type hints guy."),7]8910def complete_name(incomplete: str):11 completion = []12 for name, help_text in valid_completion_items:13 if name.startswith(incomplete):14 completion_item = (name, help_text)15 completion.append(completion_item)16 return completion171819app = typer.Typer()202122@app.command()23def main(24 name: str = typer.Option(25 "World", help="The name to say hi to.", autocompletion=complete_name26 )27):28 print(f"Hello {name}")293031if __name__ == "__main__":32 app()
added:docs_src/options_autocompletion/tutorial005.py+29 added
1import typer23valid_completion_items = [4 ("Camila", "The reader of books."),5 ("Carlos", "The writer of scripts."),6 ("Sebastian", "The type hints guy."),7]8910def complete_name(incomplete: str):11 for name, help_text in valid_completion_items:12 if name.startswith(incomplete):13 yield (name, help_text)141516app = typer.Typer()171819@app.command()20def main(21 name: str = typer.Option(22 "World", help="The name to say hi to.", autocompletion=complete_name23 )24):25 print(f"Hello {name}")262728if __name__ == "__main__":29 app()
added:docs_src/options_autocompletion/tutorial006.py+15 added
1from typing import List23import typer45app = typer.Typer()678@app.command()9def main(name: List[str] = typer.Option(["World"], help="The name to say hi to.")):10 for each_name in name:11 print(f"Hello {each_name}")121314if __name__ == "__main__":15 app()
added:docs_src/options_autocompletion/tutorial007.py+33 added
1from typing import List23import typer45valid_completion_items = [6 ("Camila", "The reader of books."),7 ("Carlos", "The writer of scripts."),8 ("Sebastian", "The type hints guy."),9]101112def complete_name(ctx: typer.Context, incomplete: str):13 names = ctx.params.get("name") or []14 for name, help_text in valid_completion_items:15 if name.startswith(incomplete) and name not in names:16 yield (name, help_text)171819app = typer.Typer()202122@app.command()23def main(24 name: List[str] = typer.Option(25 ["World"], help="The name to say hi to.", autocompletion=complete_name26 )27):28 for n in name:29 print(f"Hello {n}")303132if __name__ == "__main__":33 app()
added:docs_src/options_autocompletion/tutorial008.py+36 added
1from typing import List23import typer4from rich.console import Console56valid_completion_items = [7 ("Camila", "The reader of books."),8 ("Carlos", "The writer of scripts."),9 ("Sebastian", "The type hints guy."),10]1112err_console = Console(stderr=True)131415def complete_name(args: List[str], incomplete: str):16 err_console.print(f"{args}")17 for name, help_text in valid_completion_items:18 if name.startswith(incomplete):19 yield (name, help_text)202122app = typer.Typer()232425@app.command()26def main(27 name: List[str] = typer.Option(28 ["World"], help="The name to say hi to.", autocompletion=complete_name29 )30):31 for n in name:32 print(f"Hello {n}")333435if __name__ == "__main__":36 app()
added:docs_src/options_autocompletion/tutorial009.py+37 added
1from typing import List23import typer4from rich.console import Console56valid_completion_items = [7 ("Camila", "The reader of books."),8 ("Carlos", "The writer of scripts."),9 ("Sebastian", "The type hints guy."),10]1112err_console = Console(stderr=True)131415def complete_name(ctx: typer.Context, args: List[str], incomplete: str):16 err_console.print(f"{args}")17 names = ctx.params.get("name") or []18 for name, help_text in valid_completion_items:19 if name.startswith(incomplete) and name not in names:20 yield (name, help_text)212223app = typer.Typer()242526@app.command()27def main(28 name: List[str] = typer.Option(29 ["World"], help="The name to say hi to.", autocompletion=complete_name30 )31):32 for n in name:33 print(f"Hello {n}")343536if __name__ == "__main__":37 app()
modified:tests/test_completion/test_completion_install.py+1 added-1 removed
5 unmodified lines
6import shellingham7import typer8from typer.testing import CliRunner910from docs_src.first_steps import tutorial001 as mod10from docs_src.commands.index import tutorial001 as mod1112runner = CliRunner()13app = typer.Typer()14app.command()(mod.main)
143 unmodified lines
modified:tests/test_completion/test_completion_show.py+1 added-1 removed
1import os2import subprocess34from docs_src.first_steps import tutorial001 as mod4from docs_src.commands.index import tutorial001 as mod567def test_completion_show_no_shell():8 result = subprocess.run(
99 unmodified lines
modified:tests/test_completion/test_completion.py+1 added-1 removed
1 unmodified line
2import subprocess3import sys4from pathlib import Path56from docs_src.first_steps import tutorial001 as mod6from docs_src.commands.index import tutorial001 as mod789def test_show_completion():10 result = subprocess.run(
165 unmodified lines
deleted:tests/test_tutorial/test_options/test_completion/__init__.py
deleted:tests/test_tutorial/test_options/test_completion/test_tutorial002.py-46 removed
1import os2import subprocess34import typer5from typer.testing import CliRunner67from docs_src.options.autocompletion import tutorial002 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_completion():16 result = subprocess.run(17 ["coverage", "run", mod.__file__, " "],18 stdout=subprocess.PIPE,19 stderr=subprocess.PIPE,20 encoding="utf-8",21 env={22 **os.environ,23 "_TUTORIAL002.PY_COMPLETE": "complete_zsh",24 "_TYPER_COMPLETE_ARGS": "tutorial002.py --name ",25 "_TYPER_COMPLETE_TESTING": "True",26 },27 )28 assert "Camila" in result.stdout29 assert "Carlos" in result.stdout30 assert "Sebastian" in result.stdout313233def test_1():34 result = runner.invoke(app, ["--name", "Camila"])35 assert result.exit_code == 036 assert "Hello Camila" in result.output373839def test_script():40 result = subprocess.run(41 ["coverage", "run", mod.__file__, "--help"],42 stdout=subprocess.PIPE,43 stderr=subprocess.PIPE,44 encoding="utf-8",45 )46 assert "Usage" in result.stdout
deleted:tests/test_tutorial/test_options/test_completion/test_tutorial003.py-46 removed
1import os2import subprocess34import typer5from typer.testing import CliRunner67from docs_src.options.autocompletion import tutorial003 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_completion():16 result = subprocess.run(17 ["coverage", "run", mod.__file__, " "],18 stdout=subprocess.PIPE,19 stderr=subprocess.PIPE,20 encoding="utf-8",21 env={22 **os.environ,23 "_TUTORIAL003.PY_COMPLETE": "complete_zsh",24 "_TYPER_COMPLETE_ARGS": "tutorial003.py --name Seb",25 "_TYPER_COMPLETE_TESTING": "True",26 },27 )28 assert "Camila" not in result.stdout29 assert "Carlos" not in result.stdout30 assert "Sebastian" in result.stdout313233def test_1():34 result = runner.invoke(app, ["--name", "Camila"])35 assert result.exit_code == 036 assert "Hello Camila" in result.output373839def test_script():40 result = subprocess.run(41 ["coverage", "run", mod.__file__, "--help"],42 stdout=subprocess.PIPE,43 stderr=subprocess.PIPE,44 encoding="utf-8",45 )46 assert "Usage" in result.stdout
deleted:tests/test_tutorial/test_options/test_completion/test_tutorial004.py-46 removed
1import os2import subprocess34import typer5from typer.testing import CliRunner67from docs_src.options.autocompletion import tutorial004 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_completion():16 result = subprocess.run(17 ["coverage", "run", mod.__file__, " "],18 stdout=subprocess.PIPE,19 stderr=subprocess.PIPE,20 encoding="utf-8",21 env={22 **os.environ,23 "_TUTORIAL004.PY_COMPLETE": "complete_zsh",24 "_TYPER_COMPLETE_ARGS": "tutorial004.py --name ",25 "_TYPER_COMPLETE_TESTING": "True",26 },27 )28 assert '"Camila":"The reader of books."' in result.stdout29 assert '"Carlos":"The writer of scripts."' in result.stdout30 assert '"Sebastian":"The type hints guy."' in result.stdout313233def test_1():34 result = runner.invoke(app, ["--name", "Camila"])35 assert result.exit_code == 036 assert "Hello Camila" in result.output373839def test_script():40 result = subprocess.run(41 ["coverage", "run", mod.__file__, "--help"],42 stdout=subprocess.PIPE,43 stderr=subprocess.PIPE,44 encoding="utf-8",45 )46 assert "Usage" in result.stdout
deleted:tests/test_tutorial/test_options/test_completion/test_tutorial007.py-47 removed
1import os2import subprocess34import typer5from typer.testing import CliRunner67from docs_src.options.autocompletion import tutorial007 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_completion():16 result = subprocess.run(17 ["coverage", "run", mod.__file__, " "],18 stdout=subprocess.PIPE,19 stderr=subprocess.PIPE,20 encoding="utf-8",21 env={22 **os.environ,23 "_TUTORIAL007.PY_COMPLETE": "complete_zsh",24 "_TYPER_COMPLETE_ARGS": "tutorial007.py --name Sebastian --name ",25 "_TYPER_COMPLETE_TESTING": "True",26 },27 )28 assert '"Camila":"The reader of books."' in result.stdout29 assert '"Carlos":"The writer of scripts."' in result.stdout30 assert '"Sebastian":"The type hints guy."' not in result.stdout313233def test_1():34 result = runner.invoke(app, ["--name", "Camila", "--name", "Sebastian"])35 assert result.exit_code == 036 assert "Hello Camila" in result.output37 assert "Hello Sebastian" in result.output383940def test_script():41 result = subprocess.run(42 ["coverage", "run", mod.__file__, "--help"],43 stdout=subprocess.PIPE,44 stderr=subprocess.PIPE,45 encoding="utf-8",46 )47 assert "Usage" in result.stdout
deleted:tests/test_tutorial/test_options/test_completion/test_tutorial008.py-49 removed
1import os2import subprocess34import typer5from typer.testing import CliRunner67from docs_src.options.autocompletion import tutorial008 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_completion():16 result = subprocess.run(17 ["coverage", "run", mod.__file__, " "],18 stdout=subprocess.PIPE,19 stderr=subprocess.PIPE,20 encoding="utf-8",21 env={22 **os.environ,23 "_TUTORIAL008.PY_COMPLETE": "complete_zsh",24 "_TYPER_COMPLETE_ARGS": "tutorial008.py --name ",25 "_TYPER_COMPLETE_TESTING": "True",26 },27 )28 assert '"Camila":"The reader of books."' in result.stdout29 assert '"Carlos":"The writer of scripts."' in result.stdout30 assert '"Sebastian":"The type hints guy."' in result.stdout31 # TODO: when deprecating Click 7, remove second option32 assert "[]" in result.stderr or "['--name']" in result.stderr333435def test_1():36 result = runner.invoke(app, ["--name", "Camila", "--name", "Sebastian"])37 assert result.exit_code == 038 assert "Hello Camila" in result.output39 assert "Hello Sebastian" in result.output404142def test_script():43 result = subprocess.run(44 ["coverage", "run", mod.__file__, "--help"],45 stdout=subprocess.PIPE,46 stderr=subprocess.PIPE,47 encoding="utf-8",48 )49 assert "Usage" in result.stdout
deleted:tests/test_tutorial/test_options/test_completion/test_tutorial009.py-49 removed
1import os2import subprocess34import typer5from typer.testing import CliRunner67from docs_src.options.autocompletion import tutorial009 as mod89runner = CliRunner()1011app = typer.Typer()12app.command()(mod.main)131415def test_completion():16 result = subprocess.run(17 ["coverage", "run", mod.__file__, " "],18 stdout=subprocess.PIPE,19 stderr=subprocess.PIPE,20 encoding="utf-8",21 env={22 **os.environ,23 "_TUTORIAL009.PY_COMPLETE": "complete_zsh",24 "_TYPER_COMPLETE_ARGS": "tutorial009.py --name Sebastian --name ",25 "_TYPER_COMPLETE_TESTING": "True",26 },27 )28 assert '"Camila":"The reader of books."' in result.stdout29 assert '"Carlos":"The writer of scripts."' in result.stdout30 assert '"Sebastian":"The type hints guy."' not in result.stdout31 # TODO: when deprecating Click 7, remove second option32 assert "[]" in result.stderr or "['--name', 'Sebastian', '--name']" in result.stderr333435def test_1():36 result = runner.invoke(app, ["--name", "Camila", "--name", "Sebastian"])37 assert result.exit_code == 038 assert "Hello Camila" in result.output39 assert "Hello Sebastian" in result.output404142def test_script():43 result = subprocess.run(44 ["coverage", "run", mod.__file__, "--help"],45 stdout=subprocess.PIPE,46 stderr=subprocess.PIPE,47 encoding="utf-8",48 )49 assert "Usage" in result.stdout
added:tests/test_tutorial/test_options_autocompletion/__init__.py
added:tests/test_tutorial/test_options_autocompletion/test_tutorial002.py+42 added
1import os2import subprocess34from typer.testing import CliRunner56from docs_src.options_autocompletion import tutorial002 as mod78runner = CliRunner()91011def test_completion():12 result = subprocess.run(13 ["coverage", "run", mod.__file__, " "],14 stdout=subprocess.PIPE,15 stderr=subprocess.PIPE,16 encoding="utf-8",17 env={18 **os.environ,19 "_TUTORIAL002.PY_COMPLETE": "complete_zsh",20 "_TYPER_COMPLETE_ARGS": "tutorial002.py --name ",21 "_TYPER_COMPLETE_TESTING": "True",22 },23 )24 assert "Camila" in result.stdout25 assert "Carlos" in result.stdout26 assert "Sebastian" in result.stdout272829def test_1():30 result = runner.invoke(mod.app, ["--name", "Camila"])31 assert result.exit_code == 032 assert "Hello Camila" in result.output333435def test_script():36 result = subprocess.run(37 ["coverage", "run", mod.__file__, "--help"],38 stdout=subprocess.PIPE,39 stderr=subprocess.PIPE,40 encoding="utf-8",41 )42 assert "Usage" in result.stdout
added:tests/test_tutorial/test_options_autocompletion/test_tutorial003.py+42 added
1import os2import subprocess34from typer.testing import CliRunner56from docs_src.options_autocompletion import tutorial003 as mod78runner = CliRunner()91011def test_completion():12 result = subprocess.run(13 ["coverage", "run", mod.__file__, " "],14 stdout=subprocess.PIPE,15 stderr=subprocess.PIPE,16 encoding="utf-8",17 env={18 **os.environ,19 "_TUTORIAL003.PY_COMPLETE": "complete_zsh",20 "_TYPER_COMPLETE_ARGS": "tutorial003.py --name Seb",21 "_TYPER_COMPLETE_TESTING": "True",22 },23 )24 assert "Camila" not in result.stdout25 assert "Carlos" not in result.stdout26 assert "Sebastian" in result.stdout272829def test_1():30 result = runner.invoke(mod.app, ["--name", "Camila"])31 assert result.exit_code == 032 assert "Hello Camila" in result.output333435def test_script():36 result = subprocess.run(37 ["coverage", "run", mod.__file__, "--help"],38 stdout=subprocess.PIPE,39 stderr=subprocess.PIPE,40 encoding="utf-8",41 )42 assert "Usage" in result.stdout
added:tests/test_tutorial/test_options_autocompletion/test_tutorial004.py+42 added
1import os2import subprocess34from typer.testing import CliRunner56from docs_src.options_autocompletion import tutorial004 as mod78runner = CliRunner()91011def test_completion():12 result = subprocess.run(13 ["coverage", "run", mod.__file__, " "],14 stdout=subprocess.PIPE,15 stderr=subprocess.PIPE,16 encoding="utf-8",17 env={18 **os.environ,19 "_TUTORIAL004.PY_COMPLETE": "complete_zsh",20 "_TYPER_COMPLETE_ARGS": "tutorial004_aux.py --name ",21 "_TYPER_COMPLETE_TESTING": "True",22 },23 )24 assert '"Camila":"The reader of books."' in result.stdout25 assert '"Carlos":"The writer of scripts."' in result.stdout26 assert '"Sebastian":"The type hints guy."' in result.stdout272829def test_1():30 result = runner.invoke(mod.app, ["--name", "Camila"])31 assert result.exit_code == 032 assert "Hello Camila" in result.output333435def test_script():36 result = subprocess.run(37 ["coverage", "run", mod.__file__, "--help"],38 stdout=subprocess.PIPE,39 stderr=subprocess.PIPE,40 encoding="utf-8",41 )42 assert "Usage" in result.stdout
added:tests/test_tutorial/test_options_autocompletion/test_tutorial007.py+43 added
1import os2import subprocess34from typer.testing import CliRunner56from docs_src.options_autocompletion import tutorial007 as mod78runner = CliRunner()91011def test_completion():12 result = subprocess.run(13 ["coverage", "run", mod.__file__, " "],14 stdout=subprocess.PIPE,15 stderr=subprocess.PIPE,16 encoding="utf-8",17 env={18 **os.environ,19 "_TUTORIAL007.PY_COMPLETE": "complete_zsh",20 "_TYPER_COMPLETE_ARGS": "tutorial007.py --name Sebastian --name ",21 "_TYPER_COMPLETE_TESTING": "True",22 },23 )24 assert '"Camila":"The reader of books."' in result.stdout25 assert '"Carlos":"The writer of scripts."' in result.stdout26 assert '"Sebastian":"The type hints guy."' not in result.stdout272829def test_1():30 result = runner.invoke(mod.app, ["--name", "Camila", "--name", "Sebastian"])31 assert result.exit_code == 032 assert "Hello Camila" in result.output33 assert "Hello Sebastian" in result.output343536def test_script():37 result = subprocess.run(38 ["coverage", "run", mod.__file__, "--help"],39 stdout=subprocess.PIPE,40 stderr=subprocess.PIPE,41 encoding="utf-8",42 )43 assert "Usage" in result.stdout
added:tests/test_tutorial/test_options_autocompletion/test_tutorial008.py+45 added
1import os2import subprocess34from typer.testing import CliRunner56from docs_src.options_autocompletion import tutorial008 as mod78runner = CliRunner()91011def test_completion():12 result = subprocess.run(13 ["coverage", "run", mod.__file__, " "],14 stdout=subprocess.PIPE,15 stderr=subprocess.PIPE,16 encoding="utf-8",17 env={18 **os.environ,19 "_TUTORIAL008.PY_COMPLETE": "complete_zsh",20 "_TYPER_COMPLETE_ARGS": "tutorial008.py --name ",21 "_TYPER_COMPLETE_TESTING": "True",22 },23 )24 assert '"Camila":"The reader of books."' in result.stdout25 assert '"Carlos":"The writer of scripts."' in result.stdout26 assert '"Sebastian":"The type hints guy."' in result.stdout27 # TODO: when deprecating Click 7, remove second option28 assert "[]" in result.stderr or "['--name']" in result.stderr293031def test_1():32 result = runner.invoke(mod.app, ["--name", "Camila", "--name", "Sebastian"])33 assert result.exit_code == 034 assert "Hello Camila" in result.output35 assert "Hello Sebastian" in result.output363738def test_script():39 result = subprocess.run(40 ["coverage", "run", mod.__file__, "--help"],41 stdout=subprocess.PIPE,42 stderr=subprocess.PIPE,43 encoding="utf-8",44 )45 assert "Usage" in result.stdout
added:tests/test_tutorial/test_options_autocompletion/test_tutorial009.py+45 added
1import os2import subprocess34from typer.testing import CliRunner56from docs_src.options_autocompletion import tutorial009 as mod78runner = CliRunner()91011def test_completion():12 result = subprocess.run(13 ["coverage", "run", mod.__file__, " "],14 stdout=subprocess.PIPE,15 stderr=subprocess.PIPE,16 encoding="utf-8",17 env={18 **os.environ,19 "_TUTORIAL009.PY_COMPLETE": "complete_zsh",20 "_TYPER_COMPLETE_ARGS": "tutorial009.py --name Sebastian --name ",21 "_TYPER_COMPLETE_TESTING": "True",22 },23 )24 assert '"Camila":"The reader of books."' in result.stdout25 assert '"Carlos":"The writer of scripts."' in result.stdout26 assert '"Sebastian":"The type hints guy."' not in result.stdout27 # TODO: when deprecating Click 7, remove second option28 assert "[]" in result.stderr or "['--name', 'Sebastian', '--name']" in result.stderr293031def test_1():32 result = runner.invoke(mod.app, ["--name", "Camila", "--name", "Sebastian"])33 assert result.exit_code == 034 assert "Hello Camila" in result.output35 assert "Hello Sebastian" in result.output363738def test_script():39 result = subprocess.run(40 ["coverage", "run", mod.__file__, "--help"],41 stdout=subprocess.PIPE,42 stderr=subprocess.PIPE,43 encoding="utf-8",44 )45 assert "Usage" in result.stdout
modified:typer/main.py+1 added-1 removed
1043 unmodified lines
1044 return wrapper104510461047def run(function: Callable[..., Any]) -> None:1048 app = Typer()1048 app = Typer(add_completion=False)1049 app.command()(function)1050 app()
modified:mkdocs.yml+1 added-1 removed
39 unmodified lines
40 - Password CLI Option and Confirmation Prompt: tutorial/options/password.md41 - CLI Option Name: tutorial/options/name.md42 - CLI Option Callback and Context: tutorial/options/callback-and-context.md43 - Version CLI Option, is_eager: tutorial/options/version.md44 - CLI Option autocompletion: tutorial/options/autocompletion.md44 - Commands:45 - Commands Intro: tutorial/commands/index.md46 - Command CLI Arguments: tutorial/commands/arguments.md47 - Command CLI Options: tutorial/commands/options.md
1 unmodified line
49 - Custom Command Name: tutorial/commands/name.md50 - Typer Callback: tutorial/commands/callback.md51 - One or Multiple Commands: tutorial/commands/one-or-multiple.md52 - Using the Context: tutorial/commands/context.md53 - CLI Option autocompletion: tutorial/options-autocompletion.md54 - CLI Parameter Types:55 - CLI Parameter Types Intro: tutorial/parameter-types/index.md56 - Number: tutorial/parameter-types/number.md57 - Boolean CLI Options: tutorial/parameter-types/bool.md
75 unmodified lines
modified:pyproject.toml+16 added
75 unmodified lines
76 "docs_src/subcommands/tutorial001/main.py",77 "docs_src/subcommands/tutorial003/lands.py",78 "docs_src/subcommands/tutorial003/main.py",79 ]8081[tool.pytest.ini_options]82addopts = [83 "--strict-config",84 "--strict-markers",85]86xfail_strict = true87junit_family = "xunit2"88filterwarnings = [89 "error",90 # TODO: until I refactor completion to use the new shell_complete91 "ignore:'autocompletion' is renamed to 'shell_complete'. The old name is deprecated and will be removed in Click 8.1. See the docs about 'Parameter' for information about new behavior.:DeprecationWarning:typer",92 'ignore:starlette.middleware.wsgi is deprecated and will be removed in a future release\..*:DeprecationWarning:starlette',93 # For pytest-xdist94 'ignore::DeprecationWarning:xdist',95]