Usage

class clui.base_clui(**kwargs)

This is the base class for a command line user interface.

You can control the style of the interface with the attributes AND OR parameters provided in this class. All are optional. Some more than others.

add(**kwargs)

This method adds menu options to the menu.

execute()

Mainloop of the clui.

Only hit this after you have added all of the options for your menu. It will enter a loop, and it will break in only three scenarios:

  1. The user’s input matches one of the exit words patterns.
  2. One of the user defined condition tests returns False
  3. The user exists the terminal/shell

Condition tests

You can pass user defined condition tests to clui, if you need more fine tuned control over your clui.

Condition tests are used to regulate the loopage of the
program, by returning, or not returning, the set condition.

Note

The default condition is True, but you can change that to any thing you want, but make sure you can match that value.

Requirements of condition tests

  1. Must take two positional arguments.

    The first positional argument is the last string that the user entered.

    The second positional argument is an integer representation of the number of loops the clui has gone through.

  2. Must return a condition, or a list/tuple with a condition in it.

    If a list or tuple is used, the condition must be the first item in the list.

    Everything value in the container after that will be printed to the screen!

An example

from clui import base_clui

def my_condition_test(user_input,looped):
    "A condition test function for a clui."

    if looped > 3: #Looped is an integer, that increases by one after each loop
        return (False,'test failed',"I don't want you to run after 3 loops!")

    return (True,'Test passed') #IMPORTANT - without returning the set condition, your loop would fail.

def a_function(): #This is a generic callable. Just for fun.
    print "I am useless!"

ui = base_clui()
ui.title = 'Testing'
ui.initial_message = "Doin' some test!"
ui.condition_tests = [my_condition_test]

ui.add(display_name='option',callables=[a_function,'Wheee!'])
ui.add(display_name='moar stuff',patterns=['^moar *(stuff)?$'],callables=[a_function])
ui.execute()

Would give you this:

_images/condition_test.png

Exit callables

Clui lets you define exit callables, which are called before your clui terminates.

They are defined in the BASE_CLUI(link) attributes, but can also be passed as named arguments

Clear

Project Versions

Table Of Contents

Previous topic

Example Usage

Next topic

Additional information

This Page