Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python library for user input

I am implementing a small command line tool in python that needs to ask the user a couple of questions. I use

raw_input('Are you male or female?')

all the time. Now I would like to be able to deal with dumb users (or those too lazy to read/remember the documentation), so I need to check whether the answer makes sense.

gender = ''
while gender not in ['male', 'female']:
    gender = raw_input('Are you male or female?')

I am wondering whether there exists something like argparse that would automate this problem, something like

import inputparse 
gender = inputparse.get_input(prompt='Are you male or female?', type=str, possible_input=['male', 'female'])

and would take care of automatic checking etc.?

like image 217
Hans Avatar asked Jun 02 '13 16:06

Hans


2 Answers

This question is quite old, but I'm researching it today. The library pyinputplus is recommended by Al Swigert in Automate the Boring Stuff With Python

like image 88
user6045 Avatar answered Sep 29 '22 20:09

user6045


From the accepted answer to this question: the cmd library might be of interest to you.

"The Cmd class provides a simple framework for writing line-oriented command interpreters."

This Python Module of the Week page features it, and it has some examples and explanations.

like image 22
A.Wan Avatar answered Sep 29 '22 18:09

A.Wan