Module to read input from commandline

J

james

Hi all,

I did some quick searching but I haven't found anything like I want.
It probably has to do with the terms I am searching for so if I
describe what I want then I hope someone can point me to a good
module.

I want to take input from the user at the command line. e.g.)
Would you like to continue [Y/n]: y
What is your favorite color: green
.....

You get the idea. Basic question answer stuff. It should handle
default options, case insensitivity, etc. Perhaps the module would
compare the inputted text against a regexp for validation. If the
module had an interface similar to OptParse that would be nice.

Anyone know of a decent module that handles this type of thing?
Writing from scratch would be simple but why re-invent the wheel.

Cheers,
James.
 
T

thashooski

What you're looking for is no module, it is included in the standard
python namespace.

raw_input

Use it like this:

value_a = raw_input("Please give a value for a: ")
# do your thing to value_a

But this belongs to the real basics, I suggest you get some reading
done on python.

GL
 
J

james

What you're looking for is no module, it is included in the standard
python namespace.

raw_input

Use it like this:

value_a = raw_input("Please give a value for a: ")
# do your thing to value_a

But this belongs to the real basics, I suggest you get some reading
done on python.

GL

Thanks for the response GL.

What I am looking for is a module to wrap raw_input so it can handle
some of the validation.

e.g.)
response = display_prompt( question, valid_responses,
default_response )

or similar. valid_responses could be a tuple of regexp strings that
would compare against a raw_input and return one in the list. Ideally
I could customize the error message (for responses not in
valid_response), etc.

I know it isn't rocket science to write it and I have something
already in place. I'd rather use a module built for the purpose now
that I have several scripts that will use the functionality.

Thanks!
James.

FYI: This is what I have so far:

from re import compile

def yes_no(question, default=None):
"""Prompt the user with a yes or no question."""
if default == "y":
question = "".join((question, " [Y/n]: "))
elif default == "n":
question = "".join((question, " [y/N]: "))
else:
question = "".join((question, " [y/n]: "))

valid_answer = "[yn]"
invalid_message = "Answer must be y or n"
answer = prompt(question, valid_answer, invalid_message, default)
return answer == 'y'

def prompt(question, valid_answer, invalid_message, default=None):
"""Prompt the user with a question and validate their response."""
is_valid = False;
compiled_valid_answers = compile(valid_answer)
while not is_valid:
answer = raw_input(question).lower()
if answer == "" and default is not None:
answer = default
is_valid = compiled_valid_answers.match(answer)
if not is_valid:
print invalid_message
return answer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top