snakeoil.cli.input module

Various methods involving user input.

exception snakeoil.cli.input.NoChoice[source]

Bases: KeyboardInterrupt

Raised by userquery if no choice was made.

HACK: this subclasses KeyboardInterrupt, so if you ignore this it should do something reasonable.

snakeoil.cli.input.userquery(prompt, out, err, responses=None, default_answer=None, limit=3)[source]

Ask the user to choose from a set of options.

Displays a prompt and a set of responses, then waits for a response which is checked against the responses. If there is an unambiguous match the value is returned.

If the user does not input a valid response after a number of tries NoChoice is raised. You can catch this if you want to do something special. Because it subclasses C{KeyboardInterrupt} the default behaviour is to abort as if the user hit ctrl+c.

Parameters:
  • responses (mapping with C{basestring} keys and tuple values.) – mapping of user input to function result. The first item in the value tuple is returned, the rest is passed to out. Defaults to:: {‘yes’: (True, out.fg(‘green’), ‘Yes’), ‘no’: (False, out.fg(‘red’), ‘No’)}

  • default_answer – returned if there is no input (user just hits enter). Defaults to True if responses is unset, unused otherwise.

  • limit – number of allowed tries.