comparison cmd2.py @ 199:12c2d23caf19

added unit check for doublepipe
author catherine@dellzilla
date Thu, 26 Feb 2009 12:50:22 -0500
parents 770c77152051
children 05c0e28306d3
comparison
equal deleted inserted replaced
198:b90021703faf 199:12c2d23caf19
168 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 168 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
169 noSpecialParse = 'set ed edit exit'.split() 169 noSpecialParse = 'set ed edit exit'.split()
170 defaultExtension = 'txt' 170 defaultExtension = 'txt'
171 defaultFileName = 'command.txt' 171 defaultFileName = 'command.txt'
172 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive', 'echo', 'timing'] 172 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive', 'echo', 'timing']
173 settable.sort()
173 174
174 editor = os.environ.get('EDITOR') 175 editor = os.environ.get('EDITOR')
175 _STOP_AND_EXIT = 2 176 _STOP_AND_EXIT = 2
176 if not editor: 177 if not editor:
177 if sys.platform[:3] == 'win': 178 if sys.platform[:3] == 'win':
275 ['simple', '', '|', ' piped'] 276 ['simple', '', '|', ' piped']
276 - command: simple 277 - command: simple
277 - pipeTo: piped 278 - pipeTo: piped
278 - statement: ['simple', ''] 279 - statement: ['simple', '']
279 - command: simple 280 - command: simple
281 >>> print c.parser.parseString('double-pipe || is not a pipe').dump()
282 ['double', '-pipe || is not a pipe']
283 - args: -pipe || is not a pipe
284 - command: double
285 - statement: ['double', '-pipe || is not a pipe']
286 - args: -pipe || is not a pipe
287 - command: double
280 >>> print c.parser.parseString('command with args, terminator;sufx | piped').dump() 288 >>> print c.parser.parseString('command with args, terminator;sufx | piped').dump()
281 ['command', 'with args, terminator', ';', 'sufx', '|', ' piped'] 289 ['command', 'with args, terminator', ';', 'sufx', '|', ' piped']
282 - args: with args, terminator 290 - args: with args, terminator
283 - command: command 291 - command: command
284 - pipeTo: piped 292 - pipeTo: piped
586 return True 594 return True
587 do_eof = do_EOF 595 do_eof = do_EOF
588 596
589 def showParam(self, param): 597 def showParam(self, param):
590 param = param.strip().lower() 598 param = param.strip().lower()
591 if param in self.settable: 599 for p in self.settable:
592 val = getattr(self, param) 600 if p.startswith(param):
593 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param)))) 601 val = getattr(self, p)
594 602 self.stdout.write('%s: %s\n' % (p, str(getattr(self, p))))
603
595 def do_quit(self, arg): 604 def do_quit(self, arg):
596 return self._STOP_AND_EXIT 605 return self._STOP_AND_EXIT
597 do_exit = do_quit 606 do_exit = do_quit
598 do_q = do_quit 607 do_q = do_quit
599 608
604 else: 613 else:
605 for param in self.settable: 614 for param in self.settable:
606 self.showParam(param) 615 self.showParam(param)
607 616
608 def do_set(self, arg): 617 def do_set(self, arg):
609 'Sets a parameter' 618 '''Sets a cmd2 parameter. Accepts abbreviated parameter names so long as there is no ambiguity.
619 Call without arguments for a list of settable parameters with their values.'''
610 try: 620 try:
611 paramName, val = arg.split(None, 1) 621 paramName, val = arg.split(None, 1)
612 paramName = paramName.strip().lower() 622 paramName = paramName.strip().lower()
613 if paramName not in self.settable: 623 hits = [paramName in p for p in self.settable]
614 raise NotSettableError 624 if hits.count(True) == 1:
615 currentVal = getattr(self, paramName) 625 paramName = self.settable[hits.index(True)]
616 if (val[0] == val[-1]) and val[0] in ("'", '"'): 626 currentVal = getattr(self, paramName)
617 val = val[1:-1] 627 if (val[0] == val[-1]) and val[0] in ("'", '"'):
618 else: 628 val = val[1:-1]
619 val = cast(currentVal, val) 629 else:
620 setattr(self, paramName, val) 630 val = cast(currentVal, val)
621 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val)) 631 setattr(self, paramName, val)
632 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val))
633 else:
634 self.do_show(paramName)
622 except (ValueError, AttributeError, NotSettableError), e: 635 except (ValueError, AttributeError, NotSettableError), e:
623 self.do_show(arg) 636 self.do_show(arg)
624 do_set.__doc__ = '%s\nOne of: %s' % (do_set.__doc__, ', '.join(settable))
625 637
626 def do_pause(self, arg): 638 def do_pause(self, arg):
627 'Displays the specified text then waits for the user to press RETURN.' 639 'Displays the specified text then waits for the user to press RETURN.'
628 raw_input(arg + '\n') 640 raw_input(arg + '\n')
629 641