Welcome to console-menu’s documentation!¶
Contents:
Usage¶
First things first, import the package:
import consolemenu
Or just import what you need:
from consolemenu import ConsoleMenu
from consolemenu.items import FunctionItem, SubmenuItem, CommandItem
Then create a menu:
menu = ConsoleMenu("This is a menu!", "It has a subtitle too!")
Create menu items for each choice you need:
command_item = CommandItem("Run a console command", "touch hello.txt")
function_item = FunctionItem("Call a function", input, ["Enter some input"])
To add other menus as submenus, use a SubmenuItem
, setting the menu property in the constructor so the submenu’s parent is set properly:
submenu = ConsoleMenu("This is the submenu")
submenu_item = SubmenuItem("Show a submenu", submenu, menu=menu)
Add the items to the menu:
menu.append_item(command_item)
menu.append_item(function_item)
menu.append_item(submenu_item)
Then start the menu:
menu.start()
After that, the menu will spawn its own thread and go about its business. If you want to wait on the user to finish with the menu before continuing, call:
menu.join()
To combine these two and simply show a menu and immediately wait for the user to exit the menu, call:
menu.show()
Getting a selection¶
If you have a list of strings, and you want to allow the user to select one, you can use a
SelectionMenu
:
from consolemenu import SelectionMenu
a_list = ["red", "blue", "green"]
selection = SelectionMenu.get_selection(a_list)
Which is equivalent to:
from consolemenu import SelectionMenu
a_list=["red", "blue", "green"]
menu = SelectionMenu(a_list,"Select an option")
menu.show()
menu.join()
selection = menu.selected_option
API Reference¶
ConsoleMenu — Standard menu class¶
-
class
consolemenu.
ConsoleMenu
(title=None, subtitle=None, screen=None, formatter=None, prologue_text=None, epilogue_text=None, clear_screen=True, show_exit_option=True, exit_option_text='Exit', exit_menu_char=None)[source]¶ A class that displays a menu and allows the user to select an option.
Parameters: - title (str) – The title of the menu, or a method reference that returns a string.
- subtitle (str) – The subtitle of the menu, or a method reference that returns a string.
- screen (
consolemenu.screen.Screen
) – The screen object associated with this menu. - formatter (
MenuFormatBuilder
) – The MenuFormatBuilder instance used to format this menu. - prologue_text (str) – Text or method reference to include in the “prologue” section of the menu.
- epilogue_text (str) – Text or method reference to include in the “epilogue” section of the menu.
- show_exit_option (bool) – Specifies whether this menu should show an exit item by default. Defaults to True. Can be overridden when the menu is started.
- exit_option_text (str) – Text for the Exit menu item. Defaults to ‘Exit’.
- exit_menu_char (str) – Character to use for exiting the menu. Defaults to None.
- clear_screen (bool) – Set to False to disable clearing of screen between menus
-
cls.
currently_active_menu
¶ Class variable that holds the currently active menu or None if no menu is currently active (e.g. when switching between menus)
Type: ConsoleMenu
-
items
¶ The list of MenuItems that the menu will display
Type: list
ofMenuItem
-
parent
¶ The parent of this menu
Type: ConsoleMenu
-
previous_active_menu
¶ the previously active menu to be restored into the class’s currently active menu
Type: ConsoleMenu
-
current_option
¶ The currently highlighted menu option
Type: int
-
selected_option
¶ The option that the user has most recently selected
Type: int
-
current_item
¶ The item corresponding to the menu option that is currently highlighted, or None.
Type: consolemenu.items.MenuItem
-
selected_item
¶ The item in
items
that the user most recently selected, or None.Type: consolemenu.items.MenuItem
-
start
(show_exit_option=None)[source]¶ Start the menu in a new thread and allow the user to interact with it. The thread is a daemon, so
join()
should be called if there’s a possibility that the main thread will exit before the menu is doneParameters: show_exit_option (bool) – Specify whether the exit item should be shown, defaults to the value set in the constructor
-
join
(timeout=None)[source]¶ Should be called at some point after
start()
to block until the menu exits.Parameters: timeout (Number) – How long to wait before timing out.
-
show
(show_exit_option=None)[source]¶ Calls start and then immediately joins.
Parameters: show_exit_option (bool) – Specify whether the exit item should be shown, defaults to the value set in the constructor
Item Management
-
append_item
(item)[source]¶ Add an item to the end of the menu before the exit item.
Parameters: item (MenuItem) – The item to be added.
-
add_exit
()[source]¶ Add the exit item if necessary. Used to make sure there aren’t multiple exit items.
Returns: True if item needed to be added, False otherwise. Return type: bool
-
remove_exit
()[source]¶ Remove the exit item if necessary. Used to make sure we only remove the exit item, not something else.
Returns: True if item needed to be removed, False otherwise. Return type: bool
User interaction
-
get_input
()[source]¶ Can be overridden to change the input method. Called in
process_user_input()
Returns: the ordinal value of a single character Return type: int
-
draw
()[source]¶ Refresh the screen and redraw the menu. Should be called whenever something changes that needs to be redrawn.
-
go_to
(option)[source]¶ Go to the option entered by the user as a number
Parameters: option (int) – the option to go to
State management
-
is_alive
()[source]¶ Check whether the thread is stil alive.
Returns: True if the thread is still alive; False otherwise. Return type: bool
SelectionMenu — Quickly get a selection¶
Bases: consolemenu.ConsoleMenu
-
class
consolemenu.
SelectionMenu
(strings, title=None, subtitle=None, screen=None, formatter=None, prologue_text=None, epilogue_text=None, show_exit_option=True, exit_option_text='Exit', clear_screen=True)[source]¶ A menu that simplifies item creation, just give it a list of strings and it builds the menu for you
Parameters: - strings (
list
ofstr
) – The list of strings this menu should be built from. - title (str) – The title of the menu.
- subtitle (str) – The subtitle of the menu.
- screen (
consolemenu.screen.Screen
) – The screen object associated with this menu. - formatter (
MenuFormatBuilder
) – The MenuFormatBuilder instance used to format this menu. - prologue_text (str) – Text to include in the “prologue” section of the menu.
- epilogue_text (str) – Text to include in the “epilogue” section of the menu.
- show_exit_option (bool) – Specifies whether this menu should show an exit item by default. Defaults to True. Can be overridden when the menu is started.
- exit_option_text (str) – Text for the Exit menu item. Defaults to ‘Exit’.
- clear_screen (bool) – Set to False to disable clearing of screen between menus
-
classmethod
get_selection
(strings, title='Select an option', subtitle=None, show_exit_option=True, _menu=None)[source]¶ Single-method way of getting a selection out of a list of strings.
Parameters: - strings (
list
ofstr
) – The list of strings this menu should be built from. - title (str) – The title of the menu.
- subtitle (str) – The subtitle of the menu.
- show_exit_option (bool) – Specifies whether this menu should show an exit item by default. Defaults to True.
- _menu – Should probably only be used for testing, pass in a list and the created menu used internally by the method will be appended to it
Returns: The index of the selected option.
Return type: int
- strings (
- strings (
MultiSelectMenu — Make multiple selections at one prompt¶
Bases: consolemenu.ConsoleMenu
-
class
consolemenu.
MultiSelectMenu
(title=None, subtitle=None, formatter=None, prologue_text=None, epilogue_text=None, ack_item_completion=True, show_exit_option=True, exit_option_text='Exit', clear_screen=True)[source]¶ Console menu that allows the selection of multiple menu items at a single prompt.
Parameters: - title – The menu title.
- subtitle – The menu subtitle.
- formatter – The menu formatter instance for styling the menu.
- prologue_text – The text to display in the prologue section of the menu.
- epilogue_text – The text to display in the epilogue section of the menu.
- show_exit_option (bool) – Determines if the exit item should be displayed.
- exit_option_text (str) – Text for the Exit menu item. Defaults to ‘Exit’.
- clear_screen (bool) – Set to False to disable clearing of screen between menus
-
append_item
(item)[source]¶ Add an item to the end of the menu before the exit item.
Note that Multi-Select Menus will not allow a SubmenuItem to be added, as multi-select menus are expected to be used only for executing multiple actions.
Parameters: item ( MenuItem
) – The item to be addedRaises: TypeError – If the specified MenuIem is a SubmenuItem.
Items¶
CommandItem¶
Bases: consolemenu.items.ExternalItem
-
class
consolemenu.items.
CommandItem
(text, command, arguments=None, menu=None, should_exit=False, menu_char=None)[source]¶ A menu item to execute a console command
Variables: - text (str) – The text shown for this menu item
- command (str) – The console command to be executed
- arguments (list[str]) – An optional list of string arguments to be passed to the command
- menu (ConsoleMenu) – The menu to which this item belongs
- should_exit (bool) – Whether the menu should exit once this item’s action is done
- menu_char (str) – The character used to select this menu item. Optional - defaults to None.
ExitItem¶
Bases: consolemenu.items.MenuItem
-
class
consolemenu.items.
ExitItem
(text='Exit', menu=None, menu_char=None)[source]¶ Used to exit the current menu. Handled by
consolemenu.ConsoleMenu
ExternalItem¶
Bases: consolemenu.items.MenuItem
FunctionItem¶
Bases: consolemenu.items.ExternalItem
-
class
consolemenu.items.
FunctionItem
(text, function, args=None, kwargs=None, menu=None, should_exit=False, menu_char=None)[source]¶ A menu item to call a Python function
Variables: - text (str) – The text shown for this menu item
- function – The function to be called
- args (list) – An optional list of arguments to be passed to the function
- kwargs (dict) – An optional dictionary of keyword arguments to be passed to the function
- menu (ConsoleMenu) – The menu to which this item belongs
- should_exit (bool) – Whether the menu should exit once this item’s action is done
- menu_char (str) – The character used to select this menu item. Optional - defaults to None.
MenuItem¶
-
class
consolemenu.items.
MenuItem
(text, menu=None, should_exit=False, menu_char=None)[source]¶ A generic menu item
Variables: - text (str) – The text shown for this menu item
- menu (ConsoleMenu) – The menu to which this item belongs
- should_exit (bool) – Whether the menu should exit once this item’s action is done
- menu_char (str) – The character used to select this menu item. Optional - defaults to None.
-
get_return
()[source]¶ Override to change what the item returns. Otherwise just returns the same value the last selected item did.
-
show
(index)[source]¶ How this item should be displayed in the menu. Can be overridden, but should keep the same signature.
Default is:
1 - Item 1
2 - Another Item
Parameters: index (int) – The index of the item in the items list of the menu Returns: The representation of the item to be shown in a menu Return type: str
SelectionItem¶
Bases: consolemenu.items.MenuItem
-
class
consolemenu.items.
SelectionItem
(text, index, menu=None, menu_char=None)[source]¶ The item type used in
consolemenu.SelectionMenu
Variables: - text (str) – The text shown for this menu item
- index (int) – The index of this item in the list used to initialize the
consolemenu.SelectionMenu
- menu (ConsoleMenu) – The menu to which this item belongs
- menu_char (str) – The character used to select this menu item. Optional - defaults to None.
SubmenuItem¶
Bases: consolemenu.items.MenuItem
-
class
consolemenu.items.
SubmenuItem
(text, submenu, menu=None, should_exit=False, menu_char=None)[source]¶ A menu item to open a submenu
Variables: - text (str) – The text shown for this menu item
- submenu (ConsoleMenu) – The submenu to be opened when this item is selected
- menu (ConsoleMenu) – The menu to which this item belongs
- should_exit (bool) – Whether the menu should exit once this item’s action is done
- menu_char (str) – The character used to select this menu item. Optional - defaults to None.
-
get_submenu
()[source]¶ We unwrap the submenu variable in case it is a reference to a method that returns a submenu
-
set_menu
(menu)[source]¶ Sets the menu of this item. Should be used instead of directly accessing the menu attribute for this class.
Parameters: menu (ConsoleMenu) – the menu
Functions¶
-
consolemenu.
clear_terminal
()[source] Call the platform specific function to clear the terminal: cls on windows, reset otherwise
Screen¶
-
class
consolemenu.screen.
Screen
[source]¶ Class representing a console screen.
-
input
(prompt='')[source]¶ Prompt the end user for input.
Parameters: prompt ( str
, optional) – The message to display as the prompt.Returns: The input provided by the user.
-
static
printf
(*args)[source]¶ Print the specified arguments to the screen.
Parameters: *args – Variable length argument list.
-
static
println
(*args)[source]¶ Print the specified arguments to the screen, including an appended newline character.
Parameters: *args – Variable length argument list.
-
screen_height
¶ The screen height in rows.
Type: int
-
screen_width
¶ The screen width in columns.
Type: int
-
Menu Components¶
Dimension¶
-
class
consolemenu.menu_component.
Dimension
(width=0, height=0, dimension=None)[source]¶ The Dimension class encapsulates the height and width of a component.
Parameters: - width (int) – the width of the Dimension, in columns.
- height (int) – the height of the Dimension, in rows.
- dimension (Dimension, optional) – an existing Dimension from which to duplicate the height and width.
MenuComponent¶
-
class
consolemenu.menu_component.
MenuComponent
(menu_style, max_dimension=None)[source]¶ Base class for a menu component.
Parameters: - menu_style (
MenuStyle
) – the style for this component. - max_dimension (
Dimension
) – the maximum Dimension (width x height) for the menu. Defaults to width=80 and height=40 if not specified.
Raises: TypeError – if menu_style is not a
MenuStyle
.-
border_style
¶ The border style for this component.
Type: consolemenu.format.MenuBorderStyle
-
calculate_border_width
()[source]¶ Calculate the width of the menu border. This will be the width of the maximum allowable dimensions (usually the screen size), minus the left and right margins and the newline character. For example, given a maximum width of 80 characters, with left and right margins both set to 1, the border width would be 77 (80 - 1 - 1 - 1 = 77).
Returns: the menu border width in columns. Return type: int
-
calculate_content_width
()[source]¶ Calculate the width of inner content of the border. This will be the width of the menu borders, minus the left and right padding, and minus the two vertical border characters. For example, given a border width of 77, with left and right margins each set to 2, the content width would be 71 (77 - 2 - 2 - 2 = 71).
Returns: the inner content width in columns. Return type: int
-
generate
()[source]¶ Generate this component.
Yields: str – The next string of characters for drawing this component.
-
inner_horizontal_border
()[source]¶ The complete inner horizontal border section, including the left and right border verticals.
Returns: The complete inner horizontal border. Return type: str
-
inner_horizontals
()[source]¶ The string of inner horizontal border characters of the required length for this component (not including the menu margins or verticals).
Returns: The inner horizontal characters. Return type: str
-
margins
¶ The margins for this component.
Type: consolemenu.format.MenuMargins
-
outer_horizontal_border_bottom
()[source]¶ The complete outer bottom horizontal border section, including left and right margins.
Returns: The bottom menu border. Return type: str
-
outer_horizontal_border_top
()[source]¶ The complete outer top horizontal border section, including left and right margins.
Returns: The top menu border. Return type: str
-
outer_horizontals
()[source]¶ The string of outer horizontal border characters of the required length for this component (not including the menu margins or verticals).
Returns: The outer horizontal characters. Return type: str
-
padding
¶ The padding for this component.
Type: consolemenu.format.MenuPadding
-
row
(content='', align='left', indent_len=0)[source]¶ A row of the menu, which comprises the left and right verticals plus the given content. If the content is larger than the alloted space for a single row, the content is wrapped onto multiple lines, while also respecting user-included newline characters.
Returns: One or more rows of this menu component with the specified content. Return type: str
-
style
¶ The style for this component.
Type: consolemenu.format.MenuStyle
- menu_style (
Formatting¶
MenuBorderStyle¶
-
class
consolemenu.format.
MenuBorderStyle
[source]¶ Base class for console menu border. Each property should be overridden by a subclass.
-
bottom_left_corner
¶ The outer, bottom left corner of the menu.
-
bottom_right_corner
¶ The outer, bottom right corner of the menu.
-
inner_horizontal
¶ The character for inner horizontal section lines.
-
inner_vertical
¶ The character for inner vertical section lines.
-
intersection
¶ The character for intersecting inner vertical and inner horizontal lines (a “+” shape).
-
outer_horizontal
¶ The character for outer, horizontal lines (the top and bottom lines of the menu).
-
outer_horizontal_inner_down
¶ The character for a top horizontal line with a downward inner line (a “T” shape).
-
outer_horizontal_inner_up
¶ The character for a bottom horizontal line with an upward inner line (an inverted “T” shape).
-
outer_vertical
¶ The character for an outer vertical line of the menu (the left and right sides of the menu).
-
outer_vertical_inner_left
¶ The character for an outer vertical line, with a protruding inner line to the left.
-
outer_vertical_inner_right
¶ The character for an outer vertical line, with a protruding inner line to the right.
-
top_left_corner
¶ The top left corner of the menu.
-
top_right_corner
¶ The top right corner of the menu.
-
MenuBorderStyleType¶
-
class
consolemenu.format.
MenuBorderStyleType
[source]¶ Defines the various menu border styles, as expected by the border factory.
-
ASCII_BORDER
= 0¶ Menu Border using pure ASCII characters. Usable on all platforms.
Type: int
-
LIGHT_BORDER
= 1¶ Menu Border using the “light” box drawing characters. Should be usable on all platforms.
Type: int
-
HEAVY_BORDER
= 2¶ Menu Border using the “heavy” box drawing characters. NOTE: On Windows, this border style will work ONLY on Python 3.6 and later. It will raise a UnicodeEncodeError exception on earlier Python versions. If requesting this border style via the MenuBorderStyleFactory when on Windows/Python 3.5 or earlier, this border style will be substituted by the DOUBLE_LINE_BORDER.
Type: int
-
DOUBLE_LINE_BORDER
= 3¶ Menu Border using “double-line” box drawing characters.
Type: int
-
HEAVY_OUTER_LIGHT_INNER_BORDER
= 4¶ Menu Border using the “heavy” box drawing characters for the outer border elements, and “light” box-drawing characters for the inner border elements. NOTE: On Windows, this border style will work ONLY on Python 3.6 and later. It will raise a UnicodeEncodeError exception on earlier Python versions. If requesting this border style via the MenuBorderStyleFactory when on Windows/Python 3.5 or earlier, this border style will be substituted by the DOUBLE_LINE_BORDER.
Type: int
-
DOUBLE_LINE_OUTER_LIGHT_INNER_BORDER
= 5¶ Menu Border using the “double-line” box drawing characters for the outer border elements, and “light” box-drawing characters for the inner border elements.
Type: int
-
MenuBorderStyleFactory¶
-
class
consolemenu.format.
MenuBorderStyleFactory
[source]¶ Factory class for creating MenuBorderStyle instances.
-
create_ascii_border
()[source]¶ Create an ASCII border style.
Returns: a new instance of AsciiBorderStyle. Return type: AsciiBorderStyle
-
create_border
(border_style_type)[source]¶ Create a new MenuBorderStyle instance based on the given border style type.
Parameters: border_style_type (int) – an integer value from MenuBorderStyleType
.Returns: a new MenuBorderStyle instance of the specified style. Return type: MenuBorderStyle
-
create_doubleline_border
()[source]¶ Create a border style using “double-line” box drawing characters.
Returns: a new instance of DoubleLineBorderStyle. Return type: DoubleLineBorderStyle
-
create_doubleline_outer_light_inner_border
()[source]¶ Create a border style using “double-line” box drawing characters for outer border elements, and “light” box drawing characters for inner border elements.
Returns: a new instance of DoubleLineOuterLightInnerBorderStyle Return type: DoubleLineOuterLightInnerBorderStyle
-
create_heavy_border
()[source]¶ Create a border style using “heavy” box drawing characters.
NOTE: The Heavy border style will work on Windows ONLY when using Python 3.6 or later. If on Windows and using an earlier version of Python, the heavy border will be substituted with the DOUBLE_LINE_BORDER.
Returns: a new instance of HeavyBorderStyle, unless on Windows and pre-Python 3.5, in which case a new instance of DoubleLineBorderStyle will be returned. Return type: HeavyBorderStyle
orDoubleLineBorderStyle
-
create_heavy_outer_light_inner_border
()[source]¶ Create a border style using “heavy” box drawing characters for outer border elements, and “light” box drawing characters for inner border elements.
NOTE: The Heavy border style will work on Windows ONLY when using Python 3.6 or later. If on Windows and using an earlier version of Python, the heavy border will be substituted with the DOUBLE_LINE_BORDER.
Returns: a new instance of HeavyOuterLightInnerBorderStyle, unless on Windows and pre-Python 3.5, in which case a new instance of DoubleLineOuterLightInnerBorderStyle will be returned. Return type: HeavyOuterLightInnerBorderStyle
orDoubleLineOuterLightInnerBorderStyle
-
Menu Borders¶
AsciiBorderStyle¶
-
class
consolemenu.format.
AsciiBorderStyle
[source] A Menu Border Style using only ASCII characters.
DoubleLineBorderStyle¶
-
class
consolemenu.format.
DoubleLineBorderStyle
[source] MenuBorderStyle class using “double-line” box drawing characters.
DoubleLineOuterLightInnerBorderStyle¶
-
class
consolemenu.format.
DoubleLineOuterLightInnerBorderStyle
[source] MenuBorderStyle class using Unicode “double-line” box drawing characters for the outer borders, and “light” box drawing characters for the inner borders.
HeavyBorderStyle¶
-
class
consolemenu.format.
HeavyBorderStyle
[source] MenuBorderStyle class using Unicode “heavy” box drawing characters.
MenuMargins¶
-
class
consolemenu.format.
MenuMargins
(top=1, left=2, bottom=0, right=2)[source]¶ Class for menu margins. A margin is the area between the maximum specified dimensions (which is usually the width and height of the screen) and the menu border.
Parameters: - top (int) – The top margin.
- left (int) – The left margin.
- bottom (int) – The bottom margin.
- right (int) – The right margin.
-
bottom
¶ The bottom margin.
Returns: The bottom margin. Return type: int
-
left
¶ The left margin.
Returns: The left margin. Return type: int
-
right
¶ The right margin.
Returns: The right margin. Return type: int
-
top
¶ The top margin.
Returns: The top margin. Return type: int
MenuPadding¶
-
class
consolemenu.format.
MenuPadding
(top=1, left=2, bottom=1, right=2)[source]¶ Class for menu padding. Padding is the area between the menu border and the content of the menu.
Parameters: - top (int) – The top padding.
- left (int) – The left padding.
- bottom (int) – The bottom padding.
- right (int) – The right padding.
-
bottom
¶ The bottom padding.
Returns: The bottom padding. Return type: int
-
left
¶ The left padding.
Returns: The left padding. Return type: int
-
right
¶ The right padding.
Returns: The right padding. Return type: int
-
top
¶ The top padding.
Returns: The top padding. Return type: int
MenuStyle¶
-
class
consolemenu.format.
MenuStyle
(margins=None, padding=None, border_style=None, border_style_type=None, border_style_factory=None)[source]¶ Class for specifying all menu styling, such as margins, padding, and border style.
Parameters: - margins (
MenuMargins
) – The menu margin settings. - padding (
MenuPadding
) – The menu padding. - ( (border_style) – obj:MenuBorderStyle`): The menu border style. Takes precedence over border_style_type if both are specified.
- border_style_type (int) – The border style type as defined by
MenuBorderStyleType
. - border_style_factory (
MenuBorderStyleFactory
) – The factory instance to use to create the borders.
-
border_style
¶ The border style instance.
Returns: The MenuBorderStyle instance. Return type: MenuBorderStyle
-
border_style_factory
¶ The border style factory instance.
Returns: The MenuBorderStyleFactory instance. Return type: MenuBorderStyleFactory
-
margins
¶ The margins instance.
Returns: The MenuMargins instance. Return type: MenuMargins
-
padding
¶ The padding instance.
Returns: The MenuPadding instance. Return type: MenuPadding
- margins (
Prompts¶
PromptFormatter¶
-
class
consolemenu.prompt_utils.
PromptFormatter
[source]¶ Class for formatting a text input prompt, to allow overriding the message as desired.
Default answers will appear in [square brackets] and allow the user to return that answer by simply pressing the Enter button.
If a ‘Quit’ option is desired, set enable_quit to True and provide a quit_string (default is ‘q’) and a quit_message (default is ‘(enter q to Quit)’).
-
static
format_prompt
(prompt=None, default=None, enable_quit=False, quit_string='q', quit_message='(enter q to Quit)')[source]¶ Format the message presented to the user during input prompting.
Parameters: - prompt (str) – The message to ask the user.
- default (str, optional) – The default answer if user does not provide explicit input.
- enable_quit (bool, optional) – Flag to determine whether a Quit option will be presented.
- quit_string (str, optional) – The string the user must input to quit (default is ‘q’).
- quit_message (str, optional) – The message to the user explaining how to Quit.
Returns: The formatted prompt string.
Return type: str
-
static
PromptUtils¶
-
class
consolemenu.prompt_utils.
PromptUtils
(screen, prompt_formatter=None)[source]¶ Utility class with various routines for prompting for user input.
Creates a new instance of ConsoleUtils with the specified console. If no console was specified, creates a new default console using the ConsoleFactory.
Parameters: - screen (
consolemenu.screen.Screen
) – The Screen instance. - prompt_formatter (
PromptFormatter
, optional) – The instance of PromptFormatter for displaying the prompt.
-
confirm_answer
(answer, message=None)[source]¶ Prompts the user to confirm a question with a yes/no prompt. If no message is specified, the default message is: “You entered {}. Is this correct?”
Parameters: - answer (str) – The answer to confirm.
- message (str, optional) – Optional message if a different confirmation prompt is desired.
Returns: True if the user confirmed Yes, or False if user specified No.
Return type: bool
-
enter_to_continue
(message=None)[source]¶ A console prompt to ask the user to ‘Press [Enter] to continue’.
Parameters: message (str, optional) – A message to display in place of the default.
-
input
(prompt=None, default=None, validators=None, enable_quit=False, quit_string='q', quit_message='(enter q to Quit)')[source]¶ Generic prompt the user for input.
Parameters: - prompt (str) – The message to prompt the user.
- default (str, optional) – The default value to suggest as an answer.
- validators (
BaseValidator
, optional) – The list of validators to perform input validation. - enable_quit (bool, optional) – Specifies whether the user can cancel out of the input prompt.
- quit_string (str, optional) – The string which the user must input in order to quit.
- quit_message (str, optional) – The message to explain how to quit.
Returns: an InputResult tuple.
Return type: InputResult
-
input_password
(message=None)[source]¶ Prompt the user for a password or other confidential data.
This is equivalent to the input() method, but does not echo inputted characters to the screen.
Parameters: message (str) – The prompt message. Returns: The password provided by the user. Return type: str
-
printf
(*args)[source]¶ Prints the specified arguments to the screen.
Parameters: *args – Variable length argument list.
-
println
(*args)[source]¶ Prints the specified arguments to the screen, followed by a newline character.
Parameters: *args – Variable length argument list.
-
prompt_and_confirm_password
(message)[source]¶ Prompt for a password using the given message, then prompt a second time for a confirmation password, and verify both provided passwords match. If the passwords do not match, an error is displayed, “Passwords do not match”, and the user must input both passwords again.
Parameters: message (str) – The prompt message. Returns: The password. Return type: str
-
prompt_for_bilateral_choice
(prompt, option1, option2)[source]¶ Prompt the user for a response that must be one of the two supplied choices.
NOTE: The user input verification is case-insensitive, but will return the original case provided by the given options.
Parameters: - prompt (str) – The prompt to present the choices to the user.
- option1 (str) – The first option.
- option2 (str) – The second option.
Returns: The choice selected by the user.
Return type: str
-
prompt_for_numbered_choice
(choices, title=None, prompt='>')[source]¶ Displays a numbered vertical list of choices from the provided list of strings.
Parameters: - choices (
list
ofstr
) – The list of choices to display. - title (str, optional) – Optional title to display above the numbered list.
- prompt (str) – The prompt string. Default is “>”.
Returns: The index of selected choice.
Return type: int
- choices (
-
prompt_for_trilateral_choice
(prompt, option1, option2, option3)[source]¶ Prompt the user for a response that must be one of the three supplied choices.
NOTE: The user input verification is case-insensitive, but will return the original case provided by the given options.
Parameters: - prompt (str) – The prompt to present the choices to the user.
- option1 (str) – The first option.
- option2 (str) – The second option.
- option3 (str) – The third option.
Returns: The choice selected by the user.
Return type: str
-
prompt_for_yes_or_no
(prompt)[source]¶ Prompts the user with the specified question, and expects a yes (y) or no (n) response, returning a boolean value representing the user’s answer.
Parameters: prompt (str) – The prompt to display to the user. Returns: True for yes, False for no. Return type: bool
-
screen
¶ The Screen instance.
Type: consolemenu.screen.Screen
-
validate_input
(input_string, validators)[source]¶ Validate the given input string against the specified list of validators.
Parameters: - input_string (str) – The input string to verify.
- validators (
list
ofBaseValidator
) – The list of validators.
Returns: The validation result. True if the input is valid; False otherwise.
Return type: bool
Raises: InvalidValidator – If the list of validators contains an invalid BaseValidator class.
- screen (