Prepared annotations
CLI flags have no effect in the UI. Great for CLI so that the user writes less.
mininterface.tag.flag.Blank = Annotated[T | None, None]
This marker specifies:
- The default value can also be None (for the case the flag is omitted).
- A different behavior when the flag is provided without a value.
If the flag is left blank, it evaluates to True
(or to the value specified by a Literal
).
If the flag is omitted entirely from the CLI, it returns the default value.
from dataclasses import dataclass
from mininterface import run
from mininterface.tag.flag import Blank
@dataclass
class Env:
test: Blank[int] = None
print(run(Env).env.test)
Let's try:
$ program.py --help
usage: program.py [-h] [-v] [--test BLANK|int]
╭─ options ───────────────────────────────────────────────────────────────────────╮
│ -h, --help show this help message and exit │
│ -v, --verbose verbosity level, can be used multiple times to increase │
│ --test BLANK|int (default: 'None / or if left blank: True') │
╰─────────────────────────────────────────────────────────────────────────────────╯
The default blank value might be specified by a Literal
in the Annotated
statement.
Let's try:
You can use multiple types together:
Note that you can not use 'True' or 'False' for values, as the parameter becomes a bool.
Let's try:
Warning
Experimental.
Discussion
The design is working but syntax Annotated[str, Blank(True)]
might be a cleaner design. Do you have an opinion? Let us know.
mininterface.tag.flag.BlankTrue = Annotated[list[str] | bool | None, PrimitiveConstructorSpec(nargs='*', metavar='blank=True|BOOL', instance_from_str=_assure_blank_or_bool, is_instance=lambda instance: True, str_from_instance=lambda instance: [instance])]
When left blank, this flag produces True.
Returns:
Name | Type | Description |
---|---|---|
bool |
for |
|
True |
When parameter is left blank. |
Raises:
Type | Description |
---|---|
ValueError
|
Raised on an unknown parameter. |
Warning
Experimental.