Mercurial > sqlpython
comparison cmd2.py @ 17:25e908abf199
multiline into cmd2
author | devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil |
---|---|
date | Wed, 19 Dec 2007 11:11:24 -0500 |
parents | 2776755a3a7e |
children | d0f0b3e0b89b |
comparison
equal
deleted
inserted
replaced
16:2776755a3a7e | 17:25e908abf199 |
---|---|
40 def isin(hi): | 40 def isin(hi): |
41 return (getme.lower() in hi.lowercase) | 41 return (getme.lower() in hi.lowercase) |
42 return [itm for itm in self if isin(itm)] | 42 return [itm for itm in self if isin(itm)] |
43 | 43 |
44 class Cmd(cmd.Cmd): | 44 class Cmd(cmd.Cmd): |
45 def __init__(self, *args, **kwargs): | 45 excludeFromHistory = '''run r list l history hi ed li'''.split() |
46 caseInsensitive = True | |
47 multilineCommands = [] | |
48 continuationPrompt = '> ' | |
49 def __init__(self, *args, **kwargs): | |
46 cmd.Cmd.__init__(self, *args, **kwargs) | 50 cmd.Cmd.__init__(self, *args, **kwargs) |
47 self.history = History() | 51 self.history = History() |
48 self.excludeFromHistory = '''run r list l history hi ed li'''.split() | 52 |
53 def precmd(self, line): | |
54 """Hook method executed just before the command line is | |
55 interpreted, but after the input prompt is generated and issued. | |
56 | |
57 Makes commands case-insensitive (but unfortunately does not alter command completion). | |
58 """ | |
59 try: | |
60 (command, args) = line.split(None,1) | |
61 except ValueError: | |
62 (command, args) = line, '' | |
63 if self.caseInsensitive: | |
64 command = command.lower() | |
65 statement = ' '.join([command, args]) | |
66 if (not self.multilineCommands) or (command not in self.multilineCommands): | |
67 return statement | |
68 return self.finishStatement(statement) | |
49 | 69 |
50 def postcmd(self, stop, line): | 70 def postcmd(self, stop, line): |
51 """Hook method executed just after a command dispatch is finished.""" | 71 """Hook method executed just after a command dispatch is finished.""" |
52 try: | 72 try: |
53 command = line.split(None,1)[0].lower() | 73 command = line.split(None,1)[0].lower() |
54 if command not in self.excludeFromHistory: | 74 if command not in self.excludeFromHistory: |
55 self.history.append(line) | 75 self.history.append(line) |
56 finally: | 76 finally: |
57 return stop | 77 return stop |
78 | |
79 def finishStatement(self, firstline): | |
80 statement = firstline | |
81 while not self.statementHasEnded(statement): | |
82 statement = '%s\n%s' % (statement, raw_input(self.continuationPrompt)) | |
83 return statement | |
84 # assembling a list of lines and joining them at the end would be faster, | |
85 # but statementHasEnded needs a string arg; anyway, we're getting | |
86 # user input and users are slow. | |
87 | |
88 def statementHasEnded(self, lines): | |
89 """This version lets statements end with ; or with a blank line. | |
90 Override for your own needs.""" | |
91 if not lines.splitlines()[-1].strip(): | |
92 return True | |
93 return (lines.strip()[-1] == ';') | |
58 | 94 |
59 def do_history(self, arg): | 95 def do_history(self, arg): |
60 """history [arg]: lists past commands issued | 96 """history [arg]: lists past commands issued |
61 | 97 |
62 no arg -> list all | 98 no arg -> list all |
92 except: | 128 except: |
93 pass | 129 pass |
94 do_hi = do_history | 130 do_hi = do_history |
95 do_l = do_list | 131 do_l = do_list |
96 do_li = do_list | 132 do_li = do_list |
133 | |
134 | |
97 | 135 |