Skip to content

Instantly share code, notes, and snippets.

@CITGuru
Created August 7, 2020 20:01
Show Gist options
  • Save CITGuru/ba824624d3a5cc9c65f1aac9e720774d to your computer and use it in GitHub Desktop.
Save CITGuru/ba824624d3a5cc9c65f1aac9e720774d to your computer and use it in GitHub Desktop.
# cli.py
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import six
import os, re
from PyInquirer import style_from_dict, Token, prompt, print_json, Separator
from PyInquirer import Validator, ValidationError
from pprint import pprint
try:
import colorama
colorama.init()
except ImportError:
colorama = None
try:
from termcolor import colored
except ImportError:
colored = None
style = style_from_dict({
Token.QuestionMark: '#673ab7 bold',
Token.Answer: '#f44336 bold',
Token.Instruction: '', # default
Token.Separator: '#cc5454',
Token.Selected: '#cc5454', # default
Token.Pointer: '#673ab7 bold',
Token.Question: '',
})
class EmptyValidator(Validator):
def validate(self, value):
if len(value.text):
return True
else:
raise ValidationError(message="You can't leave this blank", cursor_position=len(value.text))
class EmailValidator(Validator):
pattern = r"\"?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)\"?"
def validate(self, email):
if len(email.text):
if re.match(self.pattern, email.text):
return True
else:
raise ValidationError(message="Invalid email",cursor_position=len(email.text))
else:
raise ValidationError(message="You can't leave this blank",cursor_position=len(email.text))
def take_user_order():
questions = [{
'type': 'list',
'name': 'name',
'message': 'Name (Product):',
'choices': ["Rice", "Milk", "Chocolate", "Pizza", "Taco", "Zobo", "Boli"]
},
{
'type': 'input',
'name': 'customer_email',
'message': "Customer's Email:",
'validate': EmailValidator,
},
{
'type': 'input',
'name': 'customer_name',
'message': "Customer's Name:",
'validate': EmptyValidator,
},
{
'type': 'input',
'name': 'quantity',
'message': "Quantity:",
'validate': EmptyValidator,
},
{
'type': 'input',
'name': 'customer_address',
'message': "Customer's Address:",
'validate': EmptyValidator,
'multiline': True
}]
answers = prompt(questions, style=style)
return answers
answer = take_user_order() # to initialize the prompt
print(answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment