Skip to content

Commit 8363ffd

Browse files
committed
fix: config file with subcommands union resilience
1 parent 290bd65 commit 8363ffd

17 files changed

+215
-42
lines changed

asset/examples-function-with.avif

3.67 KB
Binary file not shown.

asset/examples-function.avif

1.89 KB
Binary file not shown.

asset/nested-dataclass.avif

2.17 KB
Binary file not shown.

asset/nested-subcommands-1.avif

2.25 KB
Binary file not shown.

asset/nested-subcommands-2.avif

2.55 KB
Binary file not shown.

asset/nested-subcommands-3.avif

3.42 KB
Binary file not shown.

asset/scalars.avif

4.49 KB
Binary file not shown.

asset/selecttag-multiple.avif

2.12 KB
Binary file not shown.

docs/Changelog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Changelog
22

3-
## 1.1.2 (unreleased)
3+
## 1.1.2 (2025-10-01)
44
* feat: timeout parameter for alert & confirm
55
* enh: `list[tuple]` supperfluous arguments check
66
* enh: dict in a dataclass support
7-
* fix: config file with str-attribute missing clash
7+
* fix: config file resilience (subcommands union and str-attribute missing clash)
88
* fix: SelectTag multiple choice without default value resilience
99
* fix: dynamic Literal in Annotated
1010

docs/Supported-types.md

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class Env:
103103
run(Env).form()
104104
```
105105

106+
![Scalars](asset/scalars.avif)
107+
106108
### Functions
107109

108110
Will appear as buttons.
@@ -118,6 +120,8 @@ class Env:
118120
run(Env).form()
119121
```
120122

123+
![Function](asset/examples-function.avif)
124+
121125
Or use the `with` statement to redirect the stdout into the mininterface.
122126

123127
```python
@@ -126,6 +130,8 @@ with run(Env) as m:
126130
m.alert("The text 'I'm here' is displayed in the window.")
127131
```
128132

133+
![Function](asset/examples-function-with.avif)
134+
129135
!!! Warning
130136
When used in a form like this `m.form({'My callback': my_callback)`, the value is left intact. It still points to the function. This behaviour might reconsidered and changed. (It might make more sense to change it to the return value instead.)
131137

@@ -151,7 +157,7 @@ class Env:
151157
m = run(Env)
152158
```
153159

154-
CLI shown keys.
160+
CLI shows keys.
155161

156162
```bash
157163
$ ./program.py --help
@@ -231,11 +237,13 @@ Advanced adjustments, multiple choice, dynamic values, etc.
231237
@dataclass
232238
class Env:
233239
val: Annotated[list[str], SelectTag(options=["one", "two"], multiple=True)]
240+
241+
run(Env)
234242
```
235243

236-
### Nested dataclasses or their unions (subcommands)
244+
![SelectTag multiple](asset/selecttag-multiple.avif)
237245

238-
TODO
246+
### Nested dataclasses or their unions (subcommands)
239247

240248
You can nest the classes to create a subgroup:
241249

@@ -247,12 +255,17 @@ class Message:
247255
@dataclass
248256
class Env:
249257
val: Message
258+
259+
run(Env)
250260
```
251261

262+
![Nested dataclass](asset/nested-dataclass.avif)
263+
252264
You can union the classes to create subcommands:
253265

254266
```python
255267
from typing import Literal
268+
from tyro.conf import OmitSubcommandPrefixes
256269

257270
@dataclass
258271
class ConsolePlain:
@@ -275,13 +288,53 @@ class Message:
275288
class Env:
276289
val: Message | Console
277290

278-
m = run(Env)
291+
m = run(OmitSubcommandPrefixes[Env])
279292
```
280293

281-
!!! Tip
282-
Use
283-
# TODO cli.Command to automatically
294+
First, we've chosen `Console`, then `Console rich`.
295+
296+
![Nested subcommands](asset/nested-subcommands-1.avif)
297+
![Choosing Console rich](asset/nested-subcommands-2.avif)
298+
![Fields from both Console and ConsoleRich](asset/nested-subcommands-3.avif)
299+
300+
??? Grouping
301+
Note fields from outer `Console` and inner `ConsoleRich` are displayed together in step 3. Why? You might start at arbitrary position.
302+
303+
Starting at step 1:
304+
305+
```bash
306+
$ ./program.py --help
307+
usage: program.py [-h] [-v] {message,console}
308+
$ ./program.py
309+
```
310+
311+
![Nested subcommands](asset/nested-subcommands-1.avif)
312+
313+
Starting at step 2:
314+
315+
```bash
316+
$ ./program.py console --help
317+
usage: program.py console [-h] [-v] --bot-id {id-one,id-two} {console-plain,console-rich}
318+
$ ./program.py console
319+
```
320+
321+
![Choosing Console rich](asset/nested-subcommands-2.avif)
322+
323+
That way, you may start anywhere from CLI, yet be sure all the missing fields, if possible, are grouped in a single form dialog.
324+
325+
??? OmitSubcommandPrefixes
326+
Why using `OmitSubcommandPrefixes`? This will rend the inscription shorter.
327+
328+
```bash
329+
$ ./program.py --help
330+
usage: program.py [-h] [-v] {message,console}
331+
```
284332

333+
Without:
334+
```bash
335+
$ ./program.py --help
336+
usage: program.py [-h] [-v] {val:message,val:console}
337+
```
285338

286339
### Well-known objects
287340

0 commit comments

Comments
 (0)