✨ 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
Provenance
- Commit
36e4080- Tree
4bebacf- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
bd7dd4c
41 files changed+927 added−863 removed
modified:docs/tutorial/commands/index.md+39 added-1 removed
74 unmodified lines75 A "decorator" takes the function below and does something with it.76 77 In our case, this decorator tells **Typer** that the function below is a "`command`".78 79-Both ways, with `typer.run()` and creating the explicit application, achieve the same.79+Both ways, with `typer.run()` and creating the explicit application, achieve almost the same.80 81 !!! 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 lines113 ```114 115 </div>116 117+## CLI application completion118+119+There's a little detail that is worth noting here.120+121+To get shell/tab completion, it's necessary to build a package that you and your users can install and **call directly**.122+123+So instead of running a Python script like:124+125+<div class="termy">126+127+```console128+$ python main.py129+130+✨ Some magic here ✨131+```132+133+</div>134+135+...It would be called like:136+137+<div class="termy">138+139+```console140+$ magic-app141+142+✨ Some magic here ✨143+```144+145+</div>146+147+Having a standalone program like that allows setting up shell/tab completion.148+149+The first step to be able to create an installable package like that is to use an explicit `typer.Typer()` app.150+151+Later you can learn all the process to create a standalone CLI application and [Build a Package](../package.md){.internal-link target=_blank}.152+153+But for now, it's just good to know that you are on that path. 😎154+155 ## A CLI application with multiple commands156 157 Coming back to the CLI applications with multiple commands/subcommands, **Typer** allows creating CLI applications with multiple of them.158 71 unmodified linesdeleted:docs/tutorial/options/autocompletion.md-366 removed
1-As you have seen, apps built with **Typer** have completion in your shell that works when you create a Python package or using **Typer CLI**.2-3-It normally completes *CLI options*, *CLI arguments*, and subcommands (that you will learn about later).4-5-But you can also provide auto completion for the **values** of *CLI options* and *CLI arguments*. We will learn about that here.6-7-## Review completion8-9-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}.14-15-Then let's create small example script:16-17-```Python18-{!../docs_src/options/autocompletion/tutorial001.py!}19-```20-21-And let's try it with **Typer CLI** to get completion:22-23-<div class="termy">24-25-```console26-// Hit the TAB key in your keyboard below where you see the: [TAB]27-$ typer ./main.py [TAB][TAB]28-29-// Depending on your terminal/shell you will get some completion like this ✨30-run -- Run the provided Typer app.31-utils -- Extra utility commands for Typer apps.32-33-// Then try with "run" and --34-$ typer ./main.py run --[TAB][TAB]35-36-// You will get completion for --name, depending on your terminal it will look something like this37---name -- The name to say hi to.38-39-// And you can run it as if it was with Python directly40-$ typer ./main.py run --name Camila41-42-Hello Camila43-```44-45-</div>46-47-## 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}:52-53-```Python hl_lines="4 5 10"54-{!../docs_src/options/autocompletion/tutorial002.py!}55-```56-57-We return a `list` of strings from the `complete_name()` function.58-59-And then we get those values when using completion:60-61-<div class="termy">62-63-```console64-$ typer ./main.py run --name [TAB][TAB]65-66-// We get the values returned from the function 🎉67-Camila Carlos Sebastian68-```69-70-</div>71-72-We got the basics working. Now let's improve it.73-74-## Check the incomplete value75-76-Right 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`.77-78-But we can fix that so that it always works correctly.79-80-Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.81-82-Then we can check and return only the values that start with the incomplete value from the command line:83-84-```Python hl_lines="6 7 8 9 10 11"85-{!../docs_src/options/autocompletion/tutorial003.py!}86-```87-88-Now let's try it:89-90-<div class="termy">91-92-```console93-$ typer ./main.py run --name Ca[TAB][TAB]94-95-// We get the values returned from the function that start with Ca 🎉96-Camila Carlos97-```98-99-</div>100-101-Now we are only returning the valid values, that start with `Ca`, we are no longer returning `Sebastian` as a completion option.102-103-!!! tip104- You have to declare the incomplete value of type `str` and that's what you will receive in the function.105-106- 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.107-108- And the same way, you can only return `str`, not `int`, etc.109-110-## Add help to completions111-112-Right now we are returning a `list` of `str`.113-114-But some shells (Zsh, Fish, PowerShell) are capable of showing extra help text for completion.115-116-We can provide that extra help text so that those shells can show it.117-118-In 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.119-120-So, in the end, we return a `list` of `tuples` of `str`:121-122-```Python hl_lines="3 4 5 6 7 10 11 12 13 14 15 16"123-{!../docs_src/options/autocompletion/tutorial004.py!}124-```125-126-!!! tip127- If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.128-129- Click checks specifically for a `tuple` when extracting the help text.130-131- So in the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.132-133-!!! info134- The help text will be visible in Zsh, Fish, and PowerShell.135-136- Bash doesn't support showing the help text, but completion will still work the same.137-138-If you have a shell like Zsh, it would look like:139-140-<div class="termy">141-142-```console143-$ typer ./main.py run --name [TAB][TAB]144-145-// We get the completion items with their help text 🎉146-Camila -- The reader of books.147-Carlos -- The writer of scripts.148-Sebastian -- The type hints guy.149-```150-151-</div>152-153-## Simplify with `yield`154-155-Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.156-157-That 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:158-159-```Python hl_lines="10 11 12 13"160-{!../docs_src/options/autocompletion/tutorial005.py!}161-```162-163-That simplifies our code a bit and works the same.164-165-!!! tip166- If all the `yield` part seems complex for you, don't worry, you can just use the version with the `list` above.167-168- In the end, that's just to save us a couple of lines of code.169-170-!!! 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>.172-173- But each of the elements for completion has to be a `str` or a `tuple` (when containing a help text).174-175-## Access other *CLI parameters* with the Context176-177-Let's say that now we want to modify the program to be able to "say hi" to multiple people at the same time.178-179-So, we will allow multiple `--name` *CLI options*.180-181-!!! tip182- You will learn more about *CLI parameters* with multiple values later in the tutorial.183-184- So, for now, take this as a sneak peek 😉.185-186-For this we use a `List` of `str`:187-188-```Python hl_lines="6 7 8"189-{!../docs_src/options/autocompletion/tutorial006.py!}190-```191-192-And then we can use it like:193-194-<div class="termy">195-196-```console197-$ typer ./main.py run --name Camila --name Sebastian198-199-Hello Camila200-Hello Sebastian201-```202-203-</div>204-205-### Getting completion for multiple values206-207-And 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.208-209-For 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.210-211-But you can access the context by declaring a function parameter of type `typer.Context`.212-213-And from that context you can get the current values for each parameter.214-215-```Python hl_lines="12 13 15"216-{!../docs_src/options/autocompletion/tutorial007.py!}217-```218-219-We are getting the `names` already provided with `--name` in the command line before this completion was triggered.220-221-If 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.222-223-Then, 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`.224-225-And then we `yield` each item that has not been used yet.226-227-Check it:228-229-<div class="termy">230-231-```console232-$ typer ./main.py run --name [TAB][TAB]233-234-// The first time we trigger completion, we get all the names235-Camila -- The reader of books.236-Carlos -- The writer of scripts.237-Sebastian -- The type hints guy.238-239-// Add a name and trigger completion again240-$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]241-242-// Now we get completion only for the names we haven't used 🎉243-Camila -- The reader of books.244-Carlos -- The writer of scripts.245-246-// And if we add another of the available names:247-$ typer ./main.py run --name Sebastian --name Camila --name [TAB][TAB]248-249-// We get completion for the only available one250-Carlos -- The writer of scripts.251-```252-253-</div>254-255-!!! 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.257-258-## Getting the raw *CLI parameters*259-260-You can also get the raw *CLI parameters*, just a `list` of `str` with everything passed in the command line before the incomplete value.261-262-For example, something like `["typer", "main.py", "run", "--name"]`.263-264-!!! tip265- This would be for advanced scenarios, in most use cases you would be better off using the context.266-267- But it's still possible if you need it.268-269-As a simple example, let's show it on the screen before completion.270-271-Because 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.272-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}.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)`.281-282-Using `stderr=True` tells **Rich** that the output should be shown in "standard error".283-284-```Python hl_lines="12 15-16"285-{!../docs_src/options/autocompletion/tutorial008.py!}286-```287-288-!!! 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.290-291-We get all the *CLI parameters* as a raw `list` of `str` by declaring a parameter with type `List[str]`, here it's named `args`.292-293-!!! tip294- Here we name the list of all the raw *CLI parameters* `args` because that's the convention with Click.295-296- But it doesn't contain only *CLI arguments*, it has everything, including *CLI options* and values, as a raw `list` of `str`.297-298-And then we just print it to "standard error".299-300-<div class="termy">301-302-```console303-$ typer ./main.py run --name [TAB][TAB]304-305-// First we see the raw CLI parameters306-['./main.py', 'run', '--name']307-308-// And then we see the actual completion309-Camila -- The reader of books.310-Carlos -- The writer of scripts.311-Sebastian -- The type hints guy.312-```313-314-</div>315-316-!!! tip317- This is a very simple (and quite useless) example, just so you know how it works and that you can use it.318-319- But it's probably useful only in very advanced use cases.320-321-## Getting the Context and the raw *CLI parameters*322-323-Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:324-325-```Python hl_lines="15"326-{!../docs_src/options/autocompletion/tutorial009.py!}327-```328-329-Check it:330-331-<div class="termy">332-333-```console334-$ typer ./main.py run --name [TAB][TAB]335-336-// First we see the raw CLI parameters337-['./main.py', 'run', '--name']338-339-// And then we see the actual completion340-Camila -- The reader of books.341-Carlos -- The writer of scripts.342-Sebastian -- The type hints guy.343-344-$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]345-346-// Again, we see the raw CLI parameters347-['./main.py', 'run', '--name', 'Sebastian', '--name']348-349-// And then we see the rest of the valid completion items350-Camila -- The reader of books.351-Carlos -- The writer of scripts.352-```353-354-</div>355-356-## Types, types everywhere357-358-**Typer** uses the type declarations to detect what it has to provide to your `autocompletion` function.359-360-You can declare function parameters of these types:361-362-* `str`: for the incomplete value.363-* `typer.Context`: for the current context.364-* `List[str]`: for the raw *CLI parameters*.365-366-It 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
1+As you have seen, apps built with **Typer** have completion in your shell that works when you create a Python package or using **Typer CLI**.2+3+It normally completes *CLI options*, *CLI arguments*, and subcommands (that you will learn about later).4+5+But you can also provide auto completion for the **values** of *CLI options* and *CLI arguments*. We will learn about that here.6+7+## Review completion8+9+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}.14+15+Then let's create small example program:16+17+```Python18+{!../docs_src/options_autocompletion/tutorial001.py!}19+```20+21+And let's try it with **Typer CLI** to get completion:22+23+<div class="termy">24+25+```console26+// Hit the TAB key in your keyboard below where you see the: [TAB]27+$ typer ./main.py [TAB][TAB]28+29+// Depending on your terminal/shell you will get some completion like this ✨30+run -- Run the provided Typer app.31+utils -- Extra utility commands for Typer apps.32+33+// Then try with "run" and --34+$ typer ./main.py run --[TAB][TAB]35+36+// You will get completion for --name, depending on your terminal it will look something like this37+--name -- The name to say hi to.38+39+// And you can run it as if it was with Python directly40+$ typer ./main.py run --name Camila41+42+Hello Camila43+```44+45+</div>46+47+## 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}:52+53+```Python hl_lines="4-5 14"54+{!../docs_src/options_autocompletion/tutorial002.py!}55+```56+57+We return a `list` of strings from the `complete_name()` function.58+59+And then we get those values when using completion:60+61+<div class="termy">62+63+```console64+$ typer ./main.py run --name [TAB][TAB]65+66+// We get the values returned from the function 🎉67+Camila Carlos Sebastian68+```69+70+</div>71+72+We got the basics working. Now let's improve it.73+74+## Check the incomplete value75+76+Right 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`.77+78+But we can fix that so that it always works correctly.79+80+Modify the `complete_name()` function to receive a parameter of type `str`, it will contain the incomplete value.81+82+Then we can check and return only the values that start with the incomplete value from the command line:83+84+```Python hl_lines="6-11"85+{!../docs_src/options_autocompletion/tutorial003.py!}86+```87+88+Now let's try it:89+90+<div class="termy">91+92+```console93+$ typer ./main.py run --name Ca[TAB][TAB]94+95+// We get the values returned from the function that start with Ca 🎉96+Camila Carlos97+```98+99+</div>100+101+Now we are only returning the valid values, that start with `Ca`, we are no longer returning `Sebastian` as a completion option.102+103+!!! tip104+ You have to declare the incomplete value of type `str` and that's what you will receive in the function.105+106+ 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.107+108+ And the same way, you can only return `str`, not `int`, etc.109+110+## Add help to completions111+112+Right now we are returning a `list` of `str`.113+114+But some shells (Zsh, Fish, PowerShell) are capable of showing extra help text for completion.115+116+We can provide that extra help text so that those shells can show it.117+118+In 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.119+120+So, in the end, we return a `list` of `tuples` of `str`:121+122+```Python hl_lines="3 4 5 6 7 10 11 12 13 14 15 16"123+{!../docs_src/options_autocompletion/tutorial004.py!}124+```125+126+!!! tip127+ If you want to have help text for each item, make sure each item in the list is a `tuple`. Not a `list`.128+129+ Click checks specifically for a `tuple` when extracting the help text.130+131+ So in the end, the return will be a `list` (or other iterable) of `tuples` of 2 `str`.132+133+!!! info134+ The help text will be visible in Zsh, Fish, and PowerShell.135+136+ Bash doesn't support showing the help text, but completion will still work the same.137+138+If you have a shell like Zsh, it would look like:139+140+<div class="termy">141+142+```console143+$ typer ./main.py run --name [TAB][TAB]144+145+// We get the completion items with their help text 🎉146+Camila -- The reader of books.147+Carlos -- The writer of scripts.148+Sebastian -- The type hints guy.149+```150+151+</div>152+153+## Simplify with `yield`154+155+Instead of creating and returning a list with values (`str` or `tuple`), we can use `yield` with each value that we want in the completion.156+157+That 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:158+159+```Python hl_lines="10 11 12 13"160+{!../docs_src/options_autocompletion/tutorial005.py!}161+```162+163+That simplifies our code a bit and works the same.164+165+!!! tip166+ If all the `yield` part seems complex for you, don't worry, you can just use the version with the `list` above.167+168+ In the end, that's just to save us a couple of lines of code.169+170+!!! 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>.172+173+ But each of the elements for completion has to be a `str` or a `tuple` (when containing a help text).174+175+## Access other *CLI parameters* with the Context176+177+Let's say that now we want to modify the program to be able to "say hi" to multiple people at the same time.178+179+So, we will allow multiple `--name` *CLI options*.180+181+!!! tip182+ You will learn more about *CLI parameters* with multiple values later in the tutorial.183+184+ So, for now, take this as a sneak peek 😉.185+186+For this we use a `List` of `str`:187+188+```Python hl_lines="8-11"189+{!../docs_src/options_autocompletion/tutorial006.py!}190+```191+192+And then we can use it like:193+194+<div class="termy">195+196+```console197+$ typer ./main.py run --name Camila --name Sebastian198+199+Hello Camila200+Hello Sebastian201+```202+203+</div>204+205+### Getting completion for multiple values206+207+And 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.208+209+For 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.210+211+But you can access the context by declaring a function parameter of type `typer.Context`.212+213+And from that context you can get the current values for each parameter.214+215+```Python hl_lines="12 13 15"216+{!../docs_src/options_autocompletion/tutorial007.py!}217+```218+219+We are getting the `names` already provided with `--name` in the command line before this completion was triggered.220+221+If 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.222+223+Then, 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`.224+225+And then we `yield` each item that has not been used yet.226+227+Check it:228+229+<div class="termy">230+231+```console232+$ typer ./main.py run --name [TAB][TAB]233+234+// The first time we trigger completion, we get all the names235+Camila -- The reader of books.236+Carlos -- The writer of scripts.237+Sebastian -- The type hints guy.238+239+// Add a name and trigger completion again240+$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]241+242+// Now we get completion only for the names we haven't used 🎉243+Camila -- The reader of books.244+Carlos -- The writer of scripts.245+246+// And if we add another of the available names:247+$ typer ./main.py run --name Sebastian --name Camila --name [TAB][TAB]248+249+// We get completion for the only available one250+Carlos -- The writer of scripts.251+```252+253+</div>254+255+!!! 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.257+258+## Getting the raw *CLI parameters*259+260+You can also get the raw *CLI parameters*, just a `list` of `str` with everything passed in the command line before the incomplete value.261+262+For example, something like `["typer", "main.py", "run", "--name"]`.263+264+!!! tip265+ This would be for advanced scenarios, in most use cases you would be better off using the context.266+267+ But it's still possible if you need it.268+269+As a simple example, let's show it on the screen before completion.270+271+Because 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.272+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}.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)`.281+282+Using `stderr=True` tells **Rich** that the output should be shown in "standard error".283+284+```Python hl_lines="12 15-16"285+{!../docs_src/options_autocompletion/tutorial008.py!}286+```287+288+!!! 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.290+291+We get all the *CLI parameters* as a raw `list` of `str` by declaring a parameter with type `List[str]`, here it's named `args`.292+293+!!! tip294+ Here we name the list of all the raw *CLI parameters* `args` because that's the convention with Click.295+296+ But it doesn't contain only *CLI arguments*, it has everything, including *CLI options* and values, as a raw `list` of `str`.297+298+And then we just print it to "standard error".299+300+<div class="termy">301+302+```console303+$ typer ./main.py run --name [TAB][TAB]304+305+// First we see the raw CLI parameters306+['./main.py', 'run', '--name']307+308+// And then we see the actual completion309+Camila -- The reader of books.310+Carlos -- The writer of scripts.311+Sebastian -- The type hints guy.312+```313+314+</div>315+316+!!! tip317+ This is a very simple (and quite useless) example, just so you know how it works and that you can use it.318+319+ But it's probably useful only in very advanced use cases.320+321+## Getting the Context and the raw *CLI parameters*322+323+Of course, you can declare everything if you need it, the context, the raw *CLI parameters*, and the incomplete `str`:324+325+```Python hl_lines="15"326+{!../docs_src/options_autocompletion/tutorial009.py!}327+```328+329+Check it:330+331+<div class="termy">332+333+```console334+$ typer ./main.py run --name [TAB][TAB]335+336+// First we see the raw CLI parameters337+['./main.py', 'run', '--name']338+339+// And then we see the actual completion340+Camila -- The reader of books.341+Carlos -- The writer of scripts.342+Sebastian -- The type hints guy.343+344+$ typer ./main.py run --name Sebastian --name Ca[TAB][TAB]345+346+// Again, we see the raw CLI parameters347+['./main.py', 'run', '--name', 'Sebastian', '--name']348+349+// And then we see the rest of the valid completion items350+Camila -- The reader of books.351+Carlos -- The writer of scripts.352+```353+354+</div>355+356+## Types, types everywhere357+358+**Typer** uses the type declarations to detect what it has to provide to your `autocompletion` function.359+360+You can declare function parameters of these types:361+362+* `str`: for the incomplete value.363+* `typer.Context`: for the current context.364+* `List[str]`: for the raw *CLI parameters*.365+366+It 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
1-import typer2-3-4-def main(name: str = typer.Option("World", help="The name to say hi to.")):5- print(f"Hello {name}")6-7-8-if __name__ == "__main__":9- typer.run(main)deleted:docs_src/options/autocompletion/tutorial002.py-17 removed
1-import typer2-3-4-def complete_name():5- return ["Camila", "Carlos", "Sebastian"]6-7-8-def main(9- name: str = typer.Option(10- "World", help="The name to say hi to.", autocompletion=complete_name11- )12-):13- print(f"Hello {name}")14-15-16-if __name__ == "__main__":17- typer.run(main)deleted:docs_src/options/autocompletion/tutorial003.py-23 removed
1-import typer2-3-valid_names = ["Camila", "Carlos", "Sebastian"]4-5-6-def complete_name(incomplete: str):7- completion = []8- for name in valid_names:9- if name.startswith(incomplete):10- completion.append(name)11- return completion12-13-14-def main(15- name: str = typer.Option(16- "World", help="The name to say hi to.", autocompletion=complete_name17- )18-):19- print(f"Hello {name}")20-21-22-if __name__ == "__main__":23- typer.run(main)deleted:docs_src/options/autocompletion/tutorial004.py-28 removed
1-import typer2-3-valid_completion_items = [4- ("Camila", "The reader of books."),5- ("Carlos", "The writer of scripts."),6- ("Sebastian", "The type hints guy."),7-]8-9-10-def 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 completion17-18-19-def main(20- name: str = typer.Option(21- "World", help="The name to say hi to.", autocompletion=complete_name22- )23-):24- print(f"Hello {name}")25-26-27-if __name__ == "__main__":28- typer.run(main)deleted:docs_src/options/autocompletion/tutorial005.py-25 removed
1-import typer2-3-valid_completion_items = [4- ("Camila", "The reader of books."),5- ("Carlos", "The writer of scripts."),6- ("Sebastian", "The type hints guy."),7-]8-9-10-def complete_name(incomplete: str):11- for name, help_text in valid_completion_items:12- if name.startswith(incomplete):13- yield (name, help_text)14-15-16-def main(17- name: str = typer.Option(18- "World", help="The name to say hi to.", autocompletion=complete_name19- )20-):21- print(f"Hello {name}")22-23-24-if __name__ == "__main__":25- typer.run(main)deleted:docs_src/options/autocompletion/tutorial006.py-12 removed
1-from typing import List2-3-import typer4-5-6-def 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}")9-10-11-if __name__ == "__main__":12- typer.run(main)deleted:docs_src/options/autocompletion/tutorial007.py-29 removed
1-from typing import List2-3-import typer4-5-valid_completion_items = [6- ("Camila", "The reader of books."),7- ("Carlos", "The writer of scripts."),8- ("Sebastian", "The type hints guy."),9-]10-11-12-def 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)17-18-19-def 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}")26-27-28-if __name__ == "__main__":29- typer.run(main)deleted:docs_src/options/autocompletion/tutorial008.py-32 removed
1-from typing import List2-3-import typer4-from rich.console import Console5-6-valid_completion_items = [7- ("Camila", "The reader of books."),8- ("Carlos", "The writer of scripts."),9- ("Sebastian", "The type hints guy."),10-]11-12-err_console = Console(stderr=True)13-14-15-def 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)20-21-22-def 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}")29-30-31-if __name__ == "__main__":32- typer.run(main)deleted:docs_src/options/autocompletion/tutorial009.py-33 removed
1-from typing import List2-3-import typer4-from rich.console import Console5-6-valid_completion_items = [7- ("Camila", "The reader of books."),8- ("Carlos", "The writer of scripts."),9- ("Sebastian", "The type hints guy."),10-]11-12-err_console = Console(stderr=True)13-14-15-def 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)21-22-23-def 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}")30-31-32-if __name__ == "__main__":33- typer.run(main)added:docs_src/options_autocompletion/tutorial001.py+12 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command()7+def main(name: str = typer.Option("World", help="The name to say hi to.")):8+ print(f"Hello {name}")9+10+11+if __name__ == "__main__":12+ app()added:docs_src/options_autocompletion/tutorial002.py+21 added
1+import typer2+3+4+def complete_name():5+ return ["Camila", "Carlos", "Sebastian"]6+7+8+app = typer.Typer()9+10+11+@app.command()12+def main(13+ name: str = typer.Option(14+ "World", help="The name to say hi to.", autocompletion=complete_name15+ )16+):17+ print(f"Hello {name}")18+19+20+if __name__ == "__main__":21+ app()added:docs_src/options_autocompletion/tutorial003.py+27 added
1+import typer2+3+valid_names = ["Camila", "Carlos", "Sebastian"]4+5+6+def complete_name(incomplete: str):7+ completion = []8+ for name in valid_names:9+ if name.startswith(incomplete):10+ completion.append(name)11+ return completion12+13+14+app = typer.Typer()15+16+17+@app.command()18+def main(19+ name: str = typer.Option(20+ "World", help="The name to say hi to.", autocompletion=complete_name21+ )22+):23+ print(f"Hello {name}")24+25+26+if __name__ == "__main__":27+ app()added:docs_src/options_autocompletion/tutorial004.py+32 added
1+import typer2+3+valid_completion_items = [4+ ("Camila", "The reader of books."),5+ ("Carlos", "The writer of scripts."),6+ ("Sebastian", "The type hints guy."),7+]8+9+10+def 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 completion17+18+19+app = typer.Typer()20+21+22+@app.command()23+def main(24+ name: str = typer.Option(25+ "World", help="The name to say hi to.", autocompletion=complete_name26+ )27+):28+ print(f"Hello {name}")29+30+31+if __name__ == "__main__":32+ app()added:docs_src/options_autocompletion/tutorial005.py+29 added
1+import typer2+3+valid_completion_items = [4+ ("Camila", "The reader of books."),5+ ("Carlos", "The writer of scripts."),6+ ("Sebastian", "The type hints guy."),7+]8+9+10+def complete_name(incomplete: str):11+ for name, help_text in valid_completion_items:12+ if name.startswith(incomplete):13+ yield (name, help_text)14+15+16+app = typer.Typer()17+18+19+@app.command()20+def main(21+ name: str = typer.Option(22+ "World", help="The name to say hi to.", autocompletion=complete_name23+ )24+):25+ print(f"Hello {name}")26+27+28+if __name__ == "__main__":29+ app()added:docs_src/options_autocompletion/tutorial006.py+15 added
1+from typing import List2+3+import typer4+5+app = typer.Typer()6+7+8+@app.command()9+def 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}")12+13+14+if __name__ == "__main__":15+ app()added:docs_src/options_autocompletion/tutorial007.py+33 added
1+from typing import List2+3+import typer4+5+valid_completion_items = [6+ ("Camila", "The reader of books."),7+ ("Carlos", "The writer of scripts."),8+ ("Sebastian", "The type hints guy."),9+]10+11+12+def 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)17+18+19+app = typer.Typer()20+21+22+@app.command()23+def 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}")30+31+32+if __name__ == "__main__":33+ app()added:docs_src/options_autocompletion/tutorial008.py+36 added
1+from typing import List2+3+import typer4+from rich.console import Console5+6+valid_completion_items = [7+ ("Camila", "The reader of books."),8+ ("Carlos", "The writer of scripts."),9+ ("Sebastian", "The type hints guy."),10+]11+12+err_console = Console(stderr=True)13+14+15+def 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)20+21+22+app = typer.Typer()23+24+25+@app.command()26+def 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}")33+34+35+if __name__ == "__main__":36+ app()added:docs_src/options_autocompletion/tutorial009.py+37 added
1+from typing import List2+3+import typer4+from rich.console import Console5+6+valid_completion_items = [7+ ("Camila", "The reader of books."),8+ ("Carlos", "The writer of scripts."),9+ ("Sebastian", "The type hints guy."),10+]11+12+err_console = Console(stderr=True)13+14+15+def 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)21+22+23+app = typer.Typer()24+25+26+@app.command()27+def 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}")34+35+36+if __name__ == "__main__":37+ app()modified:tests/test_completion/test_completion_install.py+1 added-1 removed
5 unmodified lines6 import shellingham7 import typer8 from typer.testing import CliRunner9 10-from docs_src.first_steps import tutorial001 as mod10+from docs_src.commands.index import tutorial001 as mod11 12 runner = CliRunner()13 app = typer.Typer()14 app.command()(mod.main)143 unmodified linesmodified:tests/test_completion/test_completion_show.py+1 added-1 removed
1 import os2 import subprocess3 4-from docs_src.first_steps import tutorial001 as mod4+from docs_src.commands.index import tutorial001 as mod5 6 7 def test_completion_show_no_shell():8 result = subprocess.run(99 unmodified linesmodified:tests/test_completion/test_completion.py+1 added-1 removed
1 unmodified line2 import subprocess3 import sys4 from pathlib import Path5 6-from docs_src.first_steps import tutorial001 as mod6+from docs_src.commands.index import tutorial001 as mod7 8 9 def test_show_completion():10 result = subprocess.run(165 unmodified linesdeleted:tests/test_tutorial/test_options/test_completion/__init__.py
deleted:tests/test_tutorial/test_options/test_completion/test_tutorial002.py-46 removed
1-import os2-import subprocess3-4-import typer5-from typer.testing import CliRunner6-7-from docs_src.options.autocompletion import tutorial002 as mod8-9-runner = CliRunner()10-11-app = typer.Typer()12-app.command()(mod.main)13-14-15-def 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.stdout31-32-33-def test_1():34- result = runner.invoke(app, ["--name", "Camila"])35- assert result.exit_code == 036- assert "Hello Camila" in result.output37-38-39-def 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.stdoutdeleted:tests/test_tutorial/test_options/test_completion/test_tutorial003.py-46 removed
1-import os2-import subprocess3-4-import typer5-from typer.testing import CliRunner6-7-from docs_src.options.autocompletion import tutorial003 as mod8-9-runner = CliRunner()10-11-app = typer.Typer()12-app.command()(mod.main)13-14-15-def 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.stdout31-32-33-def test_1():34- result = runner.invoke(app, ["--name", "Camila"])35- assert result.exit_code == 036- assert "Hello Camila" in result.output37-38-39-def 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.stdoutdeleted:tests/test_tutorial/test_options/test_completion/test_tutorial004.py-46 removed
1-import os2-import subprocess3-4-import typer5-from typer.testing import CliRunner6-7-from docs_src.options.autocompletion import tutorial004 as mod8-9-runner = CliRunner()10-11-app = typer.Typer()12-app.command()(mod.main)13-14-15-def 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.stdout31-32-33-def test_1():34- result = runner.invoke(app, ["--name", "Camila"])35- assert result.exit_code == 036- assert "Hello Camila" in result.output37-38-39-def 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.stdoutdeleted:tests/test_tutorial/test_options/test_completion/test_tutorial007.py-47 removed
1-import os2-import subprocess3-4-import typer5-from typer.testing import CliRunner6-7-from docs_src.options.autocompletion import tutorial007 as mod8-9-runner = CliRunner()10-11-app = typer.Typer()12-app.command()(mod.main)13-14-15-def 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.stdout31-32-33-def 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.output38-39-40-def 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.stdoutdeleted:tests/test_tutorial/test_options/test_completion/test_tutorial008.py-49 removed
1-import os2-import subprocess3-4-import typer5-from typer.testing import CliRunner6-7-from docs_src.options.autocompletion import tutorial008 as mod8-9-runner = CliRunner()10-11-app = typer.Typer()12-app.command()(mod.main)13-14-15-def 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.stderr33-34-35-def 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.output40-41-42-def 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.stdoutdeleted:tests/test_tutorial/test_options/test_completion/test_tutorial009.py-49 removed
1-import os2-import subprocess3-4-import typer5-from typer.testing import CliRunner6-7-from docs_src.options.autocompletion import tutorial009 as mod8-9-runner = CliRunner()10-11-app = typer.Typer()12-app.command()(mod.main)13-14-15-def 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.stderr33-34-35-def 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.output40-41-42-def 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.stdoutadded:tests/test_tutorial/test_options_autocompletion/__init__.py
added:tests/test_tutorial/test_options_autocompletion/test_tutorial002.py+42 added
1+import os2+import subprocess3+4+from typer.testing import CliRunner5+6+from docs_src.options_autocompletion import tutorial002 as mod7+8+runner = CliRunner()9+10+11+def 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.stdout27+28+29+def test_1():30+ result = runner.invoke(mod.app, ["--name", "Camila"])31+ assert result.exit_code == 032+ assert "Hello Camila" in result.output33+34+35+def 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.stdoutadded:tests/test_tutorial/test_options_autocompletion/test_tutorial003.py+42 added
1+import os2+import subprocess3+4+from typer.testing import CliRunner5+6+from docs_src.options_autocompletion import tutorial003 as mod7+8+runner = CliRunner()9+10+11+def 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.stdout27+28+29+def test_1():30+ result = runner.invoke(mod.app, ["--name", "Camila"])31+ assert result.exit_code == 032+ assert "Hello Camila" in result.output33+34+35+def 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.stdoutadded:tests/test_tutorial/test_options_autocompletion/test_tutorial004.py+42 added
1+import os2+import subprocess3+4+from typer.testing import CliRunner5+6+from docs_src.options_autocompletion import tutorial004 as mod7+8+runner = CliRunner()9+10+11+def 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.stdout27+28+29+def test_1():30+ result = runner.invoke(mod.app, ["--name", "Camila"])31+ assert result.exit_code == 032+ assert "Hello Camila" in result.output33+34+35+def 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.stdoutadded:tests/test_tutorial/test_options_autocompletion/test_tutorial007.py+43 added
1+import os2+import subprocess3+4+from typer.testing import CliRunner5+6+from docs_src.options_autocompletion import tutorial007 as mod7+8+runner = CliRunner()9+10+11+def 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.stdout27+28+29+def 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.output34+35+36+def 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.stdoutadded:tests/test_tutorial/test_options_autocompletion/test_tutorial008.py+45 added
1+import os2+import subprocess3+4+from typer.testing import CliRunner5+6+from docs_src.options_autocompletion import tutorial008 as mod7+8+runner = CliRunner()9+10+11+def 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.stderr29+30+31+def 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.output36+37+38+def 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.stdoutadded:tests/test_tutorial/test_options_autocompletion/test_tutorial009.py+45 added
1+import os2+import subprocess3+4+from typer.testing import CliRunner5+6+from docs_src.options_autocompletion import tutorial009 as mod7+8+runner = CliRunner()9+10+11+def 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.stderr29+30+31+def 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.output36+37+38+def 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.stdoutmodified:typer/main.py+1 added-1 removed
1043 unmodified lines1044 return wrapper1045 1046 1047 def 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 lines40 - 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.md1 unmodified line49 - 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.md75 unmodified linesmodified:pyproject.toml+16 added
75 unmodified lines76 "docs_src/subcommands/tutorial001/main.py",77 "docs_src/subcommands/tutorial003/lands.py",78 "docs_src/subcommands/tutorial003/main.py",79 ]80+81+[tool.pytest.ini_options]82+addopts = [83+ "--strict-config",84+ "--strict-markers",85+]86+xfail_strict = true87+junit_family = "xunit2"88+filterwarnings = [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+]