Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion docs/management-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ A PR should have a specific use case that it is solving.
* If the PR is for a feature, it should have docs.
* Unless it's a feature we want to discourage, like support for a corner case that we don't want users to use.
* The docs should include a source example file, not write Python directly in Markdown.
* If the source example(s) file can have different syntax for Python 3.8, 3.9, 3.10, there should be different versions of the file, and they should be shown in tabs in the docs.
* If the source example(s) file can have different syntax for Python different Python versions, there should be different versions of the file, and they should be shown in tabs in the docs.
* There should be tests testing the source example.
* Before the PR is applied, the new tests should fail.
* After applying the PR, the new tests should pass.
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/app-dir.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

You can get the application directory where you can, for example, save configuration files with `typer.get_app_dir()`:

{* docs_src/app_dir/tutorial001.py hl[12] *}
{* docs_src/app_dir/tutorial001_py39.py hl[12] *}

It will give you a directory for storing configurations appropriate for your CLI program for the current user in each operating system.

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/arguments/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ That way the *CLI argument* will be optional *and also* have a default value.

We can also use `typer.Argument()` to make a *CLI argument* have a default value other than `None`:

{* docs_src/arguments/default/tutorial001_an.py hl[8] *}
{* docs_src/arguments/default/tutorial001_an_py39.py hl[9] *}

/// tip

Expand Down Expand Up @@ -52,7 +52,7 @@ Hello Camila

And we can even make the default value be dynamically generated by passing a function as the `default_factory` argument:

{* docs_src/arguments/default/tutorial002_an.py hl[9:10,14] *}
{* docs_src/arguments/default/tutorial002_an_py39.py hl[9:10,14] *}

In this case, we created the function `get_name` that will just return a random `str` each time.

