Mercurial > python-cmd2
comparison cmd2.py @ 310:9d91406ca3a7
select polished
author | catherine@dellzilla |
---|---|
date | Fri, 29 Jan 2010 11:26:23 -0500 |
parents | 1941e54cb776 |
children | 54e2dd53ba38 |
comparison
equal
deleted
inserted
replaced
309:1941e54cb776 | 310:9d91406ca3a7 |
---|---|
11 Settable environment parameters | 11 Settable environment parameters |
12 Optional _onchange_{paramname} called when environment parameter changes | 12 Optional _onchange_{paramname} called when environment parameter changes |
13 Parsing commands with `optparse` options (flags) | 13 Parsing commands with `optparse` options (flags) |
14 Redirection to file with >, >>; input from file with < | 14 Redirection to file with >, >>; input from file with < |
15 Easy transcript-based testing of applications (see example/example.py) | 15 Easy transcript-based testing of applications (see example/example.py) |
16 Bash-style ``select`` available | |
16 | 17 |
17 Note that redirection with > and | will only work if `self.stdout.write()` | 18 Note that redirection with > and | will only work if `self.stdout.write()` |
18 is used in place of `print`. The standard library's `cmd` module is | 19 is used in place of `print`. The standard library's `cmd` module is |
19 written to use `self.stdout.write()`, | 20 written to use `self.stdout.write()`, |
20 | 21 |
834 do_exit = do_quit | 835 do_exit = do_quit |
835 do_q = do_quit | 836 do_q = do_quit |
836 | 837 |
837 def select(self, options, prompt='Your choice? '): | 838 def select(self, options, prompt='Your choice? '): |
838 '''Presents a numbered menu to the user. Modelled after | 839 '''Presents a numbered menu to the user. Modelled after |
839 the bash shell's SELECT. Returns the item chosen.''' | 840 the bash shell's SELECT. Returns the item chosen. |
841 | |
842 Argument ``options`` can be: | |
843 a single string -> will be split into one-word options | |
844 a list of strings -> will be offered as options | |
845 a list of tuples -> interpreted as (value, text), so | |
846 that the return value can differ from | |
847 the text advertised to the user ''' | |
840 if isinstance(options, basestring): | 848 if isinstance(options, basestring): |
841 options = options.split() | 849 options = zip(options.split(), options.split()) |
842 for (idx, opt) in enumerate(options): | 850 fulloptions = [] |
843 self.poutput(' %2d. %s\n' % (idx+1, opt)) | 851 for opt in options: |
852 if isinstance(opt, basestring): | |
853 fulloptions.append((opt, opt)) | |
854 else: | |
855 try: | |
856 fulloptions.append((opt[0], opt[1])) | |
857 except IndexError: | |
858 fulloptions.append((opt[0], opt[0])) | |
859 for (idx, (value, text)) in enumerate(fulloptions): | |
860 self.poutput(' %2d. %s\n' % (idx+1, text)) | |
844 while True: | 861 while True: |
845 response = raw_input(prompt) | 862 response = raw_input(prompt) |
846 try: | 863 try: |
847 response = int(response) | 864 response = int(response) |
848 result = options[response - 1] | 865 result = fulloptions[response - 1][0] |
849 break | 866 break |
850 except ValueError: | 867 except ValueError: |
851 pass # loop and ask again | 868 pass # loop and ask again |
852 return result | 869 return result |
853 | 870 |