Skip to main content

A way to simplify development of CLI applications

Project description


A way to simplify development of CLI applications.

Getting started

This module is based on the finite state machine pattern. It may be logic to represent a list of options in the terminal (a page) as a state. So the other lists of options (pages) are the other states. The program can be represented as an oriented graph with mappings from one state to another one.
Further the terms "menu" and "option" are interchangeable with the terms "state" and "mapping".

Create the program

Use the Program class to create the program object which is singleton. You can initialize the program with a Menu or append it later.

from FiniteConsole import Program, Menu

p = Program()  # Empty
p.init_menu = Menu('main')

p = Program(Menu('initial'))

The program can be dropped with the drop method.

Define a state (a menu)

The states are instances of Menu class.
The constructor takes a required parameter id_ which is its id. It must be unique for a whole program!

from FiniteConsole import Menu

main_menu = Menu('main')

After you have created the Program instance, all new Menu instances will be appended to the program automatically.

from FiniteConsole import Program, Menu

p = Program(Menu('main'))
# {'main': <Menu object...>, '1': <Menu object...>, 'another_one': <Menu object...>}

The second parameter action is a function, basically it is None. It means the state is not finite.
Finite states do something and then returns to the precedent state.

import sys
from FiniteConsole import Menu

main_menu = Menu('main')
exit_menu = Menu('exit', lambda: sys.exit())

If the program state changes on "exit", the function will be executed.
Parameters are passed to the function via args and kwargs attributes of the program, they are cleared at every loop iteration. Returned value is in the result attribute of the program, it is conserved until the function returns a new value.

from FiniteConsole import Program, Menu

finite = Menu('calc', lambda x: x*x)
p = Program(finite)
p.start_loop()  # E.g. as a thread
p.args = [5]
# 25

To remove states use remove_menus method of the Program instance. You can pass either str containing ids or Menu instances.

Append a mapping (an option)

Use append_options of Menu instance to append options to the menu. It takes Option object, which parameters are inp - input name, out - output name, description - text for CLI.

import sys
from FiniteConsole import Menu, Option

main_menu = Menu('main').append_options(
    Option('1', 'exit', 'Quit'),
    Option('2', 'do_stuff_1', 'Amazing stuff'),
    Option('yes', 'Another menu', 'Go to another menu, etc.')

To remove options use remove_options method of a Menu instance.


Just use start_loop and stop_loop methods to run and stop the program.
If your graph has faults, e.g. options leading nowhere, you will see report about these faults in the CLI.


You can change representation of options and input method.


Redefine the method render of the Menu class. It is responsible for CLI presentation of menus.

Input for mapper

Redefine the method read_input of the Menu class. It is responsible for getting of a new input for mapping. The returned value must be a str with id of a new state.


import sys
from FiniteConsole import Menu, Option, Program

def func():
    x = float(input('Enter a number: '))

main_menu = Menu('main').append_options(
    Option('0', 'exit', 'Quit'),  # Finite
    Option('1', 'stuff_1', 'Square'),  # Finite
    Option('3', 'submenu', 'Go to another menu')
p = Program(main_menu)

Menu('submenu').append_options(Option('back', 'main', 'Go back'))

# Finite states
Menu('exit', lambda: sys.exit())
Menu('stuff_1', func)


And yes. You can change program's comportment while runtime: append other menus, options, etc.

Project details

Release history Release notifications

This version


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for FiniteConsole, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size FiniteConsole-1.0.2-py3-none-any.whl (6.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size FiniteConsole-1.0.2.tar.gz (5.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page