prompt_smart_menu package

Submodules

prompt_smart_menu.helpers module

Shared helper classes.

exception prompt_smart_menu.helpers.InvalidArgError(ex: Exception)[source]

Bases: Exception

Invalid argument. Wrapper around various built-in exceptions.

class prompt_smart_menu.helpers.Kwarg(key: str, value)[source]

Bases: object

Represents a keyword argument as a key and value.

key() → str[source]

Return key.

value(val=None)[source]

Return or set value.

class prompt_smart_menu.helpers.NestedDict(nest: dict)[source]

Bases: object

A wrapper around a nested dict.

Dict should be of the format that can be given to prompt_toolkit function:
NestedCompleter.from_nested_dict(dict)
nest

Nest property.

prompt_smart_menu.input_parser module

Parse a command string into arguments.

class prompt_smart_menu.input_parser.DefaultCast[source]

Bases: object

Dummy cast does nothing.

static type_cast(item: str) → str[source]

Cast dummy: does nothing.

class prompt_smart_menu.input_parser.InputParser(*args)[source]

Bases: object

For parsing a command string into desired types or objects.

parse(input_string: str, recurse: bool = False) → list[source]

Parse an argument from a command string.

Parameters:
  • input_string (str) – command string to be prased
  • recurse (bool) – If true, the entire input_string is parsed. Otherwise, only the first argument is parsed. Default: False
Returns:

A list of parsed commands. If recurse=False, the list has

two items. The first is the parsed argument, the second is the remaining command string.

Return type:

list

class prompt_smart_menu.input_parser.KwargCast[source]

Bases: object

Cast a string to be used as a keyword argument.

String must match the following regex: r’–(w+)=(.*)’ The key must also be a valid python identifier.

identifier_re = re.compile('^[^\\d\\W]\\w*\\Z')
kwarg_re = re.compile('--(\\w+)=(.*)')
classmethod type_cast(item: str) → prompt_smart_menu.helpers.Kwarg[source]

Cast a string to be used as a keyword argument.

class prompt_smart_menu.input_parser.NumberCast[source]

Bases: object

Cast a string to an int or float following standard python logic.

  • ‘4’ -> int(4)
  • ‘4.0’ -> int(4)

Will cast a Kwarg’s value.

classmethod type_cast(item: Union[str, prompt_smart_menu.helpers.Kwarg]) → Union[int, float, str][source]

Cast item, or Kwarg value, to int or float.

prompt_smart_menu.smart_menu module

Build a command line menu declaratively.

class prompt_smart_menu.smart_menu.MenuNode(*, command: str, function: Callable = None, children: Union[List[dict], List[str], prompt_smart_menu.helpers.NestedDict] = None, parser: prompt_smart_menu.input_parser.InputParser = <prompt_smart_menu.input_parser.InputParser object>, validate_args: bool = False)[source]

Bases: object

The MenuNode class for PromptSmartMenu sub/commands.

Don’t use this class directly. Initialize with PromptSmartMenu.

get_menu() → dict[source]

Recursively build menu for auto-completion.

process_arg(args_str: str)[source]

Parse an argument from a command string and process appropriately.

If this MenuNode has a function, the entire command string is parsed and sent to the function as arguments. Otherwise, a single argument is parsed and the remaining command string is sent to the appropriate child MenuNode, if it exists.

Parameters:args_str (str) – The command string to be parsed and processed.
Raises:InvalidArgError – Raised in a few situations. If this MenuNode is not an end-point but was given no commands. If not an end-point and given a non-existent subcommand. If validate_args=True and arguments are invalid.
class prompt_smart_menu.smart_menu.PromptSmartMenu(menu_config: List[dict], parser: prompt_smart_menu.input_parser.InputParser = <prompt_smart_menu.input_parser.InputParser object>, validate_args: bool = False)[source]

Bases: object

The main PromptSmartMenu class.

Declare a menu’s configuration as list of menu_node dicts. Initialize a PromptSmartMenu object with this configuration to generate a dict for auto-suggestion and to execute a command string against your menu.

nested_completer_dict() → dict[source]

Return a dict for prompt_toolkit.NestedCompleter.

run(input_string: str)[source]

Run a command string against with your menu.

prompt_smart_menu.smart_menu.is_list_of_dicts(li: list) → bool[source]

Return true if input is a list of dicts.

prompt_smart_menu.smart_menu.is_list_of_kwargs(li: list) → bool[source]

Return true if input is a list of Kwargs.

prompt_smart_menu.smart_menu.is_list_of_strings(li: list) → bool[source]

Return true if input is a list of strings.

Module contents

prompt_smart_menu

Description: prompt_smart_menu is a library for building command line menus for
terminal applications in Python. It allows you to create a multi-tiered argument parsing menu for executing different subcommands of a command line application.

See documentation for usage.

class prompt_smart_menu.NestedDict(nest: dict)[source]

Bases: object

A wrapper around a nested dict.

Dict should be of the format that can be given to prompt_toolkit function:
NestedCompleter.from_nested_dict(dict)
nest

Nest property.

class prompt_smart_menu.PromptSmartMenu(menu_config: List[dict], parser: prompt_smart_menu.input_parser.InputParser = <prompt_smart_menu.input_parser.InputParser object>, validate_args: bool = False)[source]

Bases: object

The main PromptSmartMenu class.

Declare a menu’s configuration as list of menu_node dicts. Initialize a PromptSmartMenu object with this configuration to generate a dict for auto-suggestion and to execute a command string against your menu.

nested_completer_dict() → dict[source]

Return a dict for prompt_toolkit.NestedCompleter.

run(input_string: str)[source]

Run a command string against with your menu.