All high-level prompt functions from inquirer_textual.prompts module. These are the simplest way to collect single inputs.
Note on API Design: The high-level prompts.* functions provide a simplified API for common use cases. Some parameters available in the underlying widget classes (like default for InquirerText or mandatory for widgets) are intentionally not exposed to keep the API simple. For access to all widget parameters, use the widget classes directly with InquirerApp.prompt() (see Widgets API and InquirerApp Advanced).
from typing import Iterable
from inquirer_textual import prompts
from inquirer_textual.common.Choice import Choice
from inquirer_textual.common.Shortcut import Shortcut
from textual.validation import Validator, FunctionText input prompt with optional validation.
def text(
message: str,
shortcuts: list[Shortcut] | None = None,
validators: Validator | Iterable[Validator] | None = None
) -> Result[str]Result[str] with:
Basic text input:
result = prompts.text("Enter your name:")
if result.command == 'select':
print(f"Hello, {result.value}!")With validation:
from textual.validation import Function
result = prompts.text(
"Enter email:",
validators=Function(lambda s: '@' in s and '.' in s, "Invalid email format")
)
if result.command == 'select':
email = result.valueMultiple validators:
from textual.validation import Validator, ValidationResult
class MinLength(Validator):
def __init__(self, length: int):
super().__init__()
self.length = length
def validate(self, value: str) -> ValidationResult:
return self.success() if len(value) >= self.length else self.failure(f"Min {self.length} chars")
class NoSpaces(Validator):
def validate(self, value: str) -> ValidationResult:
return self.success() if ' ' not in value else self.failure("No spaces allowed")
result = prompts.text(
"Username:",
validators=[MinLength(3), NoSpaces()]
)With shortcuts:
shortcuts = [
Shortcut('escape', 'cancel', 'Cancel'),
Shortcut('ctrl+s', 'save_draft', 'Save Draft')
]
result = prompts.text("Enter text:", shortcuts=shortcuts)
if result.command == 'cancel':
print("User cancelled")
elif result.command == 'save_draft':
save_draft(result.value)
elif result.command == 'select':
save_final(result.value)Password/secret input with masked display.
def secret(
message: str,
shortcuts: list[Shortcut] | None = None
) -> Result[str]Result[str] with:
result = prompts.secret("Enter password:")
if result.command == 'select':
password = result.value
if len(password) < 8:
print("Password too short")
else:
authenticate(password)result = prompts.secret("API key:", shortcuts=[Shortcut('escape', 'cancel')])
if result.command == 'select':
api_key = result.value
elif result.command == 'cancel':
print("Cancelled")Integer number input prompt.
def number(
message: str,
shortcuts: list[Shortcut] | None = None
) -> Result[str]Result[str] with:
int(result.value)result = prompts.number("Enter your age:")
if result.command == 'select':
age = int(result.value) # Convert to int
print(f"Age: {age}")result = prompts.number("Port number:")
if result.command == 'select':
port = int(result.value)
if 1024 <= port <= 65535:
start_server(port)
else:
print("Port must be 1024-65535")Yes/No confirmation prompt.
def confirm(
message: str,
shortcuts: list[Shortcut] | None = None,
default: bool = False,
mandatory: bool = True
) -> Result[bool]Result[bool] with:
Basic confirmation:
result = prompts.confirm("Continue with installation?", default=True)
if result.command == 'select':
if result.value:
install()
else:
print("Installation cancelled")Non-mandatory:
result = prompts.confirm("Delete file?", default=False, mandatory=False)
if result.command == 'select':
if result.value:
delete_file()
elif result.command == 'ctrl+c':
print("User aborted")With shortcuts:
result = prompts.confirm(
"Overwrite existing?",
default=False,
shortcuts=[Shortcut('a', 'all', 'Yes to All')]
)
if result.command == 'all':
overwrite_all()
elif result.command == 'select' and result.value:
overwrite_one()Single selection from a list.
def select(
message: str,
choices: list[str | Choice],
shortcuts: list[Shortcut] | None = None,
default: str | Choice | None = None,
mandatory: bool = True
) -> Result[str | Choice]Result[str | Choice] with:
String choices:
result = prompts.select(
"Select language:",
["Python", "JavaScript", "Go", "Rust"]
)
if result.command == 'select':
language = result.value # str
print(f"Selected: {language}")With default:
result = prompts.select(
"Log level:",
["DEBUG", "INFO", "WARNING", "ERROR"],
default="INFO"
)Choice objects with data:
from inquirer_textual.common.Choice import Choice
environments = [
Choice("Development", data={"url": "localhost:3000", "debug": True}),
Choice("Staging", data={"url": "staging.example.com", "debug": False}),
Choice("Production", data={"url": "example.com", "debug": False})
]
result = prompts.select("Select environment:", environments)
if result.command == 'select':
env = result.value
print(f"Connecting to {env.data['url']}")
if env.data['debug']:
enable_debug()Choice with custom commands:
from inquirer_textual.common.Choice import Choice
actions = [
Choice("View Details", command="view"),
Choice("Edit", command="edit"),
Choice("Delete", command="delete")
]
result = prompts.select("Select action:", actions)
if result.command == 'view':
show_details()
elif result.command == 'edit':
edit_item()
elif result.command == 'delete':
delete_item()Non-mandatory:
result = prompts.select("Choose:", ["A", "B", "C"], mandatory=False)
if result.command == 'select':
process(result.value)
elif result.command == 'ctrl+c':
print("No selection made")Multiple selection from a list with checkboxes.
def checkbox(
message: str,
choices: list[str | Choice],
shortcuts: list[Shortcut] | None = None,
enabled: list[str | Choice] | None = None
) -> Result[list[str | Choice]]Result[list[str | Choice]] with:
enabled parameter does NOT work; all items start uncheckedString choices:
features = ["Authentication", "Database", "Caching", "Logging"]
result = prompts.checkbox("Select features to enable:", features)
if result.command == 'select':
enabled_features = result.value # list[str]
print(f"Enabled: {', '.join(enabled_features)}")Choice objects:
from inquirer_textual.common.Choice import Choice
plugins = [
Choice("ESLint", data={"package": "eslint"}),
Choice("Prettier", data={"package": "prettier"}),
Choice("Jest", data={"package": "jest"})
]
result = prompts.checkbox("Select plugins:", plugins)
if result.command == 'select':
for plugin in result.value:
install_package(plugin.data["package"])With shortcuts:
shortcuts = [
Shortcut('a', 'all', 'Select All'),
Shortcut('n', 'none', 'Select None')
]
result = prompts.checkbox("Select items:", ["A", "B", "C"], shortcuts=shortcuts)
if result.command == 'all':
# Custom logic for "select all"
selected = ["A", "B", "C"]
elif result.command == 'none':
selected = []
elif result.command == 'select':
selected = result.valueEnabled parameter (NOT WORKING in v0.2.0):
# This does NOT work in v0.2.0
result = prompts.checkbox(
"Select:",
["A", "B", "C"],
enabled=["A", "B"] # Has no effect; all items start unchecked
)
# Workaround: User must manually check items with SpaceSequential multi-prompt form.
def multi(
widgets: list[InquirerWidget],
shortcuts: list[Shortcut] | None = None
) -> Result[list[Any]]Result[list[Any]] with:
Basic form:
from inquirer_textual.widgets.InquirerText import InquirerText
from inquirer_textual.widgets.InquirerConfirm import InquirerConfirm
widgets = [
InquirerText("Name:"),
InquirerText("Email:"),
InquirerConfirm("Subscribe?", default=True)
]
result = prompts.multi(widgets)
if result.command == 'select':
name, email, subscribe = result.value
print(f"Name: {name}, Email: {email}, Subscribe: {subscribe}")Registration form:
from inquirer_textual.widgets.InquirerText import InquirerText
from inquirer_textual.widgets.InquirerSecret import InquirerSecret
from inquirer_textual.widgets.InquirerNumber import InquirerNumber
from textual.validation import Function
widgets = [
InquirerText(
"Username:",
validators=Function(lambda s: len(s) >= 3, "Min 3 characters")
),
InquirerSecret("Password:"),
InquirerText(
"Email:",
validators=Function(lambda s: '@' in s, "Invalid email")
),
InquirerNumber("Age:")
]
result = prompts.multi(widgets)
if result.command == 'select':
username, password, email, age_str = result.value
age = int(age_str)
create_user(username, password, email, age)Configuration wizard:
from inquirer_textual.widgets.InquirerText import InquirerText
from inquirer_textual.widgets.InquirerSelect import InquirerSelect
from inquirer_textual.widgets.InquirerCheckbox import InquirerCheckbox
widgets = [
InquirerText("Project name:"),
InquirerSelect("Framework:", ["React", "Vue", "Angular"]),
InquirerCheckbox("Features:", ["TypeScript", "ESLint", "Tests"]),
InquirerSelect("Package manager:", ["npm", "yarn", "pnpm"])
]
result = prompts.multi(widgets)
if result.command == 'select':
name, framework, features, pkg_manager = result.value
create_project(name, framework, features, pkg_manager)With shortcuts and early exit:
shortcuts = [
Shortcut('escape', 'cancel', 'Cancel'),
Shortcut('ctrl+s', 'save_draft', 'Save Draft')
]
widgets = [
InquirerText("Title:"),
InquirerText("Description:"),
InquirerText("Tags:")
]
result = prompts.multi(widgets, shortcuts=shortcuts)
if result.command == 'cancel':
print("Cancelled")
elif result.command == 'save_draft':
# Partial data - some fields may not be filled
save_draft(result.value)
elif result.command == 'select':
# All fields completed
title, description, tags = result.value
create_post(title, description, tags)Handling early quit:
widgets = [
InquirerText("Field 1:"),
InquirerText("Field 2:"),
InquirerText("Field 3:")
]
result = prompts.multi(widgets)
if result.command == 'quit':
# User quit early
completed_count = len(result.value)
print(f"Completed {completed_count} of {len(widgets)} fields")
if completed_count >= 2:
# Process partial data
process_partial(result.value)
elif result.command == 'select':
# All fields completed
f1, f2, f3 = result.value
process_complete(f1, f2, f3)result = prompts.text("Input:")
# ALWAYS check command before using value
if result.command == 'select':
# Safe to use result.value
process(result.value)
elif result.command == 'quit':
print("User quit")COMMON_SHORTCUTS = [
Shortcut('escape', 'cancel', 'Cancel'),
Shortcut('ctrl+h', 'help', 'Help')
]
result1 = prompts.text("Input 1:", shortcuts=COMMON_SHORTCUTS)
result2 = prompts.text("Input 2:", shortcuts=COMMON_SHORTCUTS)from inquirer_textual.common.Choice import Choice
def create_choices(items):
return [
Choice(item['name'], data=item)
for item in items
]
items = [{'name': 'A', 'id': 1}, {'name': 'B', 'id': 2}]
choices = create_choices(items)
result = prompts.select("Pick:", choices)
if result.command == 'select':
item_id = result.value.data['id']from textual.validation import Validator, ValidationResult
def create_min_length_validator(min_len: int) -> Validator:
class MinLengthValidator(Validator):
def validate(self, value: str) -> ValidationResult:
if len(value) >= min_len:
return self.success()
return self.failure(f"Minimum {min_len} characters required")
return MinLengthValidator()
result = prompts.text("Username:", validators=create_min_length_validator(3))