forked from CITGuru/PyInquirer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.py
48 lines (40 loc) · 1.36 KB
/
input.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
"""
* Input prompt example
* run example by writing `python example/input.py` in your console
"""
from __future__ import print_function, unicode_literals
import regex
from pprint import pprint
from prompt_toolkit.validation import Validator, ValidationError
from PyInquirer import prompt
from examples import custom_style_2
class PhoneNumberValidator(Validator):
def validate(self, document):
ok = regex.match('^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$', document.text)
if not ok:
raise ValidationError(
message='Please enter a valid phone number',
cursor_position=len(document.text)) # Move cursor to end
questions = [
{
'type': 'input',
'name': 'first_name',
'message': 'What\'s your first name',
},
{
'type': 'input',
'name': 'last_name',
'message': 'What\'s your last name',
'default': lambda answers: 'Smith' if answers['first_name'] == 'Dave' else 'Doe',
'validate': lambda val: val == 'Doe' or 'is your last name Doe?'
},
{
'type': 'input',
'name': 'phone',
'message': 'What\'s your phone number',
'validate': PhoneNumberValidator
}
]
answers = prompt.prompt(questions, style=custom_style_2)
pprint(answers)