import getpass


def get_user(prompt=None):
    """
    Prompts the user for his login name, defaulting to the USER environment
    variable. Returns a string containing the username.
    May throw an exception if EOF is given by the user.

    :type  prompt: str|None
    :param prompt: The user prompt or the default one if None.
    :rtype:  string
    :return: A username.
    """
    # Read username and password.
    try:
        env_user = getpass.getuser()
    except KeyError:
        env_user = ''
    if prompt is None:
        prompt = "Please enter your user name"
    if env_user is None or env_user == '':
        user = input('%s: ' % prompt)
    else:
        user = input('%s [%s]: ' % (prompt, env_user))
        if user == '':
            user = env_user
    return user