Prompts¶
All of these are importable from the top-level package (t3.confirm,
t3.ask, t3.choose, t3.pick_file). See the
prompts guide.
confirm
¶
Ask a yes/no question. The default answer is no.
Raises:
| Type | Description |
|---|---|
NotInteractive
|
there's no terminal to ask in. |
ask
¶
ask(question: str, *, default: str | None = None, parse: Callable[[str], object] | None = None) -> object
Ask for a value. An empty answer takes default, or asks again if there's none.
parse turns the answer into something else. If it raises
ValueError, the message is shown and the question asked again:
quantity = t3.ask("Quantity", parse=float)
when = t3.ask("Date (YYYY-MM-DD)", default=str(date.today()), parse=date.fromisoformat)
Raises:
| Type | Description |
|---|---|
NotInteractive
|
there's no terminal to ask in. |
choose
¶
choose(title: str, items: Sequence[T], columns: Sequence[str], render: Callable[[T], Sequence[str]], *, filterable: bool | None = None) -> T
Show items as a numbered table and return the chosen one.
package = t3.choose(
"Source package",
packages,
columns=["Label", "Item", "Quantity"],
render=lambda p: [p["label"], p["item"]["name"], str(p["quantity"])],
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The table's title. |
required |
items
|
Sequence[T]
|
What to choose from. It must not be empty. |
required |
columns
|
Sequence[str]
|
Column headings, after the |
required |
render
|
Callable[[T], Sequence[str]]
|
Returns one string per column for an item. |
required |
filterable
|
bool | None
|
Let typing text instead of a number narrow the list. Defaults to on for more than 10 items. |
None
|
Raises:
| Type | Description |
|---|---|
NotInteractive
|
there's no terminal to ask in. |
ValueError
|
|
pick_file
¶
pick_file(directory: str | Path = '.', *, patterns: Sequence[str] = DEFAULT_FILE_PATTERNS, title: str = 'Files') -> Path
Pick a file from directory (newest first), or type a path.