prompt

Input and output functions


Functions

f input_object(prompt_text, cast, default=None, castarg=[], castkwarg={}) ...

Gets input from the command line and validates it.

prompt_text
A string. Used to prompt the user.
cast

This can be any callable object (class, function, type, etc). It simply calls the cast with the given arguements and returns the result. If a ValueError is raised, it will output an error message and prompt the user again.

Because some builtin python objects don't do casting in the way that we might like you can easily write a wrapper function that looks and the input and returns the appropriate object or exception. Look in the cast submodule for examples.

default
function returns this value if the user types nothing in. This is can be used to cancel the input so-to-speek
castarg, castkwarg
list and dictionary. Extra arguments passed on to the cast.

f query(prompt_text, answers, default=None, list_values=False, ignorecase=True) ...

Preset a few options

The prompt_text argument is a string, nothing magical.

The answers argument accepts input in two different forms. The simpler form (a tuple with strings) looks like:

('Male','Female')

And it will pop up a question asking the user for a gender and requiring the user to enter either 'male' or 'female' (case doesn't matter unless you set the third arguement to false). The other form is something like:

({'values':('Male','M'),'fg':'cyan'},

{'values':('Female','F'),'fg':'magenta'})

This will pop up a question with Male/Female (each with appropriate colouring). Additionally, if the user types in just 'M', it will be treated as if 'Male' was typed in. The first item in the 'values' tuple is treated as default and is the one that is returned by the function if the user chooses one in that group. In addition the function can handle non-string objects quite fine. It simple displays the output object.__str__() and compares the user's input against that. So the the code

query("Python rocks? ",(True, False))

will return a bool (True) when the user types in the string 'True' (Of course there isn't any other reasonable answer than True anyways :P)

default is the value function returns if the user types nothing in. This is can be used to cancel the input so-to-speek

Using list_values = False will display a list, with descriptions printed out from the 'desc' keyword

f file_chooser(prompt_text='Enter File: ', default=None, filearg=[], filekwarg={}) ...

A simple tool to get a file from the user. Takes keyworded arguemnts and passes them to open().

If the user enters nothing the function will return the default value. Otherwise it continues to prompt the user until it get's a decent response.

filekwarg may contain arguements passed on to open().

Modules

The terminate.prompt module exposes 3 submodules:

casts
A series of functions that provide useful casts from strings into other common objects.
queries
Example values for the answers argument in prompt.query()
rl
Readline related stuff

See the source for more information.