Building a Menu

A menu is constructed as a list of menu_node dicts. Each menu node is equivalent to a command (or subcommand).

PromptSmartMenu

Once a menu’s configuration has been declared. Simply initialize a PromptSmartMenu with it. The parser and validate_args options can also be declared here; in most situations this makes the most sense.

from prompt_smart_menu import PromptSmartMenu
from prompt_smart_menu.input_parser import InputParser, KwargCast, NumberCast

menu_config = [
    {
        'command': 'exit',
        'function': exit,
    },
    {
        'command': 'show',
        'children': [
            {
                'command': 'version',
                'function': show_version,
            },
            {
                'command': 'clock',
                'function': show_clock
            }
        ]
    }
]

psm = PromptSmartMenu(
        menu_config,
        parser=InputParser(KwargCast, NumberCast),
        validate_args=True)