File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ import sys
2
+
3
+ def query_yes_no_quit (question , default = "yes" ):
4
+ """Ask a yes/no/quit question via raw_input() and return their answer.
5
+
6
+ "question" is a string that is presented to the user.
7
+ "default" is the presumed answer if the user just hits <Enter>.
8
+ It must be "yes" (the default), "no", "quit" or None (meaning
9
+ an answer is required of the user).
10
+
11
+ The "answer" return value is one of "yes", "no" or "quit".
12
+ """
13
+ valid = {"yes" :"yes" , "y" :"yes" , "ye" :"yes" ,
14
+ "no" :"no" , "n" :"no" ,
15
+ "quit" :"quit" , "qui" :"quit" , "qu" :"quit" , "q" :"quit" }
16
+ if default == None :
17
+ prompt = " [y/n/q] : "
18
+ elif default == "yes" :
19
+ prompt = " [Y/n/q] : "
20
+ elif default == "no" :
21
+ prompt = " [y/N/q] : "
22
+ elif default == "quit" :
23
+ prompt = " [y/n/Q] : "
24
+ else :
25
+ raise ValueError ("invalid default answer: '%s'" % default )
26
+
27
+ while 1 :
28
+ sys .stdout .write (question + prompt )
29
+ choice = raw_input ().lower ()
30
+ if default is not None and choice == '' :
31
+ return default
32
+ elif choice in valid .keys ():
33
+ return valid [choice ]
34
+ else :
35
+ sys .stdout .write ("Please respond with 'yes', 'no' or 'quit'.\n " )
36
+
37
+ if __name__ == '__main__' :
38
+ print query_yes_no_quit ("Do you like OCNI ;-) ?" , "no" )
You can’t perform that action at this time.
0 commit comments