blob: 35b9e0b1fc6e97337ed831c03d979d98ae4dd2cf [file] [log] [blame]
Guido van Rossumb53e6781992-01-24 01:12:17 +00001# A generic class to build line-oriented command interpreters
Guido van Rossum80884071998-06-29 17:58:55 +00002#
3# Interpreters constructed with this class obey the following conventions:
4#
5# 1. End of file on input is processed as the command 'EOF'.
6# 2. A command is parsed out of each line by collecting the prefix composed
7# of characters in the identchars member.
8# 3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method
9# is passed a single argument consisting of the remainder of the line.
10# 4. Typing an empty line repeats the last command. (Actually, it calls the
11# method `emptyline', which may be overridden in a subclass.)
12# 5. There is a predefined `help' method. Given an argument `topic', it
13# calls the command `help_topic'. With no arguments, it lists all topics
14# with defined help_ functions, broken into up to three topics; documented
15# commands, miscellaneous help topics, and undocumented commands.
16# 6. The command '?' is a synonym for `help'. The command '!' is a synonym
17# for `shell', if a do_shell method exists.
18#
19# The `default' method may be overridden to intercept commands for which there
20# is no do_ method.
21#
22# The data member `self.ruler' sets the character used to draw separator lines
23# in the help messages. If empty, no ruler line is drawn. It defaults to "=".
24#
25# If the value of `self.intro' is nonempty when the cmdloop method is called,
26# it is printed out on interpreter startup. This value may be overridden
27# via an optional argument to the cmdloop() method.
28#
29# The data members `self.doc_header', `self.misc_header', and
30# `self.undoc_header' set the headers used for the help function's
31# listings of documented functions, miscellaneous topics, and undocumented
32# functions respectively.
33#
34# These interpreters use raw_input; thus, if the readline module is loaded,
35# they automatically support Emacs-like command history and editing features.
36#
Guido van Rossumb53e6781992-01-24 01:12:17 +000037
38import string
39import sys
40import linecache
41
42PROMPT = '(Cmd) '
43IDENTCHARS = string.letters + string.digits + '_'
44
45class Cmd:
Guido van Rossum80884071998-06-29 17:58:55 +000046 prompt = PROMPT
47 identchars = IDENTCHARS
48 ruler = '='
49 lastcmd = ''
50 intro = None
51 doc_header = "Documented commands (type help <topic>):"
52 misc_header = "Miscellaneous help topics:"
53 undoc_header = "Undocumented commands:"
Guido van Rossum9b3bc711993-06-20 21:02:22 +000054
Guido van Rossum030eb111998-07-01 22:53:04 +000055 def __init__(self): pass
56
Guido van Rossum80884071998-06-29 17:58:55 +000057 def cmdloop(self, intro=None):
58 self.preloop()
59 if intro != None:
60 self.intro = intro
61 if self.intro:
62 print self.intro
Guido van Rossumb53e6781992-01-24 01:12:17 +000063 stop = None
64 while not stop:
65 try:
66 line = raw_input(self.prompt)
67 except EOFError:
68 line = 'EOF'
Guido van Rossum80884071998-06-29 17:58:55 +000069 self.precmd()
Guido van Rossumb53e6781992-01-24 01:12:17 +000070 stop = self.onecmd(line)
Guido van Rossum80884071998-06-29 17:58:55 +000071 self.postcmd()
72 self.postloop()
73
74 def precmd(self):
75 pass
76
77 def postcmd(self):
78 pass
79
80 def preloop(self):
81 pass
82
83 def postloop(self):
84 pass
Guido van Rossumb53e6781992-01-24 01:12:17 +000085
86 def onecmd(self, line):
87 line = string.strip(line)
Guido van Rossum80884071998-06-29 17:58:55 +000088 if line == '?':
89 line = 'help'
90 elif line == '!':
91 if hasattr(self, 'do_shell'):
92 line = 'shell'
93 else:
Guido van Rossumc6126811998-07-20 21:22:08 +000094 return self.default(line)
Guido van Rossum80884071998-06-29 17:58:55 +000095 elif not line:
Guido van Rossumc6126811998-07-20 21:22:08 +000096 return self.emptyline()
Guido van Rossum80884071998-06-29 17:58:55 +000097 self.lastcmd = line
Guido van Rossumb53e6781992-01-24 01:12:17 +000098 i, n = 0, len(line)
99 while i < n and line[i] in self.identchars: i = i+1
100 cmd, arg = line[:i], string.strip(line[i:])
101 if cmd == '':
102 return self.default(line)
103 else:
104 try:
Guido van Rossumc629d341992-11-05 10:43:02 +0000105 func = getattr(self, 'do_' + cmd)
Guido van Rossumb53e6781992-01-24 01:12:17 +0000106 except AttributeError:
107 return self.default(line)
108 return func(arg)
109
Guido van Rossum80884071998-06-29 17:58:55 +0000110 def emptyline(self):
111 return self.onecmd(self.lastcmd)
112
Guido van Rossumb53e6781992-01-24 01:12:17 +0000113 def default(self, line):
114 print '*** Unknown syntax:', line
115
116 def do_help(self, arg):
117 if arg:
118 # XXX check arg syntax
119 try:
Guido van Rossumc629d341992-11-05 10:43:02 +0000120 func = getattr(self, 'help_' + arg)
Guido van Rossumb53e6781992-01-24 01:12:17 +0000121 except:
122 print '*** No help on', `arg`
123 return
124 func()
125 else:
Guido van Rossum7ef2a1d1998-05-22 14:11:57 +0000126 names = dir(self.__class__)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000127 cmds_doc = []
128 cmds_undoc = []
129 help = {}
130 for name in names:
131 if name[:5] == 'help_':
132 help[name[5:]]=1
Guido van Rossumb53e6781992-01-24 01:12:17 +0000133 for name in names:
134 if name[:3] == 'do_':
Guido van Rossumb6775db1994-08-01 11:34:53 +0000135 cmd=name[3:]
136 if help.has_key(cmd):
137 cmds_doc.append(cmd)
138 del help[cmd]
139 else:
140 cmds_undoc.append(cmd)
141 print
Guido van Rossum80884071998-06-29 17:58:55 +0000142 self.print_topics(self.doc_header, cmds_doc, 15,80)
143 self.print_topics(self.misc_header, help.keys(),15,80)
144 self.print_topics(self.undoc_header, cmds_undoc, 15,80)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000145
146 def print_topics(self, header, cmds, cmdlen, maxcol):
147 if cmds:
148 print header;
Guido van Rossum80884071998-06-29 17:58:55 +0000149 if self.ruler:
150 print self.ruler * len(header)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000151 (cmds_per_line,junk)=divmod(maxcol,cmdlen)
152 col=cmds_per_line
153 for cmd in cmds:
154 if col==0: print
155 print (("%-"+`cmdlen`+"s") % cmd),
156 col = (col+1) % cmds_per_line
157 print "\n"