1 """A generic class to build line-oriented command interpreters.
3 Interpreters constructed with this class obey the following conventions:
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 7. If completion is enabled, completing commands will be done automatically,
19 and completing of commands args is done by calling complete_foo() with
20 arguments text, line, begidx, endidx. text is string we are matching
21 against, all returned matches must begin with it. line is the current
22 input line (lstripped), begidx and endidx are the beginning and end
23 indexes of the text being matched, which could be used to provide
24 different completion depending upon which position the argument is in.
26 The `default' method may be overridden to intercept commands for which there
29 The `completedefault' method may be overridden to intercept completions for
30 commands that have no complete_ method.
32 The data member `self.ruler' sets the character used to draw separator lines
33 in the help messages. If empty, no ruler line is drawn. It defaults to "=".
35 If the value of `self.intro' is nonempty when the cmdloop method is called,
36 it is printed out on interpreter startup. This value may be overridden
37 via an optional argument to the cmdloop() method.
39 The data members `self.doc_header', `self.misc_header', and
40 `self.undoc_header' set the headers used for the help function's
41 listings of documented functions, miscellaneous topics, and undocumented
42 functions respectively.
44 These interpreters use raw_input; thus, if the readline module is loaded,
45 they automatically support Emacs-like command history and editing features.
53 IDENTCHARS = string.ascii_letters + string.digits +
'_'
57 identchars = IDENTCHARS
63 doc_header =
"Documented commands (type help <topic>):"
64 misc_header =
"Miscellaneous help topics:"
65 undoc_header =
"Undocumented commands:"
66 nohelp =
"*** No help on %s"
73 readline.set_completer(self.
complete)
74 readline.parse_and_bind(completekey+
": complete")
92 line = raw_input(self.
prompt)
96 sys.stdout.write(self.
prompt)
98 line = sys.stdin.readline()
105 stop = self.
postcmd(stop, line)
123 return None,
None, line
125 line =
'help ' + line[1:]
127 if hasattr(self,
'do_shell'):
128 line =
'shell ' + line[1:]
130 return None,
None, line
132 while i < n
and line[i]
in self.identchars: i = i+1
133 cmd, arg = line[:i], line[i:].
strip()
134 return cmd, arg, line
147 func = getattr(self,
'do_' + cmd)
148 except AttributeError:
157 print '*** Unknown syntax:', line
164 return [a[3:]
for a
in self.
get_names()
if a.startswith(dotext)]
167 """Return the next possible completion for 'text'.
169 If a command has not been entered, then complete against command list.
170 Otherwise try to call complete_<command> to get list of completions.
174 origline = readline.get_line_buffer()
175 line = origline.lstrip()
176 stripped = len(origline) - len(line)
177 begidx = readline.get_begidx() - stripped
178 endidx = readline.get_endidx() - stripped
185 compfunc = getattr(self,
'complete_' + cmd)
186 except AttributeError:
200 classes = [self.__class__]
204 classes = classes + list(aclass.__bases__)
205 names = names +
dir(aclass)
216 func = getattr(self,
'help_' + arg)
219 doc=getattr(self,
'do_' + arg).__doc__
225 print self.
nohelp % (arg,)
234 if name[:5] ==
'help_':
240 if name[:3] ==
'do_':
245 if help.has_key(cmd):
248 elif getattr(self, name).__doc__:
251 cmds_undoc.append(cmd)
261 print self.
ruler * len(header)
262 (cmds_per_line,junk)=divmod(maxcol,cmdlen)
266 print ((
"%-"+`cmdlen`+
"s") % cmd),
267 col = (col+1) % cmds_per_line