comparison cmd2.py @ 43:e03a8c03ac37

command-line args working
author catherine@cordelia
date Sun, 25 May 2008 05:22:27 -0400
parents b3200c6e5763
children b66372883bb4
comparison
equal deleted inserted replaced
42:b3200c6e5763 43:e03a8c03ac37
123 else: 123 else:
124 def getPasteBuffer(): 124 def getPasteBuffer():
125 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') 125 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"')
126 setPasteBuffer = getPasteBuffer 126 setPasteBuffer = getPasteBuffer
127 127
128 class ExitException(Exception):
129 pass
130
131 class Cmd(cmd.Cmd): 128 class Cmd(cmd.Cmd):
132 caseInsensitive = True 129 caseInsensitive = True
133 multilineCommands = [] 130 multilineCommands = []
134 continuationPrompt = '> ' 131 continuationPrompt = '> '
135 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'} 132 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
338 try: 335 try:
339 import readline 336 import readline
340 readline.set_completer(self.old_completer) 337 readline.set_completer(self.old_completer)
341 except ImportError: 338 except ImportError:
342 pass 339 pass
343 return stop 340 return stop
344 341
345 def do_EOF(self, arg): 342 def do_EOF(self, arg):
346 return True 343 return True
347 do_eof = do_EOF 344 do_eof = do_EOF
348 345
373 if param in self.settable: 370 if param in self.settable:
374 val = getattr(self, param) 371 val = getattr(self, param)
375 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param)))) 372 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param))))
376 373
377 def do_quit(self, arg): 374 def do_quit(self, arg):
378 raise ExitException 375 return self._STOP_AND_EXIT
379 do_exit = do_quit 376 do_exit = do_quit
380 do_q = do_quit 377 do_q = do_quit
381 378
382 def do_show(self, arg): 379 def do_show(self, arg):
383 'Shows value of a parameter' 380 'Shows value of a parameter'
505 keepstate.restore() 502 keepstate.restore()
506 return 503 return
507 self.use_rawinput = False 504 self.use_rawinput = False
508 self.prompt = self.continuationPrompt = '' 505 self.prompt = self.continuationPrompt = ''
509 stop = self.cmdloop() 506 stop = self.cmdloop()
510 # but how to detect whether to exit totally?
511 self.stdin.close() 507 self.stdin.close()
512 keepstate.restore() 508 keepstate.restore()
513 self.lastcmd = '' 509 self.lastcmd = ''
514 return (stop == self._STOP_AND_EXIT) 510 return (stop == self._STOP_AND_EXIT) and self._STOP_AND_EXIT
515 511
516 def do_run(self, arg): 512 def do_run(self, arg):
517 """run [arg]: re-runs an earlier command 513 """run [arg]: re-runs an earlier command
518 514
519 no arg -> run most recent command 515 no arg -> run most recent command
520 arg is integer -> run one history item, by index 516 arg is integer -> run one history item, by index