blob: 508c4dedf745d9b3c8a55ec96a16e613cf104b30 [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:
94 self.default(line)
95 return
96 elif not line:
97 self.emptyline()
98 return
99 self.lastcmd = line
Guido van Rossumb53e6781992-01-24 01:12:17 +0000100 i, n = 0, len(line)
101 while i < n and line[i] in self.identchars: i = i+1
102 cmd, arg = line[:i], string.strip(line[i:])
103 if cmd == '':
104 return self.default(line)
105 else:
106 try:
Guido van Rossumc629d341992-11-05 10:43:02 +0000107 func = getattr(self, 'do_' + cmd)
Guido van Rossumb53e6781992-01-24 01:12:17 +0000108 except AttributeError:
109 return self.default(line)
110 return func(arg)
111
Guido van Rossum80884071998-06-29 17:58:55 +0000112 def emptyline(self):
113 return self.onecmd(self.lastcmd)
114
Guido van Rossumb53e6781992-01-24 01:12:17 +0000115 def default(self, line):
116 print '*** Unknown syntax:', line
117
118 def do_help(self, arg):
119 if arg:
120 # XXX check arg syntax
121 try:
Guido van Rossumc629d341992-11-05 10:43:02 +0000122 func = getattr(self, 'help_' + arg)
Guido van Rossumb53e6781992-01-24 01:12:17 +0000123 except:
124 print '*** No help on', `arg`
125 return
126 func()
127 else:
Guido van Rossum7ef2a1d1998-05-22 14:11:57 +0000128 names = dir(self.__class__)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000129 cmds_doc = []
130 cmds_undoc = []
131 help = {}
132 for name in names:
133 if name[:5] == 'help_':
134 help[name[5:]]=1
Guido van Rossumb53e6781992-01-24 01:12:17 +0000135 for name in names:
136 if name[:3] == 'do_':
Guido van Rossumb6775db1994-08-01 11:34:53 +0000137 cmd=name[3:]
138 if help.has_key(cmd):
139 cmds_doc.append(cmd)
140 del help[cmd]
141 else:
142 cmds_undoc.append(cmd)
143 print
Guido van Rossum80884071998-06-29 17:58:55 +0000144 self.print_topics(self.doc_header, cmds_doc, 15,80)
145 self.print_topics(self.misc_header, help.keys(),15,80)
146 self.print_topics(self.undoc_header, cmds_undoc, 15,80)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147
148 def print_topics(self, header, cmds, cmdlen, maxcol):
149 if cmds:
150 print header;
Guido van Rossum80884071998-06-29 17:58:55 +0000151 if self.ruler:
152 print self.ruler * len(header)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000153 (cmds_per_line,junk)=divmod(maxcol,cmdlen)
154 col=cmds_per_line
155 for cmd in cmds:
156 if col==0: print
157 print (("%-"+`cmdlen`+"s") % cmd),
158 col = (col+1) % cmds_per_line
159 print "\n"