tools/perf/core/cli_helpers.py
def Ask(question, answers=None, default=None):
  """Asks the user to answer a question with multiple choices.

  Users are able to press Return to access the default answer (if specified) and
  to type part of the full answer, e.g. "y", "ye" or "yes" are all valid answers
  for "yes". The func will ask user again in case an invalid answer is provided.

  Raises ValueError if default is specified, but not listed an a valid answer.

  Args:
    question: Question to be asked.
    answers: List or dictinary describing user choices. In case of a dictionary,
        the keys are the options display to the user and values are the return
        values for this method. In case of a list, returned values are same as
        options displayed to the user. When presenting to the user, the order of
        answers is preserved if list is used, otherwise answers are sorted
        alphabetically. Defaults to {'yes': True, 'no': False}.
    default: Default option chosen on empty answer. Defaults to 'yes' if default
        value is used for answers parameter or to lack of default answer
        otherwise.

  Returns:
    Chosen option from answers. Full option name is returned even if user only
    enters part of it or chooses the default.
  """
  if answers is None:
    answers = {'yes': True, 'no': False}
    default = 'yes'
  if isinstance(answers, list):
    ordered_answers = answers
    answers = {v: v for v in answers}
  else:
    ordered_answers = sorted(answers)

  # Generate a set of prefixes for all answers such that the user can type just
  # the minimum number of characters required, e.g. 'y' or 'ye' can be used for
  # the 'yes' answer. Shared prefixes are ignored, e.g. 'n' and 'ne' will not be
  # accepted if 'negate' and 'next' are both valid answers, whereas 'nex' and
  # 'neg' would be accepted.
  inputs = {}
  common_prefixes = set()
  for ans, retval in answers.items():
    for i in range(len(ans)):
      inp = ans[:i+1]
      if inp in inputs:
        common_prefixes.add(inp)
        del inputs[inp]
      if inp not in common_prefixes:
        inputs[inp] = retval

  if default is None:
    prompt = ' [%s] ' % '/'.join(ordered_answers)
  elif default in answers:
    ans_with_def = (a if a != default else a.upper() for a in ordered_answers)
    prompt = ' [%s] ' % '/'.join(ans_with_def)
  else:
    raise ValueError('invalid default answer: "%s"' % default)

  while True:
    print(Colored(question + prompt, 'cyan'), end=' ')
    choice = input().strip().lower()
    if default is not None and choice == '':
      return inputs[default]
    elif choice in inputs:
      return inputs[choice]
    else:
      choices = sorted(['"%s"' % a for a in sorted(answers.keys())])
      Error('Please respond with %s or %s.' % (
        ', '.join(choices[:-1]), choices[-1]))
Asks the user to answer a question with multiple choices.

  Users are able to press Return to access the default answer (if specified) and
  to type part of the full answer, e.g. "y", "ye" or "yes" are all valid answers
  for "yes". The func will ask user again in case an invalid answer is provided.

  Raises ValueError if default is specified, but not listed an a valid answer.

  Args:
    question: Question to be asked.
    answers: List or dictinary describing user choices. In case of a dictionary,
        the keys are the options display to the user and values are the return
        values for this method. In case of a list, returned values are same as
        options displayed to the user. When presenting to the user, the order of
        answers is preserved if list is used, otherwise answers are sorted
        alphabetically. Defaults to {'yes': True, 'no': False}.
    default: Default option chosen on empty answer. Defaults to 'yes' if default
        value is used for answers parameter or to lack of default answer
        otherwise.

  Returns:
    Chosen option from answers. Full option name is returned even if user only
    enters part of it or chooses the default.
  
Asks the user to answer a question with multiple choices.
Users are able to press Return to access the default answer (if specified) and
to type part of the full answer, e.g.

Raises ValueError if default is specified, but not listed an a valid answer.
Asks the user to answer a question with multiple choices . Users are able to press Return to access the default answer ( if specified ) and to type part of the full answer e . g . Raises ValueError if default is specified but not listed an a valid answer .
def Ask(question, answers=None, default=None):
  if answers is None:
    answers = {'yes': True, 'no': False}
    default = 'yes'
  if isinstance(answers, list):
    ordered_answers = answers
    answers = {v: v for v in answers}
  else:
    ordered_answers = sorted(answers)
  inputs = {}
  common_prefixes = set()
  for ans, retval in answers.items():
    for i in range(len(ans)):
      inp = ans[:i+1]
      if inp in inputs:
        common_prefixes.add(inp)
        del inputs[inp]
      if inp not in common_prefixes:
        inputs[inp] = retval
  if default is None:
    prompt = ' [%s] ' % '/'.join(ordered_answers)
  elif default in answers:
    ans_with_def = (a if a != default else a.upper() for a in ordered_answers)
    prompt = ' [%s] ' % '/'.join(ans_with_def)
  else:
    raise ValueError('invalid default answer: "%s"' % default)
  while True:
    print(Colored(question + prompt, 'cyan'), end=' ')
    choice = input().strip().lower()
    if default is not None and choice == '':
      return inputs[default]
    elif choice in inputs:
      return inputs[choice]
    else:
      choices = sorted(['"%s"' % a for a in sorted(answers.keys())])
      Error('Please respond with %s or %s.' % (
        ', '.join(choices[:-1]), choices[-1]))
