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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
# CLI Arguments with Help
In the *First Steps* section you saw how to add help for a CLI app/command by adding it to a function's <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>.
Here's how that last example looked like:
{* docs_src/first_steps/tutorial006.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[5] *}
And it will be used in the automatic `--help` option:
<div class="termy">
```console
$ python main.py --help
// Check the section with Arguments below ๐
Usage: main.py [OPTIONS] NAME
Arguments:
NAME The name of the user to greet [required]
Options:
--help Show this message and exit.
```
</div>
## Combine help text and docstrings
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[5:8] *}
And the `--help` option will combine all the information:
<div class="termy">
```console
$ python main.py --help
// Notice that we have the help text from the docstring and also the Arguments ๐
Usage: main.py [OPTIONS] NAME
Say hi to NAME very gently, like Dirk.
Arguments:
NAME The name of the user to greet [required]
Options:
--help Show this message and exit.
```
</div>
## Help with defaults
If you have a *CLI argument* with a default value, like `"World"`:
{* docs_src/arguments/help/tutorial003_an.py hl[5] *}
It will show that default value in the help text:
<div class="termy">
```console
$ python main.py --help
// Notice the [default: World] ๐
Usage: main.py [OPTIONS] [NAME]
Say hi to NAME very gently, like Dirk.
Arguments:
[NAME] Who to greet [default: World]
Options:
--help Show this message and exit.
```
</div>
But you can disable that if you want to, with `show_default=False`:
{* docs_src/arguments/help/tutorial004_an.py hl[7] *}
And then it won't show the default value:
<div class="termy">
```console
$ python main.py --help
// Notice the there's no [default: World] now ๐ฅ
Usage: main.py [OPTIONS] [NAME]
Say hi to NAME very gently, like Dirk.
Arguments:
[NAME] Who to greet
Options:
--help Show this message and exit.
```
</div>
/// note | Technical Details
In Click applications the default values are hidden by default. ๐
In **Typer** these default values are shown by default. ๐
///
## Custom default string
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[9] *}
And it will be used in the help text:
<div class="termy">
```console
$ python main.py --help
Usage: main.py [OPTIONS] [NAME]
Arguments:
[NAME] Who to greet [default: (Deadpoolio the amazing's name)]
Options:
--help Show this message and exit.
// See it shows "(Deadpoolio the amazing's name)" instead of the actual default of "Wade Wilson"
```
</div>
## Custom help name (`metavar`)
You can also customize the text used in the generated help text to represent a *CLI argument*.
By default, it will be the same name you declared, in uppercase letters.
So, if you declare it as:
```Python
name: str
```
It will be shown as:
```
NAME
```
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[5] *}
Now the generated help text will have `โจusernameโจ` instead of `NAME`:
<div class="termy">
```console
$ python main.py --help
Usage: main.py [OPTIONS] โจusernameโจ
Arguments:
โจusernameโจ [default: World]
Options:
--help Show this message and exit.
```
</div>
## *CLI Argument* help panels
You might want to show the help information for *CLI arguments* in different panels when using the `--help` option.
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[8,12] *}
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`.
And next you will see other panels for the *CLI arguments* that have a custom panel set in the `rich_help_panel` parameter:
<div class="termy">
```console
$ python main.py --help
<b> </b><font color="#F4BF75"><b>Usage: </b></font><b>main.py [OPTIONS] NAME [LASTNAME] [AGE] </b>
<b> </b>
Say hi to NAME very gently, like Dirk.
<font color="#A5A5A1">โญโ Arguments โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ</font>
<font color="#A5A5A1">โ </font><font color="#F92672">*</font> name <font color="#F4BF75"><b>TEXT</b></font> Who to greet [default: None] <font color="#A6194C">[required]</font> โ
<font color="#A5A5A1">โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ</font>
<font color="#A5A5A1">โญโ Secondary Arguments โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ</font>
<font color="#A5A5A1">โ lastname </font><font color="#A37F4E"><b>[LASTNAME]</b></font> The last name โ
<font color="#A5A5A1">โ age </font><font color="#A37F4E"><b>[AGE] </b></font> The user's age โ
<font color="#A5A5A1">โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ</font>
<font color="#A5A5A1">โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ</font>
<font color="#A5A5A1">โ </font><font color="#A1EFE4"><b>--help</b></font> Show this message and exit. โ
<font color="#A5A5A1">โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ</font>
```
</div>
In this example we have a custom *CLI arguments* panel named "`Secondary Arguments`".
## Help with style using Rich
In a future section you will see how to use custom markup in the `help` for *CLI arguments* when reading about [Commands - Command Help](../commands/help.md#rich-markdown-and-markup){.internal-link target=_blank}.
If you are in a hurry you can jump there, but otherwise, it would be better to continue reading here and following the tutorial in order.
## Hide a *CLI argument* from the help text
If you want, you can make a *CLI argument* **not** show up in the `Arguments` section in the help text.
You will probably not want to do this normally, but it's possible:
{* docs_src/arguments/help/tutorial008_an.py hl[5] *}
Check it:
<div class="termy">
```console
$ python main.py --help
// Notice there's no Arguments section at all ๐ฅ
Usage: main.py [OPTIONS] [NAME]
Say hi to NAME very gently, like Dirk.
Options:
--help Show this message and exit.
```
</div>
/// info
Have in mind that the *CLI argument* will still show up in the first line with `Usage`.
But it won't show up in the main help text under the `Arguments` section.
///
### Help text for *CLI arguments* in Click
Click itself doesn't support adding help for *CLI arguments*, and it doesn't generate help for them as in the "`Arguments:`" sections in the examples above.
Not supporting `help` in *CLI arguments* is an intentional <a href="https://click.palletsprojects.com/en/7.x/documentation/#documenting-arguments" class="external-link" target="_blank">design decision in Click</a>:
> This is to follow the general convention of Unix tools of using arguments for only the most necessary things, and to document them in the command help text by referring to them by name.
So, in Click applications, you are expected to write all the documentation for *CLI arguments* by hand in the <abbr title="a multi-line string as the first expression inside a function (not assigned to any variable) used for documentation">docstring</abbr>.
---
Nevertheless, **Typer supports `help` for *CLI arguments***. โจ ๐คทโโ
**Typer** doesn't follow that convention and instead supports `help` to make it easier to have consistent help texts with a consistent format for your CLI programs. ๐จ
This is also to help you create CLI programs that are โจ awesome โจ *by default*. With very little code.
If you want to keep Click's convention in a **Typer** app, you can do it with the `hidden` parameter as described above.
/// note | Technical Details
To support `help` in *CLI arguments* **Typer** does a lot of internal work in its own sub-classes of Click's internal classes.
///
|