From 91eb98301496a5dae1fe75dcbcb77abe3fb4135a Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sat, 24 Oct 2009 07:06:38 +0000 Subject: Bug #134466 - Add a --ask-enter-invalid option. When used together with the --ask option, interpret a single "Enter" key press as invalid input. This helps prevent accidental acceptance of the first choice. (trunk r14710) svn path=/main/branches/2.1.7/; revision=14719 --- pym/_emerge/userquery.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'pym/_emerge/userquery.py') diff --git a/pym/_emerge/userquery.py b/pym/_emerge/userquery.py index 5da236007..f177c9e87 100644 --- a/pym/_emerge/userquery.py +++ b/pym/_emerge/userquery.py @@ -8,11 +8,12 @@ import sys from portage.output import bold, create_color_func -def userquery(prompt, responses=None, colours=None): +def userquery(prompt, enter_invalid, responses=None, colours=None): """Displays a prompt and a set of responses, then waits for a response which is checked against the responses and the first to match is - returned. An empty response will match the first value in responses. The - input buffer is *not* cleared prior to the prompt! + returned. An empty response will match the first value in responses, + unless enter_invalid is True. The input buffer is *not* cleared prior + to the prompt! prompt: a String. responses: a List of Strings. @@ -42,10 +43,12 @@ def userquery(prompt, responses=None, colours=None): response=input("["+"/".join([colours[i](responses[i]) for i in range(len(responses))])+"] ") else: response=raw_input("["+"/".join([colours[i](responses[i]) for i in range(len(responses))])+"] ") - for key in responses: - # An empty response will match the first value in responses. - if response.upper()==key[:len(response)].upper(): - return key + if response or not enter_invalid: + for key in responses: + # An empty response will match the + # first value in responses. + if response.upper()==key[:len(response)].upper(): + return key print("Sorry, response '%s' not understood." % response, end=' ') except (EOFError, KeyboardInterrupt): print("Interrupted.") -- cgit v1.2.3-1-g7c22