def Ask ( question , answers = None , default = None ) : if answers is None : answers = { 'yes' : True , 'no' : False } default = 'yes' if isinstance ( answers , list ) : ordered_answers = answers answers = { v : v for v in answers } else : ordered_answers = sorted ( answers ) inputs = { } common_prefixes = set ( ) for ans , retval in answers . items ( ) : for i in range ( len ( ans ) ) : inp = ans [ : i + 1 ] if inp in inputs : common_prefixes . add ( inp ) del inputs [ inp ] if inp not in common_prefixes : inputs [ inp ] = retval if default is None : prompt = ' [%s] ' % '/' . join ( ordered_answers ) elif default in answers : ans_with_def = ( a if a != default else a . upper ( ) for a in ordered_answers ) prompt = ' [%s] ' % '/' . join ( ans_with_def ) else : raise ValueError ( 'invalid default answer: "%s"' % default ) while True : print ( Colored ( question + prompt , 'cyan' ) , end = ' ' ) choice = input ( ) . strip ( ) . lower ( ) if default is not None and choice == '' : return inputs [ default ] elif choice in inputs : return inputs [ choice ] else : choices = sorted ( [ '"%s"' % a for a in sorted ( answers . keys ( ) ) ] ) Error ( 'Please respond with %s or %s.' % ( ', ' . join ( choices [ : - 1 ] ) , choices [ - 1 ] ) )
Asks the user to answer a question with multiple choices.
Asks the user to answer a question with multiple choices .
"""Asks the user to answer a question with multiple choices.

  Users are able to press Return to access the default answer (if specified) and
  to type part of the full answer, e.g. "y", "ye" or "yes" are all valid answers
  for "yes". The func will ask user again in case an invalid answer is provided.

  Raises ValueError if default is specified, but not listed an a valid answer.

  Args:
    question: Question to be asked.
    answers: List or dictinary describing user choices. In case of a dictionary,
        the keys are the options display to the user and values are the return
        values for this method. In case of a list, returned values are same as
        options displayed to the user. When presenting to the user, the order of
        answers is preserved if list is used, otherwise answers are sorted
        alphabetically. Defaults to {'yes': True, 'no': False}.
    default: Default option chosen on empty answer. Defaults to 'yes' if default
        value is used for answers parameter or to lack of default answer
        otherwise.

  Returns:
    Chosen option from answers. Full option name is returned even if user only
    enters part of it or chooses the default.
  """ # Generate a set of prefixes for all answers such that the user can type just # the minimum number of characters required, e.g. 'y' or 'ye' can be used for # the 'yes' answer. Shared prefixes are ignored, e.g. 'n' and 'ne' will not be # accepted if 'negate' and 'next' are both valid answers, whereas 'nex' and # 'neg' would be accepted.
Chosen option from answers. Full option name is returned even if user only
enters part of it or chooses the default.
Chosen option from answers . Full option name is returned even if user only enters part of it or chooses the default .
question
Question to be asked.
Question to be asked .
answers
List or dictinary describing user choices. In case of a dictionary,
the keys are the options display to the user and values are the return
values for this method. In case of a list, returned values are same as
options displayed to the user. When presenting to the user, the order of
answers is preserved if list is used, otherwise answers are sorted
alphabetically. Defaults to {'yes': True, 'no': False}.
List or dictinary describing user choices . In case of a dictionary the keys are the options display to the user and values are the return values for this method . In case of a list returned values are same as options displayed to the user . When presenting to the user the order of answers is preserved if list is used otherwise answers are sorted alphabetically . Defaults to { ' yes ' : True ' no ' : False } .
default
Default option chosen on empty answer. Defaults to 'yes' if default
value is used for answers parameter or to lack of default answer
otherwise.
Default option chosen on empty answer . Defaults to ' yes ' if default value is used for answers parameter or to lack of default answer otherwise .