comparison cmd2.py @ 309:1941e54cb776

added bash-style SELECT
author catherine@dellzilla
date Fri, 29 Jan 2010 10:32:40 -0500
parents 4e9011c3f732
children 9d91406ca3a7
comparison
equal deleted inserted replaced
308:4e9011c3f732 309:1941e54cb776
36 import glob 36 import glob
37 import traceback 37 import traceback
38 from code import InteractiveConsole, InteractiveInterpreter, softspace 38 from code import InteractiveConsole, InteractiveInterpreter, softspace
39 from optparse import make_option 39 from optparse import make_option
40 40
41 import pyparsing 41 if sys.version_info[0] > 2:
42 import pyparsing_py3 as pyparsing
43 else:
44 import pyparsing
45
42 __version__ = '0.5.6' 46 __version__ = '0.5.6'
43 47
44 class OptionParser(optparse.OptionParser): 48 class OptionParser(optparse.OptionParser):
45 def exit(self, status=0, msg=None): 49 def exit(self, status=0, msg=None):
46 self.values._exit = True 50 self.values._exit = True
828 def do_quit(self, arg): 832 def do_quit(self, arg):
829 return self._STOP_AND_EXIT 833 return self._STOP_AND_EXIT
830 do_exit = do_quit 834 do_exit = do_quit
831 do_q = do_quit 835 do_q = do_quit
832 836
837 def select(self, options, prompt='Your choice? '):
838 '''Presents a numbered menu to the user. Modelled after
839 the bash shell's SELECT. Returns the item chosen.'''
840 if isinstance(options, basestring):
841 options = options.split()
842 for (idx, opt) in enumerate(options):
843 self.poutput(' %2d. %s\n' % (idx+1, opt))
844 while True:
845 response = raw_input(prompt)
846 try:
847 response = int(response)
848 result = options[response - 1]
849 break
850 except ValueError:
851 pass # loop and ask again
852 return result
853
833 @options([make_option('-l', '--long', action="store_true", 854 @options([make_option('-l', '--long', action="store_true",
834 help="describe function of parameter")]) 855 help="describe function of parameter")])
835 def do_show(self, arg, opts): 856 def do_show(self, arg, opts):
836 '''Shows value of a parameter.''' 857 '''Shows value of a parameter.'''
837 param = arg.strip().lower() 858 param = arg.strip().lower()