File: context.md

package info (click to toggle)
typer 0.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,688 kB
  • sloc: python: 16,702; javascript: 280; sh: 28; makefile: 27
file content (127 lines) | stat: -rw-r--r-- 3,483 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Using 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/8.1.x/commands/#nested-handling-and-contexts" class="external-link" target="_blank">"Context"</a> that is normally hidden.

But you can access the context by declaring a function parameter of type `typer.Context`.

You might have read it in [CLI Option Callback and Context](../options/callback-and-context.md){.internal-link target=_blank}.

The same way, in commands or in the main `Typer` callback you can access the context by declaring a function parameter of type `typer.Context`.

## Getting the context

For example, let's say that you want to execute some logic in a `Typer` callback depending on the subcommand that is being called.

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

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

Check it:

<div class="termy">

```console
$ python main.py create Camila

// We get the message from the callback
About to execute command: create
Creating user: Camila

$ python main.py delete Camila

// We get the message from the callback, this time with delete
About to execute command: delete
Deleting user: Camila
```

</div>

## Executable callback

By default, the callback is only executed right before executing a command.

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] *}

Check it:

<div class="termy">

```console
$ python main.py

// The callback is executed, we don't get the default help message
Initializing database

// Try with a command
$ python main.py create Camila

// The callback is still executed
Initializing database
Creating user: Camila
```

</div>

## Exclusive executable callback

We might not want the callback to be executed if there's already other command that will be executed.

For that, we can get the `typer.Context` and check if there's an invoked command in `ctx.invoked_subcommand`.

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] *}

Check it:

<div class="termy">

```console
$ python main.py

// The callback is executed
Initializing database

// Check it with a subcommand
$ python main.py create Camila

// This time the callback is not executed
Creating user: Camila
```

</div>

## Configuring the context

You can pass configurations for the context when creating a command or callback.

To read more about the available configurations check the docs for <a href="https://click.palletsprojects.com/en/7.x/api/#context" class="external-link" target="_blank">Click's `Context`</a>.

For example, you could keep additional *CLI parameters* not declared in your CLI program with `ignore_unknown_options` and `allow_extra_args`.

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] *}

<div class="termy">

```console
$ python main.py --name Camila --city Berlin

Got extra arg: --name
Got extra arg: Camila
Got extra arg: --city
Got extra arg: Berlin
```

</div>

/// tip

Notice that it saves all the extra *CLI parameters* as a raw `list` of `str`, including the *CLI option* names and values, everything together.

///