It can be necessary to create a simple interactive menu on CLI (Command-Line Interface) using Python to allow users to make some choices while executing a script/program. As there is no standard library for this in Python, you will have to adapt it by yourself.
Here is a very simple example of how to do this. Feel free to modify, update and improve it as depending on your needs and your expectations 😉 !
#!/usr/bin/env python # -*- coding: utf-8 -*- #title :menu.py #description :This program displays an interactive menu on CLI #author : #date : #version :0.1 #usage :python menu.py #notes : #python_version :2.7.6 #======================================================================= # Import the modules needed to run the script. import sys, os # Main definition - constants menu_actions = {} # ======================= # MENUS FUNCTIONS # ======================= # Main menu def main_menu(): os.system('clear') print "Welcome,\n" print "Please choose the menu you want to start:" print "1. Menu 1" print "2. Menu 2" print "\n0. Quit" choice = raw_input(" >> ") exec_menu(choice) return # Execute menu def exec_menu(choice): os.system('clear') ch = choice.lower() if ch == '': menu_actions['main_menu']() else: try: menu_actions[ch]() except KeyError: print "Invalid selection, please try again.\n" menu_actions['main_menu']() return # Menu 1 def menu1(): print "Hello Menu 1 !\n" print "9. Back" print "0. Quit" choice = raw_input(" >> ") exec_menu(choice) return # Menu 2 def menu2(): print "Hello Menu 2 !\n" print "9. Back" print "0. Quit" choice = raw_input(" >> ") exec_menu(choice) return # Back to main menu def back(): menu_actions['main_menu']() # Exit program def exit(): sys.exit() # ======================= # MENUS DEFINITIONS # ======================= # Menu definition menu_actions = { 'main_menu': main_menu, '1': menu1, '2': menu2, '9': back, '0': exit, } # ======================= # MAIN PROGRAM # ======================= # Main Program if __name__ == "__main__": # Launch main menu main_menu()