The builder pattern in pinq allows you to configure prompts step-by-step with method chaining.
All prompt classes follow the builder pattern:
import pinq
# Create a prompt
prompt = pinq.TextPrompt("Your question: ")
# Configure it with fluent methods
prompt.with_default("default value")
prompt.with_help_message("Help text")
prompt.with_page_size(10)
# Execute it
result = prompt.prompt()All with_* methods return self, enabling method chaining:
import pinq
result = (pinq.TextPrompt("Name: ")
.with_default("User")
.with_help_message("Enter your name")
.with_page_size(7)
.prompt())prompt = pinq.TextPrompt("Username: ")
prompt.with_default("admin") # Set default value
prompt.with_help_message("Required") # Set help text
prompt.with_page_size(5) # Set pagination size
result = prompt.prompt()Available methods:
with_default(value: str) -> TextPromptwith_help_message(text: str) -> TextPromptwith_page_size(size: int) -> TextPromptprompt() -> strprompt_skippable() -> Optional[str]
prompt = pinq.ConfirmPrompt("Delete file?")
prompt.with_default(False) # Default to "no"
prompt.with_help_message("Cannot be undone")
result = prompt.prompt()Available methods:
with_default(value: bool) -> ConfirmPromptwith_help_message(text: str) -> ConfirmPromptprompt() -> boolprompt_skippable() -> Optional[bool]
prompt = pinq.PasswordPrompt("Password: ")
prompt.with_help_message("At least 8 chars")
result = prompt.prompt()Available methods:
with_help_message(text: str) -> PasswordPromptprompt() -> strprompt_skippable() -> Optional[str]
options = ["Red", "Green", "Blue"]
prompt = pinq.SelectPrompt("Pick a color:", options)
prompt.with_default(0) # Default to first option
prompt.with_help_message("Use arrows")
prompt.with_page_size(10)
result = prompt.prompt()Available methods:
with_default(index: int) -> SelectPromptwith_help_message(text: str) -> SelectPromptwith_page_size(size: int) -> SelectPromptprompt() -> strprompt_skippable() -> Optional[str]
options = ["Python", "Rust", "Go"]
prompt = pinq.MultiSelectPrompt("Languages:", options)
prompt.with_defaults([0, 2]) # Default select first and third
prompt.with_help_message("SPACE to select")
prompt.with_page_size(5)
result = prompt.prompt()Available methods:
with_defaults(indices: List[int]) -> MultiSelectPromptwith_help_message(text: str) -> MultiSelectPromptwith_page_size(size: int) -> MultiSelectPromptprompt() -> List[str]prompt_skippable() -> Optional[List[str]]
prompt = pinq.IntPrompt("Count: ")
prompt.with_default(10)
prompt.with_help_message("Must be > 0")
result = prompt.prompt()Available methods:
with_default(value: int) -> IntPromptwith_help_message(text: str) -> IntPromptprompt() -> intprompt_skippable() -> Optional[int]
prompt = pinq.FloatPrompt("Price: $")
prompt.with_default(9.99)
prompt.with_help_message("Use decimal point")
result = prompt.prompt()Available methods:
with_default(value: float) -> FloatPromptwith_help_message(text: str) -> FloatPromptprompt() -> floatprompt_skippable() -> Optional[float]
prompt = pinq.DateSelectPrompt("Select date: ")
prompt.with_help_message("Navigate with arrows")
result = prompt.prompt() # Returns YYYY-MM-DD stringAvailable methods:
with_help_message(text: str) -> DateSelectPromptprompt() -> strprompt_skippable() -> Optional[str]
prompt = pinq.EditorPrompt("Write message: ")
prompt.with_help_message("Press e to open editor")
result = prompt.prompt()Available methods:
with_help_message(text: str) -> EditorPromptprompt() -> strprompt_skippable() -> Optional[str]
import pinq
# Get username with default
username = (pinq.TextPrompt("Username: ")
.with_default("user")
.with_help_message("3-20 alphanumeric characters")
.prompt())
# Get password
password = pinq.PasswordPrompt("Password: ").prompt()
# Confirm
confirmed = (pinq.ConfirmPrompt("Register this account?")
.with_default(True)
.prompt())
if confirmed:
print(f"Registered user: {username}")import pinq
# Get application settings
port = (pinq.IntPrompt("API Port: ")
.with_default(3000)
.with_help_message("1024-65535")
.prompt())
debug = (pinq.ConfirmPrompt("Enable debug mode?")
.with_default(False)
.prompt())
env = (pinq.SelectPrompt("Environment:",
["development", "staging", "production"])
.with_default(0)
.with_help_message("Select deployment environment")
.prompt())
print(f"Config: port={port}, debug={debug}, env={env}")import pinq
name = pinq.prompt_text("Your name: ")
# Optional feedback
feedback = (pinq.EditorPrompt("Feedback (optional): ")
.with_help_message("Press ESC to skip")
.prompt_skippable())
if feedback is not None:
print(f"Feedback from {name}: {feedback[:100]}...")
else:
print(f"No feedback from {name}")import pinq
options = [
"Apples",
"Bananas",
"Carrots",
"Dates",
"Eggplant"
]
selected = (pinq.MultiSelectPrompt("Select fruits:", options)
.with_defaults([0, 1]) # Default: Apples and Bananas
.with_page_size(5)
.with_help_message("SPACE to select/deselect, ENTER to confirm")
.prompt())
print(f"Selected {len(selected)} items: {', '.join(selected)}")import pinq
principal = (pinq.FloatPrompt("Principal amount: $")
.with_default(1000.0)
.with_help_message("Enter amount without $ sign")
.prompt())
rate = (pinq.FloatPrompt("Annual interest rate (%): ")
.with_default(5.0)
.prompt())
years = (pinq.IntPrompt("Time period (years): ")
.with_default(5)
.prompt())
total = principal * (1 + rate/100) ** years
print(f"Total after {years} years: ${total:.2f}")import pinq
print("=== Server Configuration ===\n")
host = (pinq.TextPrompt("Hostname: ")
.with_default("localhost")
.with_help_message("Use localhost or IP address")
.prompt())
port = (pinq.IntPrompt("Port: ")
.with_default(8000)
.with_help_message("1024-65535")
.prompt())
protocol = (pinq.SelectPrompt("Protocol:", ["HTTP", "HTTPS"])
.with_default(0)
.with_page_size(2)
.prompt())
features = (pinq.MultiSelectPrompt("Features:",
["Caching", "Auth", "Logging", "Monitoring"])
.with_defaults([0, 1, 2]) # Enable first 3 by default
.with_help_message("Use SPACE to toggle")
.prompt())
confirm = (pinq.ConfirmPrompt(
f"Apply: {protocol}://{host}:{port} with {len(features)} features?")
.with_default(True)
.prompt())
if confirm:
print("Configuration applied!")# Good: Clear what's being configured
prompt = pinq.TextPrompt("Username: ")
prompt.with_default("admin")
prompt.with_help_message("System administrator account")
username = prompt.prompt()# Good: Concise one-liner
username = (pinq.TextPrompt("Username: ")
.with_default("admin")
.with_help_message("System administrator account")
.prompt())import pinq
try:
age = (pinq.IntPrompt("Your age: ")
.with_default(25)
.with_help_message("Enter a number")
.prompt())
except RuntimeError as e:
print(f"Error: {e}")import pinq
def create_username_prompt():
return (pinq.TextPrompt("Username: ")
.with_default("user")
.with_help_message("3-20 alphanumeric characters")
.with_page_size(7))
username = create_username_prompt().prompt()import pinq
is_admin = pinq.prompt_confirmation("Admin account? ")
prompt = pinq.TextPrompt("Username: ")
if is_admin:
prompt.with_default("admin")
else:
prompt.with_default("user")
username = prompt.prompt()# User can skip by pressing ESC
name = pinq.TextPrompt("Name (optional): ").prompt_skippable()
if name is None:
print("No name provided")
else:
print(f"Name: {name}")results = {}
results['name'] = pinq.prompt_text("Name: ")
results['email'] = pinq.prompt_text("Email: ")
results['age'] = pinq.prompt_int("Age: ")
results['subscribe'] = pinq.prompt_confirmation("Subscribe? ")
print(results)import pinq
while True:
try:
port = pinq.prompt_int("Port (1024-65535): ")
if 1024 <= port <= 65535:
break
else:
print("Port out of range!")
except RuntimeError:
print("Invalid input!")- Classes Reference - Detailed class documentation
- Functions Reference - One-liner functions
- Examples - More real-world examples