summaryrefslogtreecommitdiff
path: root/Lib/cmd.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-08-27 19:02:51 +0000
committerGuido van Rossum <guido@python.org>1998-08-27 19:02:51 +0000
commitb9f48608f6c0338a85c7162ec07fbad5f95d1f8a (patch)
treede35731da9cf7f9ed5ebbedc299fd932c0b3db7b /Lib/cmd.py
parentde57030a1caedda84ba53921f6fa85fdaa625027 (diff)
downloadcpython-git-b9f48608f6c0338a85c7162ec07fbad5f95d1f8a.tar.gz
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.
Diffstat (limited to 'Lib/cmd.py')
-rw-r--r--Lib/cmd.py34
1 files changed, 21 insertions, 13 deletions
diff --git a/Lib/cmd.py b/Lib/cmd.py
index 35b9e0b1fc..20c01aae89 100644
--- a/Lib/cmd.py
+++ b/Lib/cmd.py
@@ -47,10 +47,13 @@ class Cmd:
identchars = IDENTCHARS
ruler = '='
lastcmd = ''
+ cmdqueue = []
intro = None
+ doc_leader = ""
doc_header = "Documented commands (type help <topic>):"
misc_header = "Miscellaneous help topics:"
undoc_header = "Undocumented commands:"
+ nohelp = "*** No help on %s"
def __init__(self): pass
@@ -62,20 +65,24 @@ class Cmd:
print self.intro
stop = None
while not stop:
- try:
- line = raw_input(self.prompt)
- except EOFError:
- line = 'EOF'
- self.precmd()
+ if self.cmdqueue:
+ line = self.cmdqueue[0]
+ del self.cmdqueue[0]
+ else:
+ try:
+ line = raw_input(self.prompt)
+ except EOFError:
+ line = 'EOF'
+ line = self.precmd(line)
stop = self.onecmd(line)
- self.postcmd()
+ stop = self.postcmd(stop, line)
self.postloop()
- def precmd(self):
- pass
+ def precmd(self, line):
+ return line
- def postcmd(self):
- pass
+ def postcmd(self, stop, line):
+ return stop
def preloop(self):
pass
@@ -108,7 +115,8 @@ class Cmd:
return func(arg)
def emptyline(self):
- return self.onecmd(self.lastcmd)
+ if self.lastcmd:
+ return self.onecmd(self.lastcmd)
def default(self, line):
print '*** Unknown syntax:', line
@@ -119,7 +127,7 @@ class Cmd:
try:
func = getattr(self, 'help_' + arg)
except:
- print '*** No help on', `arg`
+ print self.nohelp % (arg,)
return
func()
else:
@@ -138,7 +146,7 @@ class Cmd:
del help[cmd]
else:
cmds_undoc.append(cmd)
- print
+ print self.doc_leader
self.print_topics(self.doc_header, cmds_doc, 15,80)
self.print_topics(self.misc_header, help.keys(),15,80)
self.print_topics(self.undoc_header, cmds_undoc, 15,80)