0
# Text Input Prompts
1
2
Text input prompts provide basic text collection functionality with support for single-line input, password masking, multiline text, and input validation.
3
4
## Capabilities
5
6
### Basic Text Input
7
8
Prompts for single-line text input with optional default values, validation, and custom styling.
9
10
```python { .api }
11
def text(message: str, default: str = "", validate: Any = None,
12
qmark: str = "?", style: Optional[Style] = None,
13
multiline: bool = False, instruction: Optional[str] = None,
14
lexer: Optional[Lexer] = None, **kwargs) -> Question:
15
"""
16
Create a text input prompt.
17
18
Args:
19
message: The question/prompt text to display
20
default: Default value pre-filled in input
21
validate: Validator function or Validator instance for input validation
22
qmark: Question prefix symbol (default "?")
23
style: Custom styling configuration
24
multiline: Enable multiline input mode
25
instruction: Additional instruction text displayed below prompt
26
lexer: Syntax highlighting lexer for multiline input
27
**kwargs: Additional prompt_toolkit arguments
28
29
Returns:
30
Question instance ready for execution
31
"""
32
```
33
34
### Password Input
35
36
Prompts for password or sensitive text input with characters masked/hidden during typing.
37
38
```python { .api }
39
def password(message: str, default: str = "", validate: Any = None,
40
qmark: str = "?", style: Optional[Style] = None,
41
**kwargs) -> Question:
42
"""
43
Create a password input prompt with hidden text.
44
45
Args:
46
message: The question/prompt text to display
47
default: Default value (not recommended for passwords)
48
validate: Validator function or Validator instance
49
qmark: Question prefix symbol (default "?")
50
style: Custom styling configuration
51
**kwargs: Additional prompt_toolkit arguments
52
53
Returns:
54
Question instance ready for execution
55
"""
56
```
57
58
## Usage Examples
59
60
### Simple Text Input
61
62
```python
63
import questionary
64
65
# Basic text input
66
name = questionary.text("Enter your name:").ask()
67
68
# Text input with default value
69
city = questionary.text("Enter your city:", default="New York").ask()
70
71
# Text input with validation
72
def validate_email(text):
73
if "@" not in text:
74
return "Please enter a valid email address"
75
return True
76
77
email = questionary.text(
78
"Enter your email:",
79
validate=validate_email
80
).ask()
81
```
82
83
### Password Input
84
85
```python
86
import questionary
87
88
# Basic password input
89
password = questionary.password("Enter password:").ask()
90
91
# Password with validation
92
def validate_password(password):
93
if len(password) < 8:
94
return "Password must be at least 8 characters"
95
return True
96
97
secure_password = questionary.password(
98
"Create password:",
99
validate=validate_password
100
).ask()
101
```
102
103
### Multiline Text Input
104
105
```python
106
import questionary
107
108
# Multiline text input
109
description = questionary.text(
110
"Enter description:",
111
multiline=True,
112
instruction="Press ESC then ENTER to submit"
113
).ask()
114
115
# Multiline with syntax highlighting
116
code = questionary.text(
117
"Enter Python code:",
118
multiline=True,
119
lexer=PythonLexer() # Requires pygments
120
).ask()
121
```
122
123
### Advanced Validation
124
125
```python
126
import questionary
127
from questionary import Validator, ValidationError
128
129
# Custom validator class
130
class NumberValidator(Validator):
131
def validate(self, document):
132
try:
133
int(document.text)
134
except ValueError:
135
raise ValidationError(
136
message="Please enter a valid number",
137
cursor_position=len(document.text)
138
)
139
140
age = questionary.text(
141
"Enter your age:",
142
validate=NumberValidator()
143
).ask()
144
145
# Lambda validation
146
phone = questionary.text(
147
"Enter phone number:",
148
validate=lambda x: "Invalid format" if not x.isdigit() else True
149
).ask()
150
```
151
152
### Custom Styling
153
154
```python
155
import questionary
156
from prompt_toolkit.styles import Style
157
158
custom_style = Style([
159
('question', 'bold'),
160
('answer', 'fg:#ff0066 bold'),
161
('pointer', 'fg:#673ab7 bold'),
162
('highlighted', 'fg:#673ab7 bold'),
163
])
164
165
name = questionary.text(
166
"Stylized prompt:",
167
style=custom_style
168
).ask()
169
```