Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 1 | # A generic class to build line-oriented command interpreters |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 2 | # |
| 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 Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 37 | |
| 38 | import string |
| 39 | import sys |
| 40 | import linecache |
| 41 | |
| 42 | PROMPT = '(Cmd) ' |
| 43 | IDENTCHARS = string.letters + string.digits + '_' |
| 44 | |
| 45 | class Cmd: |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 46 | 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 Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 030eb11 | 1998-07-01 22:53:04 +0000 | [diff] [blame] | 55 | def __init__(self): pass |
| 56 | |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 57 | 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 Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 63 | stop = None |
| 64 | while not stop: |
| 65 | try: |
| 66 | line = raw_input(self.prompt) |
| 67 | except EOFError: |
| 68 | line = 'EOF' |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 69 | self.precmd() |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 70 | stop = self.onecmd(line) |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 71 | 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 Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 85 | |
| 86 | def onecmd(self, line): |
| 87 | line = string.strip(line) |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 88 | if line == '?': |
| 89 | line = 'help' |
| 90 | elif line == '!': |
| 91 | if hasattr(self, 'do_shell'): |
| 92 | line = 'shell' |
| 93 | else: |
Guido van Rossum | c612681 | 1998-07-20 21:22:08 +0000 | [diff] [blame] | 94 | return self.default(line) |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 95 | elif not line: |
Guido van Rossum | c612681 | 1998-07-20 21:22:08 +0000 | [diff] [blame] | 96 | return self.emptyline() |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 97 | self.lastcmd = line |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 98 | 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 Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 105 | func = getattr(self, 'do_' + cmd) |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 106 | except AttributeError: |
| 107 | return self.default(line) |
| 108 | return func(arg) |
| 109 | |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 110 | def emptyline(self): |
| 111 | return self.onecmd(self.lastcmd) |
| 112 | |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 113 | 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 Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 120 | func = getattr(self, 'help_' + arg) |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 121 | except: |
| 122 | print '*** No help on', `arg` |
| 123 | return |
| 124 | func() |
| 125 | else: |
Guido van Rossum | 7ef2a1d | 1998-05-22 14:11:57 +0000 | [diff] [blame] | 126 | names = dir(self.__class__) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 127 | cmds_doc = [] |
| 128 | cmds_undoc = [] |
| 129 | help = {} |
| 130 | for name in names: |
| 131 | if name[:5] == 'help_': |
| 132 | help[name[5:]]=1 |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 133 | for name in names: |
| 134 | if name[:3] == 'do_': |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 135 | 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 Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 142 | 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 145 | |
| 146 | def print_topics(self, header, cmds, cmdlen, maxcol): |
| 147 | if cmds: |
| 148 | print header; |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 149 | if self.ruler: |
| 150 | print self.ruler * len(header) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 151 | (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" |