Skip to content

A tiny utility aiming to simplify and speed up the creation of command-line menus in Python.

License

Notifications You must be signed in to change notification settings

QiuDev/TinyMenu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TinyMenu

made-with-python HitCount Maintenance Ask Me Anything ! Open Source Love

TinyMenu is a simple yet effective utility to simplify and speed up the creation of customizable command-line menus in Python. It is marked by its plain and basic functionality, leaving implementation details up to the developer.

Usage

In order to use TinyMenu functionality the following import statement is required:

import tinymenu

Creating the menu

The code snippet below will create a basic command-line menu containg one command. An explanation and other available attributes and functions are documented below.

def handle_help_command(cmd, *args):
    """
    Function that is called when user enters 'help' command
    """
    print('You entered the help command!')

menu = tinymenu.TinyMenu()
menu.prompt = '$> '

command = tinymenu.Command('help', handle_help_command)
command.arg_limit = [0] # Don't allow any command arguments

menu.add_command(command)
menu.run()
Output:

$> help
You entered the help command!

TinyMenuclass:

  • prompt (expects string) - Stores prompt to be displayed (e.g.: $> or ~/home>)
  • interrupt_handler (expects method/function) - Stores method/function to be called on KeyboardInterrupt
    • e.g.: def handle_interrupt()
  • unknown_command_handler (expects method/function) - Stores method/function to be called when an unknown command was entered
    • 1st argument: entered command [string]
    • 2nd argument: command arguments [*string]
    • e.g.: def handle_unknown_command(cmd, *cmd_arguments)
  • invalid_args_handler (expects method/function) - Stores method/function to be called when a command with invalid command arguments was entered
    • 1st argument: entered command [Command (see below)]
    • 2nd argument command arguments [string]
    • e.g.: def handle_invalid_args(cmd, *cmd_arguments)
  • add_command(command) - Adds a command to the menu
    • command - Command (see below) to be added
  • remove_command(command) - Removes a command from the menu
    • command - Command (see below) to be added
  • run() - Starts the menu and listens for input

Commandclass:

  • __init__(command, handler) - Initialization of Command instance
  • command (expects string) - Stores name of the command (string to be entered in menu)
  • handler (expects method/function) - Stores method/function to be called when command entered
    • 1st argument: entered command [Command]
    • 2nd argument: command arguments [*string]
  • arg_limit (expects list of integers) - Stores possible amounts of arguments (None = no limit)

Dependencies & Compatibility

TinyMenu does not have any dependencies and is compatible with Python 2 & Python 3

About

A tiny utility aiming to simplify and speed up the creation of command-line menus in Python.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages