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.?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With