text_question_validator.py 685 B

12345678910111213141516171819202122232425262728
  1. from PyInquirer import prompt, Validator, ValidationError
  2. from prompt_toolkit import document
  3. import regex
  4. class NickNameValidator(Validator):
  5. def validate(self, document: document.Document) -> None:
  6. ok = regex.match('^[A-Z][a-z]+$', document.text)
  7. if not ok:
  8. raise ValidationError(message = 'Please enter a correct name, with a uppercase and letters', cursor_position = len(document.text))
  9. widget = [
  10. {
  11. 'type':'input',
  12. 'name':'nickname',
  13. 'message':'Choose a nickname :',
  14. 'validate':NickNameValidator
  15. }
  16. ]
  17. try :
  18. result = prompt(widget)
  19. except ValueError :
  20. print('Pb !!!')
  21. exit()
  22. print('Your answer is')
  23. print(result["nickname"])
  24. print(type(result["nickname"]))