Mercurial > python-cmd2
comparison cmd2.py @ 245:2cda5a817e5a
send to ioug
author | catherine@Elli.myhome.westell.com |
---|---|
date | Wed, 25 Mar 2009 19:15:43 -0400 |
parents | e0c60ea7ad5d |
children | 2e37155d2b95 |
comparison
equal
deleted
inserted
replaced
244:e0c60ea7ad5d | 245:2cda5a817e5a |
---|---|
24 CHANGES: | 24 CHANGES: |
25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | 25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. |
26 flagReader.py options are still supported for backward compatibility | 26 flagReader.py options are still supported for backward compatibility |
27 """ | 27 """ |
28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | 28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
29 import unittest, string, datetime, urllib, inspect | 29 import unittest, string, datetime, urllib, inspect, itertools |
30 from code import InteractiveConsole, InteractiveInterpreter, softspace | 30 from code import InteractiveConsole, InteractiveInterpreter, softspace |
31 from optparse import make_option | 31 from optparse import make_option |
32 __version__ = '0.5.0' | 32 __version__ = '0.5.0' |
33 | 33 |
34 class OptionParser(optparse.OptionParser): | 34 class OptionParser(optparse.OptionParser): |
263 defaultExtension = 'txt' | 263 defaultExtension = 'txt' |
264 default_file_name = 'command.txt' | 264 default_file_name = 'command.txt' |
265 abbrev = True | 265 abbrev = True |
266 nonpythoncommand = 'cmd' | 266 nonpythoncommand = 'cmd' |
267 current_script_dir = None | 267 current_script_dir = None |
268 reserved_words = [] | |
268 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', | 269 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', |
269 'case_insensitive', 'echo', 'timing', 'abbrev'] | 270 'case_insensitive', 'echo', 'timing', 'abbrev'] |
270 settable.sort() | 271 settable.sort() |
271 | 272 |
272 editor = os.environ.get('EDITOR') | 273 editor = os.environ.get('EDITOR') |
273 _STOP_AND_EXIT = 2 | 274 _STOP_AND_EXIT = 2 |
274 if not editor: | 275 if not editor: |
304 cmd.Cmd.__init__(self, *args, **kwargs) | 305 cmd.Cmd.__init__(self, *args, **kwargs) |
305 self.history = History() | 306 self.history = History() |
306 self._init_parser() | 307 self._init_parser() |
307 self.pystate = {} | 308 self.pystate = {} |
308 self.shortcuts = sorted(self.shortcuts.items(), reverse=True) | 309 self.shortcuts = sorted(self.shortcuts.items(), reverse=True) |
310 self.keywords = list(itertools.izip(self.reserved_words, itertools.repeat(None))) + \ | |
311 [(fname[3:], func) for (fname, func) in | |
312 inspect.getmembers(self, inspect.ismethod) | |
313 if fname.startswith('do_')] | |
309 | 314 |
310 def do_shortcuts(self, args): | 315 def do_shortcuts(self, args): |
311 """Lists single-key shortcuts available.""" | 316 """Lists single-key shortcuts available.""" |
312 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in sorted(self.shortcuts)) | 317 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in sorted(self.shortcuts)) |
313 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) | 318 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) |
623 try: | 628 try: |
624 func = getattr(self, 'do_' + statement.parsed.command) | 629 func = getattr(self, 'do_' + statement.parsed.command) |
625 except AttributeError: | 630 except AttributeError: |
626 func = None | 631 func = None |
627 if self.abbrev: # accept shortened versions of commands | 632 if self.abbrev: # accept shortened versions of commands |
628 funcs = [(fname, function) for (fname, function) in inspect.getmembers(self, inspect.ismethod) | 633 funcs = [(fname, function) for (fname, function) in self.keywords |
629 if fname.startswith('do_' + statement.parsed.command)] | 634 if fname.startswith(statement.parsed.command)] |
630 if len(funcs) == 1: | 635 if len(funcs) == 1: |
631 func = funcs[0][1] | 636 func = funcs[0][1] |
632 if not func: | 637 if not func: |
633 return self.postparsing_postcmd(self.default(statement)) | 638 return self.postparsing_postcmd(self.default(statement)) |
634 timestart = datetime.datetime.now() | 639 timestart = datetime.datetime.now() |
814 keepstate = Statekeeper(sys, ('stdin','stdout')) | 819 keepstate = Statekeeper(sys, ('stdin','stdout')) |
815 sys.stdout = self.stdout | 820 sys.stdout = self.stdout |
816 sys.stdin = self.stdin | 821 sys.stdin = self.stdin |
817 interp.interact(banner= "Python %s on %s\n%s\n(%s)\n%s" % | 822 interp.interact(banner= "Python %s on %s\n%s\n(%s)\n%s" % |
818 (sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__)) | 823 (sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__)) |
819 keepstate.restore() | |
820 except EmbeddedConsoleExit: | 824 except EmbeddedConsoleExit: |
821 return | 825 pass |
826 keepstate.restore() | |
822 | 827 |
823 def do_history(self, arg): | 828 def do_history(self, arg): |
824 """history [arg]: lists past commands issued | 829 """history [arg]: lists past commands issued |
825 | 830 |
826 no arg -> list all | 831 no arg -> list all |