def user_choice_from_list(choices, pre_prompt_message, input_message,
                          error_message_none, error_message_invalid):
    """Prompts a user for a choice from a list.

    If there is only one choice, does not prompt the user.

    Args:
        choices: List of strings the user can choose from.
        pre_prompt_message: String; a message to print before prompting.
        input_message: String; message to print in raw_input prompt.
        error_message_none: String; the error message to use when there are no
            choices.
        error_message_invalid: String; the error message to use when the user
            choice is invalid.

    Returns:
        Value chosen by the user. This can occur either if the user types the
            value or the index of the value in the list.

    Raises:
        GitRvException: If there are no choices.
        GitRvException: If the choice entered by the user is not valid.
    """
    if len(choices) == 0:
        raise GitRvException(error_message_none)
    elif len(choices) == 1:
        return choices[0]
    else:
        print pre_prompt_message
        options = '\n'.join(['%d: %s' % pair for pair in enumerate(choices)])
        print options
        # Accept index of choice or string value
        choice = raw_input(input_message).strip()
        if choice in choices:
          return choice
        else:
            try:
                index = int(choice)
                return choices[index]
            except (ValueError, TypeError, IndexError):
                raise GitRvException(error_message_invalid % (choice,))
dataset/ETHPy150Open googlearchive/git-rv/utils.py/user_choice_from_list/original