Skip to content

Helper types

ValidationResult = bool | ErrorMessage

Callback validation result is either boolean or an error message.

ErrorMessage = TypeVar('ErrorMessage')

A string, callback validation error message.

TagValue = TypeVar('TagValue', bound=Any)

Any value. It is being wrapped by a Tag.

ChoicesType = list[TagValue] | tuple[TagValue] | set[TagValue] | dict[RichChoiceLabel, TagValue] | list[Enum] | Type[Enum]

You can denote the choices in many ways. Either put options in an iterable or to a dict {labels: value}. Values might be Tags as well. Let's take a detailed look. We will use the run.choice(ChoicesType) to illustrate the examples.

Iterables like list

Either put options in an iterable:

from mininterface import run
m = run()
m.choice([1, 2])

Choices as a list

Dict for labels

Or to a dict {name: value}. Then name are used as labels.

m.choice({"one": 1, "two": 2})  # returns 1
Dict with tuples for table

If you use tuple as the keys, they will be joined into a table.

m.choice({("one", "two", "three"): 1, ("lorem", "ipsum", "dolor") : 2})

Table like

Tags for labels

Alternatively, you may specify the names in Tags.

m.choice([Tag(1, name="one"), Tag(2, name="two")])  # returns 1

Choices with labels

Enums

Alternatively, you may use an Enum.

class Color(Enum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"

m.choice(Color)

Choices from enum

Alternatively, you may use an Enum instance. (Which means the default value is already selected.)

class Color(Enum):
    RED = "red"
    GREEN = "green"
    BLUE = "blue"

m.choice(Color.BLUE)

Choices from enum

Alternatively, you may use an Enum instances list.

m.choice([Color.GREEN, Color.BLUE])

Choices from enum list

Further examples

See mininterface.choice or EnumTag.choices for further usage.

DataClass = TypeVar('DataClass')

Any dataclass. Or a pydantic model or attrs.

EnvClass = TypeVar('EnvClass', bound=DataClass)

Any dataclass. Its instance will be available through [miniterface.env] after CLI parsing.