Asks a question and returns a boolean (y/n) answer. If default is given (one of 'y','n'), it is used if the user input is empty. If interrupt is given (one of 'y','n'), it is used if the user presses Ctrl-C. Otherwise the question is repeated until an answer is given. An EOF is
(prompt, default=None, interrupt=None)
| 155 | |
| 156 | |
| 157 | def ask_yes_no(prompt, default=None, interrupt=None): |
| 158 | """Asks a question and returns a boolean (y/n) answer. |
| 159 | |
| 160 | If default is given (one of 'y','n'), it is used if the user input is |
| 161 | empty. If interrupt is given (one of 'y','n'), it is used if the user |
| 162 | presses Ctrl-C. Otherwise the question is repeated until an answer is |
| 163 | given. |
| 164 | |
| 165 | An EOF is treated as the default answer. If there is no default, an |
| 166 | exception is raised to prevent infinite loops. |
| 167 | |
| 168 | Valid answers are: y/yes/n/no (match is not case sensitive).""" |
| 169 | |
| 170 | answers = {'y':True,'n':False,'yes':True,'no':False} |
| 171 | ans = None |
| 172 | while ans not in answers.keys(): |
| 173 | try: |
| 174 | ans = input(prompt+' ').lower() |
| 175 | if not ans: # response was an empty string |
| 176 | ans = default |
| 177 | except KeyboardInterrupt: |
| 178 | if interrupt: |
| 179 | ans = interrupt |
| 180 | print("\r") |
| 181 | except EOFError: |
| 182 | if default in answers.keys(): |
| 183 | ans = default |
| 184 | print() |
| 185 | else: |
| 186 | raise |
| 187 | |
| 188 | return answers[ans] |
| 189 | |
| 190 | |
| 191 | def temp_pyfile(src, ext='.py'): |
no test coverage detected