Skip to content

Commit 42196d4

Browse files
💥 Update metavar printing (#1863)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 3a3bd0f commit 42196d4

133 files changed

Lines changed: 685 additions & 454 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,29 @@ Run your application with the `typer` command:
7878
// Run your application
7979
$ typer main.py run
8080

81-
// You get a nice error, you are missing NAME
82-
Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME
81+
// You get a nice error, you are missing 'name'
82+
Usage: typer [PATH_OR_MODULE] run [OPTIONS] {name}
8383
Try 'typer [PATH_OR_MODULE] run --help' for help.
8484
╭─ Error ───────────────────────────────────────────╮
85-
│ Missing argument 'NAME'. │
85+
│ Missing argument 'name'. │
8686
╰───────────────────────────────────────────────────╯
8787

8888

8989
// You get a --help for free
9090
$ typer main.py run --help
9191

92-
Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME
92+
Usage: typer [PATH_OR_MODULE] run [OPTIONS] {name}
9393

9494
Run the provided Typer app.
9595

9696
╭─ Arguments ───────────────────────────────────────╮
97-
│ * name TEXT [default: None] [required] |
97+
│ * name <str> [required] │
9898
╰───────────────────────────────────────────────────╯
9999
╭─ Options ─────────────────────────────────────────╮
100100
│ --help Show this message and exit. │
101101
╰───────────────────────────────────────────────────╯
102102

103-
// Now pass the NAME argument
103+
// Now pass the 'name' argument
104104
$ typer main.py run Camila
105105

106106
Hello Camila
@@ -138,27 +138,27 @@ Now you could run it with Python directly:
138138
// Run your application
139139
$ python main.py
140140

141-
// You get a nice error, you are missing NAME
142-
Usage: main.py [OPTIONS] NAME
141+
// You get a nice error, you are missing 'name'
142+
Usage: main.py [OPTIONS] {name}
143143
Try 'main.py --help' for help.
144144
╭─ Error ───────────────────────────────────────────╮
145-
│ Missing argument 'NAME'. │
145+
│ Missing argument 'name'. │
146146
╰───────────────────────────────────────────────────╯
147147

148148

149149
// You get a --help for free
150150
$ python main.py --help
151151

152-
Usage: main.py [OPTIONS] NAME
152+
Usage: main.py [OPTIONS] {name}
153153

154154
╭─ Arguments ───────────────────────────────────────╮
155-
│ * name TEXT [default: None] [required] |
155+
│ * name <str> [required] │
156156
╰───────────────────────────────────────────────────╯
157157
╭─ Options ─────────────────────────────────────────╮
158158
│ --help Show this message and exit. │
159159
╰───────────────────────────────────────────────────╯
160160

161-
// Now pass the NAME argument
161+
// Now pass the 'name' argument
162162
$ python main.py Camila
163163

164164
Hello Camila
@@ -236,13 +236,13 @@ $ python main.py --help
236236
│ and exit. │
237237
╰───────────────────────────────────────────────────╯
238238
╭─ Commands ────────────────────────────────────────╮
239-
│ goodbye │
240239
│ hello │
240+
│ goodbye │
241241
╰───────────────────────────────────────────────────╯
242242

243243
// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion
244244

245-
// You have 2 subcommands (the 2 functions): goodbye and hello
245+
// You have 2 subcommands (the 2 functions): hello and goodbye
246246
```
247247

248248
</div>
@@ -254,10 +254,10 @@ Now check the help for the `hello` command:
254254
```console
255255
$ python main.py hello --help
256256

257-
Usage: main.py hello [OPTIONS] NAME
257+
Usage: main.py hello [OPTIONS] {name}
258258

259259
╭─ Arguments ───────────────────────────────────────╮
260-
│ * name TEXT [default: None] [required] │
260+
│ * name <str> [required] │
261261
╰───────────────────────────────────────────────────╯
262262
╭─ Options ─────────────────────────────────────────╮
263263
│ --help Show this message and exit. │
@@ -273,10 +273,10 @@ And now check the help for the `goodbye` command:
273273
```console
274274
$ python main.py goodbye --help
275275

276-
Usage: main.py goodbye [OPTIONS] NAME
276+
Usage: main.py goodbye [OPTIONS] {name}
277277

278278
╭─ Arguments ───────────────────────────────────────╮
279-
│ * name TEXT [default: None] [required] │
279+
│ * name <str> [required] │
280280
╰───────────────────────────────────────────────────╯
281281
╭─ Options ─────────────────────────────────────────╮
282282
│ --formal --no-formal [default: no-formal] │

‎docs/index.md‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,29 @@ Run your application with the `typer` command:
8686
// Run your application
8787
$ typer main.py run
8888

89-
// You get a nice error, you are missing NAME
90-
Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME
89+
// You get a nice error, you are missing 'name'
90+
Usage: typer [PATH_OR_MODULE] run [OPTIONS] {name}
9191
Try 'typer [PATH_OR_MODULE] run --help' for help.
9292
╭─ Error ───────────────────────────────────────────╮
93-
│ Missing argument 'NAME'. │
93+
│ Missing argument 'name'. │
9494
╰───────────────────────────────────────────────────╯
9595

9696

9797
// You get a --help for free
9898
$ typer main.py run --help
9999

100-
Usage: typer [PATH_OR_MODULE] run [OPTIONS] NAME
100+
Usage: typer [PATH_OR_MODULE] run [OPTIONS] {name}
101101

102102
Run the provided Typer app.
103103

104104
╭─ Arguments ───────────────────────────────────────╮
105-
│ * name TEXT [default: None] [required] |
105+
│ * name <str> [required] │
106106
╰───────────────────────────────────────────────────╯
107107
╭─ Options ─────────────────────────────────────────╮
108108
│ --help Show this message and exit. │
109109
╰───────────────────────────────────────────────────╯
110110

111-
// Now pass the NAME argument
111+
// Now pass the 'name' argument
112112
$ typer main.py run Camila
113113

114114
Hello Camila
@@ -146,27 +146,27 @@ Now you could run it with Python directly:
146146
// Run your application
147147
$ python main.py
148148

149-
// You get a nice error, you are missing NAME
150-
Usage: main.py [OPTIONS] NAME
149+
// You get a nice error, you are missing 'name'
150+
Usage: main.py [OPTIONS] {name}
151151
Try 'main.py --help' for help.
152152
╭─ Error ───────────────────────────────────────────╮
153-
│ Missing argument 'NAME'. │
153+
│ Missing argument 'name'. │
154154
╰───────────────────────────────────────────────────╯
155155

156156

157157
// You get a --help for free
158158
$ python main.py --help
159159

160-
Usage: main.py [OPTIONS] NAME
160+
Usage: main.py [OPTIONS] {name}
161161

162162
╭─ Arguments ───────────────────────────────────────╮
163-
│ * name TEXT [default: None] [required] |
163+
│ * name <str> [required] │
164164
╰───────────────────────────────────────────────────╯
165165
╭─ Options ─────────────────────────────────────────╮
166166
│ --help Show this message and exit. │
167167
╰───────────────────────────────────────────────────╯
168168

169-
// Now pass the NAME argument
169+
// Now pass the 'name' argument
170170
$ python main.py Camila
171171

172172
Hello Camila
@@ -244,13 +244,13 @@ $ python main.py --help
244244
│ and exit. │
245245
╰───────────────────────────────────────────────────╯
246246
╭─ Commands ────────────────────────────────────────╮
247-
│ goodbye │
248247
│ hello │
248+
│ goodbye │
249249
╰───────────────────────────────────────────────────╯
250250

251251
// When you create a package you get ✨ auto-completion ✨ for free, installed with --install-completion
252252

253-
// You have 2 subcommands (the 2 functions): goodbye and hello
253+
// You have 2 subcommands (the 2 functions): hello and goodbye
254254
```
255255

256256
</div>
@@ -262,10 +262,10 @@ Now check the help for the `hello` command:
262262
```console
263263
$ python main.py hello --help
264264

265-
Usage: main.py hello [OPTIONS] NAME
265+
Usage: main.py hello [OPTIONS] {name}
266266

267267
╭─ Arguments ───────────────────────────────────────╮
268-
│ * name TEXT [default: None] [required] │
268+
│ * name <str> [required] │
269269
╰───────────────────────────────────────────────────╯
270270
╭─ Options ─────────────────────────────────────────╮
271271
│ --help Show this message and exit. │
@@ -281,10 +281,10 @@ And now check the help for the `goodbye` command:
281281
```console
282282
$ python main.py goodbye --help
283283

284-
Usage: main.py goodbye [OPTIONS] NAME
284+
Usage: main.py goodbye [OPTIONS] {name}
285285

286286
╭─ Arguments ───────────────────────────────────────╮
287-
│ * name TEXT [default: None] [required] │
287+
│ * name <str> [required] │
288288
╰───────────────────────────────────────────────────╯
289289
╭─ Options ─────────────────────────────────────────╮
290290
│ --formal --no-formal [default: no-formal] │

‎docs/js/custom.js‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
function escapePlainCliLine(line) {
2+
return line
3+
.replace(/&/g, "&amp;")
4+
.replace(/</g, "&lt;")
5+
.replace(/>/g, "&gt;");
6+
}
7+
8+
function prepareTermynalLines(lines, isRichHtml) {
9+
if (isRichHtml) {
10+
return lines.join("<br>");
11+
}
12+
return lines.map(escapePlainCliLine).join("<br>");
13+
}
14+
115
function setupTermynal() {
216
document.querySelectorAll(".use-termynal").forEach(node => {
317
node.style.display = "block";
@@ -17,6 +31,11 @@ function setupTermynal() {
1731
.forEach(node => {
1832
const text = node.textContent;
1933
const lines = text.split("\n");
34+
// If it is a type in brackets like <str> or <str int boolean>, it won't have
35+
// HTML attributes (=) or a closing tag, while Rich HTML tags do.
36+
const hasHtmlAttribute = /<[A-Za-z][^>]*=/.test(text);
37+
const hasClosingTag = /<\/[a-zA-Z0-9]/.test(text);
38+
const isRichHtml = hasHtmlAttribute || hasClosingTag;
2039
const useLines = [];
2140
let buffer = [];
2241
function saveBuffer() {
@@ -36,7 +55,7 @@ function setupTermynal() {
3655
// so put an additional one
3756
buffer.push("");
3857
}
39-
const bufferValue = buffer.join("<br>");
58+
const bufferValue = prepareTermynalLines(buffer, isRichHtml);
4059
dataValue["value"] = bufferValue;
4160
useLines.push(dataValue);
4261
buffer = [];
@@ -57,7 +76,11 @@ function setupTermynal() {
5776
});
5877
} else if (line.startsWith("// ")) {
5978
saveBuffer();
60-
const value = "💬 " + line.replace("// ", "").trimEnd();
79+
let comment = line.replace("// ", "").trimEnd();
80+
if (!isRichHtml) {
81+
comment = escapePlainCliLine(comment);
82+
}
83+
const value = "💬 " + comment;
6184
useLines.push({
6285
value: value,
6386
class: "termynal-comment",

‎docs/tutorial/arguments/default.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ Check it:
2727
$ python main.py --help
2828

2929
// Notice the [default: Wade Wilson] ✨
30-
Usage: main.py [OPTIONS] [NAME]
30+
Usage: main.py [OPTIONS] [name]
3131

3232
Arguments:
33-
[NAME] [default: Wade Wilson]
33+
name [default: Wade Wilson]
3434

3535
Options:
3636
--help Show this message and exit.
@@ -72,10 +72,10 @@ Check it:
7272
// Check the help
7373
$ python main.py --help
7474

75-
Usage: main.py [OPTIONS] [NAME]
75+
Usage: main.py [OPTIONS] [name]
7676

7777
Arguments:
78-
[NAME] [default: (dynamic)]
78+
name [default: (dynamic)]
7979

8080
Options:
8181
--help Show this message and exit.

‎docs/tutorial/arguments/envvar.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ In this case, the *CLI argument* `name` will have a default value of `"World"`,
2020
// Check the help
2121
$ python main.py --help
2222

23-
Usage: main.py [OPTIONS] [NAME]
23+
Usage: main.py [OPTIONS] [name]
2424

2525
Arguments:
26-
[NAME] [env var: AWESOME_NAME;default: World]
26+
name [env var: AWESOME_NAME; default: World]
2727

2828
Options:
2929
--help Show this message and exit.
@@ -65,10 +65,10 @@ Check it:
6565
// Check the help
6666
$ python main.py --help
6767

68-
Usage: main.py [OPTIONS] [NAME]
68+
Usage: main.py [OPTIONS] [name]
6969

7070
Arguments:
71-
[NAME] [env var: AWESOME_NAME, GOD_NAME;default: World]
71+
name [env var: AWESOME_NAME, GOD_NAME; default: World]
7272

7373
Options:
7474
--help Show this message and exit.
@@ -101,10 +101,10 @@ Check it:
101101
$ python main.py --help
102102

103103
// It won't show the env var
104-
Usage: main.py [OPTIONS] [NAME]
104+
Usage: main.py [OPTIONS] [name]
105105

106106
Arguments:
107-
[NAME] [default: World]
107+
name [default: World]
108108

109109
Options:
110110
--help Show this message and exit.

0 commit comments

Comments
 (0)