Expand Down
6 changes: 3 additions & 3 deletions docs/tutorial/arguments/envvar.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You can learn more about environment variables in the [Environment Variables](..

To do that, use the `envvar` parameter for `typer.Argument()`:

{* docs_src/arguments/envvar/tutorial001_an.py hl[8] *}
{* docs_src/arguments/envvar/tutorial001_an_py39.py hl[9] *}

In this case, the *CLI argument* `name` will have a default value of `"World"`, but will also read any value passed to the environment variable `AWESOME_NAME` if no value is provided in the command line:

Expand Down Expand Up @@ -55,7 +55,7 @@ Hello Mr. Czernobog

You are not restricted to a single environment variable, you can declare a list of environment variables that could be used to get a value if it was not passed in the command line:

{* docs_src/arguments/envvar/tutorial002_an.py hl[9] *}
{* docs_src/arguments/envvar/tutorial002_an_py39.py hl[10] *}

Check it:

Expand Down Expand Up @@ -90,7 +90,7 @@ Hello Mr. Anubis

By default, environment variables used will be shown in the help text, but you can disable them with `show_envvar=False`:

{* docs_src/arguments/envvar/tutorial003_an.py hl[10] *}
{* docs_src/arguments/envvar/tutorial003_an_py39.py hl[11] *}

Check it:

Expand Down
18 changes: 9 additions & 9 deletions docs/tutorial/arguments/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ In the *First Steps* section you saw how to add help for a CLI app/command by ad

Here's how that last example looked like:

{* docs_src/first_steps/tutorial006.py *}
{* docs_src/first_steps/tutorial006_py39.py *}

Now that you also know how to use `typer.Argument()`, let's use it to add documentation specific for a *CLI argument*.

## Add a `help` text for a *CLI argument*

You can use the `help` parameter to add a help text for a *CLI argument*:

{* docs_src/arguments/help/tutorial001_an.py hl[8] *}
{* docs_src/arguments/help/tutorial001_an_py39.py hl[9] *}

And it will be used in the automatic `--help` option:

Expand All @@ -37,7 +37,7 @@ Options:

And of course, you can also combine that `help` with the <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>:

{* docs_src/arguments/help/tutorial002_an.py hl[8:11] *}
{* docs_src/arguments/help/tutorial002_an_py39.py hl[9:12] *}

And the `--help` option will combine all the information:

Expand All @@ -64,7 +64,7 @@ Options:

If you have a *CLI argument* with a default value, like `"World"`:

{* docs_src/arguments/help/tutorial003_an.py hl[8] *}
{* docs_src/arguments/help/tutorial003_an_py39.py hl[9] *}

It will show that default value in the help text:

Expand All @@ -89,7 +89,7 @@ Options:

But you can disable that if you want to, with `show_default=False`:

{* docs_src/arguments/help/tutorial004_an.py hl[10] *}
{* docs_src/arguments/help/tutorial004_an_py39.py hl[11] *}

And then it won't show the default value:

Expand Down Expand Up @@ -124,7 +124,7 @@ In **Typer** these default values are shown by default. 👀

You can use the same `show_default` to pass a custom string (instead of a `bool`) to customize the default value to be shown in the help text:

{* docs_src/arguments/help/tutorial005_an.py hl[12] *}
{* docs_src/arguments/help/tutorial005_an_py39.py hl[13] *}

And it will be used in the help text:

Expand Down Expand Up @@ -169,7 +169,7 @@ But you can customize it with the `metavar` parameter for `typer.Argument()`.

For example, let's say you don't want to have the default of `NAME`, you want to have `username`, in lowercase, and you really want ✨ emojis ✨ everywhere:

{* docs_src/arguments/help/tutorial006_an.py hl[8] *}
{* docs_src/arguments/help/tutorial006_an_py39.py hl[9] *}

Now the generated help text will have `✨username✨` instead of `NAME`:

Expand All @@ -195,7 +195,7 @@ You might want to show the help information for *CLI arguments* in different pan

If you have installed Rich as described in the docs for [Printing and Colors](../printing.md){.internal-link target=_blank}, you can set the `rich_help_panel` parameter to the name of the panel where you want this *CLI argument* to be shown:

{* docs_src/arguments/help/tutorial007_an.py hl[11,15] *}
{* docs_src/arguments/help/tutorial007_an_py39.py hl[12,16] *}

Then, if you check the `--help` option, you will see a default panel named "`Arguments`" for the *CLI arguments* that don't have a custom `rich_help_panel`.

Expand Down Expand Up @@ -238,7 +238,7 @@ If you want, you can make a *CLI argument* **not** show up in the `Arguments` se

You will probably not want to do this normally, but it's possible:

{* docs_src/arguments/help/tutorial008_an.py hl[8] *}
{* docs_src/arguments/help/tutorial008_an_py39.py hl[9] *}

Check it:

Expand Down
14 changes: 7 additions & 7 deletions docs/tutorial/arguments/optional.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ __init__.py test_tutorial

In the [First Steps](../first-steps.md#add-a-cli-argument){.internal-link target=_blank} you saw how to add a *CLI argument*:

{* docs_src/first_steps/tutorial002.py hl[4] *}
{* docs_src/first_steps/tutorial002_py39.py hl[4] *}

Now let's see an alternative way to create the same *CLI argument*:

{* docs_src/arguments/optional/tutorial000.py hl[4] *}
{* docs_src/arguments/optional/tutorial000_py39.py hl[4] *}

Or, using an explicit `Typer()` instance creation:

{* docs_src/arguments/optional/tutorial001_an.py hl[8] *}
{* docs_src/arguments/optional/tutorial001_an_py39.py hl[9] *}

/// info

Expand Down Expand Up @@ -114,7 +114,7 @@ Now, finally what we came for, an optional *CLI argument*.

To make a *CLI argument* optional, use `typer.Argument()` and make sure to provide a "default" value, for example `"World"`:

{* docs_src/arguments/optional/tutorial002_an.py hl[8] *}
{* docs_src/arguments/optional/tutorial002_an_py39.py hl[9] *}

Now we have:

Expand Down Expand Up @@ -181,7 +181,7 @@ Notice that "`Camila`" here is an optional *CLI argument*, not a *CLI option*, b

Instead of using `Annotated`, you can use `typer.Argument()` as the default value:

{* docs_src/arguments/optional/tutorial001.py hl[7] *}
{* docs_src/arguments/optional/tutorial001_py39.py hl[7] *}

/// tip

Expand Down Expand Up @@ -215,11 +215,11 @@ If you hadn't seen that `...` before: it is a special single value, it is <a hre

///

{* docs_src/arguments/optional/tutorial003.py hl[7] *}
{* docs_src/arguments/optional/tutorial003_py39.py hl[7] *}

And the same way, you can make it optional by passing a different `default` value, for example `"World"`:

{* docs_src/arguments/optional/tutorial002.py hl[7] *}
{* docs_src/arguments/optional/tutorial002_py39.py hl[7] *}

Because the first parameter passed to `typer.Argument(default="World")` (the new "default" value) is `"World"`, **Typer** knows that this is an **optional** *CLI argument*, if no value is provided when calling it in the command line, it will have that default value of `"World"`.

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/commands/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The same way as with a CLI application with a single command, subcommands (or just "commands") can also have their own *CLI arguments*:

{* docs_src/commands/arguments/tutorial001.py hl[7,12] *}
{* docs_src/commands/arguments/tutorial001_py39.py hl[7,12] *}

<div class="termy">

Expand Down
8 changes: 4 additions & 4 deletions docs/tutorial/commands/callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ But we can use `@app.callback()` for that.

It's very similar to `@app.command()`, but it declares the *CLI parameters* for the main CLI application (before the commands):

{* docs_src/commands/callback/tutorial001.py hl[25,26,27,28,29,30,31,32] *}
{* docs_src/commands/callback/tutorial001_py39.py hl[25,26,27,28,29,30,31,32] *}

Here we create a `callback` with a `--verbose` *CLI option*.

Expand Down Expand Up @@ -79,7 +79,7 @@ Error: No such option: --verbose

It's also possible to add a callback when creating the `typer.Typer()` app:

{* docs_src/commands/callback/tutorial002.py hl[4,5,8] *}
{* docs_src/commands/callback/tutorial002_py39.py hl[4,5,8] *}

That achieves the same as with `@app.callback()`.

Expand All @@ -100,7 +100,7 @@ Creating user: Camila

If you added a callback when creating the `typer.Typer()` app, it's possible to override it with `@app.callback()`:

{* docs_src/commands/callback/tutorial003.py hl[11,12,13] *}
{* docs_src/commands/callback/tutorial003_py39.py hl[11,12,13] *}

Now `new_callback()` will be the one used.

Expand All @@ -124,7 +124,7 @@ You can also add a callback just to add the documentation in the docstring.

It can be convenient especially if you have several lines of text, as the indentation will be automatically handled for you:

{* docs_src/commands/callback/tutorial004.py hl[8,9,10,11,12,13,14,15,16] *}
{* docs_src/commands/callback/tutorial004_py39.py hl[8,9,10,11,12,13,14,15,16] *}

Now the callback will be used mainly to extract the docstring for the help text.

Expand Down
8 changes: 4 additions & 4 deletions docs/tutorial/commands/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For example, let's say that you want to execute some logic in a `Typer` callback

You can get the name of the subcommand from the context:

{* docs_src/commands/context/tutorial001.py hl[17,21] *}
{* docs_src/commands/context/tutorial001_py39.py hl[17,21] *}

Check it:

Expand Down Expand Up @@ -44,7 +44,7 @@ And if no command is provided, the help message is shown.

But we could make it run even without a subcommand with `invoke_without_command=True`:

{* docs_src/commands/context/tutorial002.py hl[16] *}
{* docs_src/commands/context/tutorial002_py39.py hl[16] *}

Check it:

Expand Down Expand Up @@ -74,7 +74,7 @@ For that, we can get the `typer.Context` and check if there's an invoked command

If it's `None`, it means that we are not calling a subcommand but the main program (the callback) directly:

{* docs_src/commands/context/tutorial003.py hl[17,21] *}
{* docs_src/commands/context/tutorial003_py39.py hl[17,21] *}

Check it:

Expand Down Expand Up @@ -105,7 +105,7 @@ For example, you could keep additional *CLI parameters* not declared in your CLI

Then you can access those extra raw *CLI parameters* as a `list` of `str` in `ctx.args`:

{* docs_src/commands/context/tutorial004.py hl[7,9,10] *}
{* docs_src/commands/context/tutorial004_py39.py hl[7,9,10] *}

<div class="termy">

Expand Down
18 changes: 9 additions & 9 deletions docs/tutorial/commands/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The same as before, you can add help for the commands in the docstrings and the

And the `typer.Typer()` application receives a parameter `help` that you can pass with the main help text for your CLI program:

{* docs_src/commands/help/tutorial001_an.py hl[4,9:11,22,26:30,43,47:51,60:62] *}
{* docs_src/commands/help/tutorial001_an_py39.py hl[5,10:12,23,27:31,44,48:52,61:63] *}

Check it:

Expand Down Expand Up @@ -92,7 +92,7 @@ You will also see how to use "Callbacks" later, and those include a way to add t

You will probably be better adding the help text as a docstring to your functions, but if for some reason you wanted to overwrite it, you can use the `help` function argument passed to `@app.command()`:

{* docs_src/commands/help/tutorial002.py hl[6,14] *}
{* docs_src/commands/help/tutorial002_py39.py hl[6,14] *}

Check it:

Expand Down Expand Up @@ -126,7 +126,7 @@ There could be cases where you have a command in your app that you need to depre

You can mark it with the parameter `deprecated=True`:

{* docs_src/commands/help/tutorial003.py hl[14] *}
{* docs_src/commands/help/tutorial003_py39.py hl[14] *}

And when you show the `--help` option you will see it's marked as "`deprecated`":

Expand Down Expand Up @@ -180,7 +180,7 @@ $ python main.py delete --help

As of version 0.20.0, Typer added support for suggesting mistyped command names. This feature is **enabled by default**, but you can disable it with the parameter `suggest_commands=False`:

{* docs_src/commands/index/tutorial005.py hl[3] *}
{* docs_src/commands/index/tutorial005_py39.py hl[3] *}

If a user mistypes a command, they'll see a helpful suggestion:

Expand Down Expand Up @@ -216,7 +216,7 @@ By default, `rich_markup_mode` is `None` if Rich is not installed, and `"rich"`

If you set `rich_markup_mode="rich"` when creating the `typer.Typer()` app, you will be able to use <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">Rich Console Markup</a> in the docstring, and even in the help for the *CLI arguments* and options:

{* docs_src/commands/help/tutorial004_an.py hl[4,10,14:16,21,24,27] *}
{* docs_src/commands/help/tutorial004_an_py39.py hl[5,11,15:17,22,25,28] *}

With that, you can use <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">Rich Console Markup</a> to format the text in the docstring for the command `create`, make the word "`create`" bold and green, and even use an <a href="https://rich.readthedocs.io/en/stable/markup.html#emoji" class="external-link" target="_blank">emoji</a>.

Expand Down Expand Up @@ -279,7 +279,7 @@ $ python main.py delete --help

If you set `rich_markup_mode="markdown"` when creating the `typer.Typer()` app, you will be able to use Markdown in the docstring:

{* docs_src/commands/help/tutorial005_an.py hl[4,9,12:20,25,27:28] *}
{* docs_src/commands/help/tutorial005_an_py39.py hl[5,10,13:21,26,28:29] *}

With that, you can use Markdown to format the text in the docstring for the command `create`, make the word "`create`" bold, show a list of items, and even use an <a href="https://rich.readthedocs.io/en/stable/markup.html#emoji" class="external-link" target="_blank">emoji</a>.

Expand Down Expand Up @@ -355,7 +355,7 @@ If you installed <a href="https://rich.readthedocs.io/" class="external-link" ta

To set the panel for a command you can pass the argument `rich_help_panel` with the name of the panel you want to use:

{* docs_src/commands/help/tutorial006.py hl[22,30,38,46] *}
{* docs_src/commands/help/tutorial006_py39.py hl[22,30,38,46] *}

Commands without a panel will be shown in the default panel `Commands`, and the rest will be shown in the next panels:

Expand Down Expand Up @@ -396,7 +396,7 @@ The same way, you can configure the panels for *CLI arguments* and *CLI options*

And of course, in the same application you can also set the `rich_help_panel` for commands.

{* docs_src/commands/help/tutorial007_an.py hl[15,21,27,37] *}
{* docs_src/commands/help/tutorial007_an_py310.py hl[14,20,26,36] *}

Then if you run the application you will see all the *CLI parameters* in their respective panels.

Expand Down Expand Up @@ -474,7 +474,7 @@ You can see the custom panel for the commands for "`Utils and Configs`".

If you need, you can also add an epilog section to the help of your commands:

{* docs_src/commands/help/tutorial008.py hl[6] *}
{* docs_src/commands/help/tutorial008_py39.py hl[6] *}

And when you check the `--help` option it will look like:

Expand Down
6 changes: 3 additions & 3 deletions docs/tutorial/commands/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ We'll have a command to `create` users and another command to `delete` them.

To begin, let's say it can only create and delete one single predefined user:

{* docs_src/commands/index/tutorial002.py hl[6,11] *}
{* docs_src/commands/index/tutorial002_py39.py hl[6,11] *}

Now we have a CLI application with 2 commands, `create` and `delete`:

Expand Down Expand Up @@ -107,7 +107,7 @@ By default, we need to specify `--help` to get the command's help page.

However, by setting `no_args_is_help=True` when defining the `typer.Typer()` application, the help function will be shown whenever no argument is given:

{* docs_src/commands/index/tutorial003.py hl[3] *}
{* docs_src/commands/index/tutorial003_py39.py hl[3] *}

Now we can run this:

Expand Down Expand Up @@ -138,7 +138,7 @@ Note that by design, **Typer** shows the commands in the order they've been decl

So, if we take our original example, with `create` and `delete` commands, and reverse the order in the Python file:

{* docs_src/commands/index/tutorial004.py hl[7,12] *}
{* docs_src/commands/index/tutorial004_py39.py hl[7,12] *}

Then we will see the `delete` command first in the help output:

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/commands/name.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ And what if you wanted the command to still be named `create`?

For this, you can set the name of the command in the first parameter for the `@app.command()` decorator:

{* docs_src/commands/name/tutorial001.py hl[6,11] *}
{* docs_src/commands/name/tutorial001_py39.py hl[6,11] *}

Now, even though the functions are named `cli_create_user()` and `cli_delete_user()`, the commands will still be named `create` and `delete`:

Expand Down
Loading