Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9f4860

Browse files
committed
Changes by Richard Wolff:
1) I added a command queue which is helpful to me (at least so far) and would also allow syntax like 's;s' (step; step) in conjunction with precmd 2) doc_leader allows the derived class to print a message before the help output. Defaults to current practise of a blank line 3) nohelp allows one to override the 'No help on' message. I need 'Undefined command: "%s". Try "help".' 4) Pass line to self.precmd to allow one to do some parsing: change first word to lower case, strip out a leading number, whatever. 5) Pass the result of onecmd and the input line to postcmd. This allows one to ponder the stop result before it is effective. 6) emptyline() requires a if self.lastcmd: conditional because if the first command is null (<cr>), you get an infinite recursion with the code as it stands.
1 parent de57030 commit b9f4860

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

Lib/cmd.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ class Cmd:
4747
identchars = IDENTCHARS
4848
ruler = '='
4949
lastcmd = ''
50+
cmdqueue = []
5051
intro = None
52+
doc_leader = ""
5153
doc_header = "Documented commands (type help <topic>):"
5254
misc_header = "Miscellaneous help topics:"
5355
undoc_header = "Undocumented commands:"
56+
nohelp = "*** No help on %s"
5457

5558
def __init__(self): pass
5659

@@ -62,20 +65,24 @@ def cmdloop(self, intro=None):
6265
print self.intro
6366
stop = None
6467
while not stop:
65-
try:
66-
line = raw_input(self.prompt)
67-
except EOFError:
68-
line = 'EOF'
69-
self.precmd()
68+
if self.cmdqueue:
69+
line = self.cmdqueue[0]
70+
del self.cmdqueue[0]
71+
else:
72+
try:
73+
line = raw_input(self.prompt)
74+
except EOFError:
75+
line = 'EOF'
76+
line = self.precmd(line)
7077
stop = self.onecmd(line)
71-
self.postcmd()
78+
stop = self.postcmd(stop, line)
7279
self.postloop()
7380

74-
def precmd(self):
75-
pass
81+
def precmd(self, line):
82+
return line
7683

77-
def postcmd(self):
78-
pass
84+
def postcmd(self, stop, line):
85+
return stop
7986

8087
def preloop(self):
8188
pass
@@ -108,7 +115,8 @@ def onecmd(self, line):
108115
return func(arg)
109116

110117
def emptyline(self):
111-
return self.onecmd(self.lastcmd)
118+
if self.lastcmd:
119+
return self.onecmd(self.lastcmd)
112120

113121
def default(self, line):
114122
print '*** Unknown syntax:', line
@@ -119,7 +127,7 @@ def do_help(self, arg):
119127
try:
120128
func = getattr(self, 'help_' + arg)
121129
except:
122-
print '*** No help on', `arg`
130+
print self.nohelp % (arg,)
123131
return
124132
func()
125133
else:
@@ -138,7 +146,7 @@ def do_help(self, arg):
138146
del help[cmd]
139147
else:
140148
cmds_undoc.append(cmd)
141-
print
149+
print self.doc_leader
142150
self.print_topics(self.doc_header, cmds_doc, 15,80)
143151
self.print_topics(self.misc_header, help.keys(),15,80)
144152
self.print_topics(self.undoc_header, cmds_undoc, 15,80)

0 commit comments

Comments
 (0)