Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """A generic class to build line-oriented command interpreters. |
| 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. |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 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 |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 23 | indexes of the text being matched, which could be used to provide |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 24 | different completion depending upon which position the argument is in. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 25 | |
| 26 | The `default' method may be overridden to intercept commands for which there |
| 27 | is no do_ method. |
| 28 | |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 29 | The `completedefault' method may be overridden to intercept completions for |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 30 | commands that have no complete_ method. |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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 "=". |
| 34 | |
| 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. |
| 38 | |
| 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. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 43 | """ |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 44 | |
Neal Norwitz | ce96f69 | 2006-03-17 06:49:51 +0000 | [diff] [blame] | 45 | import string, sys |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 46 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 47 | __all__ = ["Cmd"] |
| 48 | |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 49 | PROMPT = '(Cmd) ' |
Fred Drake | 79e75e1 | 2001-07-20 19:05:50 +0000 | [diff] [blame] | 50 | IDENTCHARS = string.ascii_letters + string.digits + '_' |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 51 | |
| 52 | class Cmd: |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 53 | """A simple framework for writing line-oriented command interpreters. |
| 54 | |
| 55 | These are often useful for test harnesses, administrative tools, and |
| 56 | prototypes that will later be wrapped in a more sophisticated interface. |
| 57 | |
| 58 | A Cmd instance or subclass instance is a line-oriented interpreter |
| 59 | framework. There is no good reason to instantiate Cmd itself; rather, |
| 60 | it's useful as a superclass of an interpreter class you define yourself |
| 61 | in order to inherit Cmd's methods and encapsulate action methods. |
| 62 | |
| 63 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 64 | prompt = PROMPT |
| 65 | identchars = IDENTCHARS |
| 66 | ruler = '=' |
| 67 | lastcmd = '' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 68 | intro = None |
| 69 | doc_leader = "" |
| 70 | doc_header = "Documented commands (type help <topic>):" |
| 71 | misc_header = "Miscellaneous help topics:" |
| 72 | undoc_header = "Undocumented commands:" |
| 73 | nohelp = "*** No help on %s" |
Guido van Rossum | bfb9184 | 2001-03-22 21:59:20 +0000 | [diff] [blame] | 74 | use_rawinput = 1 |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 75 | |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 76 | def __init__(self, completekey='tab', stdin=None, stdout=None): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 77 | """Instantiate a line-oriented interpreter framework. |
| 78 | |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 79 | The optional argument 'completekey' is the readline name of a |
| 80 | completion key; it defaults to the Tab key. If completekey is |
| 81 | not None and the readline module is available, command completion |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 82 | is done automatically. The optional arguments stdin and stdout |
| 83 | specify alternate input and output file objects; if not specified, |
| 84 | sys.stdin and sys.stdout are used. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 85 | |
| 86 | """ |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 87 | import sys |
| 88 | if stdin is not None: |
| 89 | self.stdin = stdin |
| 90 | else: |
| 91 | self.stdin = sys.stdin |
| 92 | if stdout is not None: |
| 93 | self.stdout = stdout |
| 94 | else: |
| 95 | self.stdout = sys.stdout |
Guido van Rossum | 87fec22 | 2003-01-13 21:18:54 +0000 | [diff] [blame] | 96 | self.cmdqueue = [] |
Michael W. Hudson | 35a92ce | 2003-02-03 11:04:27 +0000 | [diff] [blame] | 97 | self.completekey = completekey |
Guido van Rossum | 030eb11 | 1998-07-01 22:53:04 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 99 | def cmdloop(self, intro=None): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 100 | """Repeatedly issue a prompt, accept input, parse an initial prefix |
| 101 | off the received input, and dispatch to action methods, passing them |
| 102 | the remainder of the line as argument. |
| 103 | |
| 104 | """ |
| 105 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 106 | self.preloop() |
Michael W. Hudson | d7cc1bd | 2004-07-01 14:52:10 +0000 | [diff] [blame] | 107 | if self.use_rawinput and self.completekey: |
| 108 | try: |
| 109 | import readline |
| 110 | self.old_completer = readline.get_completer() |
| 111 | readline.set_completer(self.complete) |
| 112 | readline.parse_and_bind(self.completekey+": complete") |
| 113 | except ImportError: |
| 114 | pass |
| 115 | try: |
| 116 | if intro is not None: |
| 117 | self.intro = intro |
| 118 | if self.intro: |
| 119 | self.stdout.write(str(self.intro)+"\n") |
| 120 | stop = None |
| 121 | while not stop: |
| 122 | if self.cmdqueue: |
| 123 | line = self.cmdqueue.pop(0) |
Guido van Rossum | bfb9184 | 2001-03-22 21:59:20 +0000 | [diff] [blame] | 124 | else: |
Michael W. Hudson | d7cc1bd | 2004-07-01 14:52:10 +0000 | [diff] [blame] | 125 | if self.use_rawinput: |
| 126 | try: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 127 | line = input(self.prompt) |
Michael W. Hudson | d7cc1bd | 2004-07-01 14:52:10 +0000 | [diff] [blame] | 128 | except EOFError: |
| 129 | line = 'EOF' |
Guido van Rossum | bfb9184 | 2001-03-22 21:59:20 +0000 | [diff] [blame] | 130 | else: |
Michael W. Hudson | d7cc1bd | 2004-07-01 14:52:10 +0000 | [diff] [blame] | 131 | self.stdout.write(self.prompt) |
| 132 | self.stdout.flush() |
| 133 | line = self.stdin.readline() |
| 134 | if not len(line): |
| 135 | line = 'EOF' |
| 136 | else: |
| 137 | line = line[:-1] # chop \n |
| 138 | line = self.precmd(line) |
| 139 | stop = self.onecmd(line) |
| 140 | stop = self.postcmd(stop, line) |
| 141 | self.postloop() |
| 142 | finally: |
| 143 | if self.use_rawinput and self.completekey: |
| 144 | try: |
| 145 | import readline |
| 146 | readline.set_completer(self.old_completer) |
| 147 | except ImportError: |
| 148 | pass |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 151 | def precmd(self, line): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 152 | """Hook method executed just before the command line is |
| 153 | interpreted, but after the input prompt is generated and issued. |
| 154 | |
| 155 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 156 | return line |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 157 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 158 | def postcmd(self, stop, line): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 159 | """Hook method executed just after a command dispatch is finished.""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 160 | return stop |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 161 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 162 | def preloop(self): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 163 | """Hook method executed once when the cmdloop() method is called.""" |
Michael W. Hudson | d7cc1bd | 2004-07-01 14:52:10 +0000 | [diff] [blame] | 164 | pass |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 166 | def postloop(self): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 167 | """Hook method executed once when the cmdloop() method is about to |
| 168 | return. |
| 169 | |
| 170 | """ |
Michael W. Hudson | d7cc1bd | 2004-07-01 14:52:10 +0000 | [diff] [blame] | 171 | pass |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 172 | |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 173 | def parseline(self, line): |
Andrew M. Kuchling | 7cebbf3 | 2003-10-22 14:38:54 +0000 | [diff] [blame] | 174 | """Parse the line into a command name and a string containing |
| 175 | the arguments. Returns a tuple containing (command, args, line). |
| 176 | 'command' and 'args' may be None if the line couldn't be parsed. |
| 177 | """ |
Eric S. Raymond | 20e4423 | 2001-02-09 04:52:11 +0000 | [diff] [blame] | 178 | line = line.strip() |
Eric S. Raymond | 5f1b270 | 2000-07-11 13:03:55 +0000 | [diff] [blame] | 179 | if not line: |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 180 | return None, None, line |
Eric S. Raymond | 5f1b270 | 2000-07-11 13:03:55 +0000 | [diff] [blame] | 181 | elif line[0] == '?': |
| 182 | line = 'help ' + line[1:] |
| 183 | elif line[0] == '!': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 184 | if hasattr(self, 'do_shell'): |
Eric S. Raymond | 5f1b270 | 2000-07-11 13:03:55 +0000 | [diff] [blame] | 185 | line = 'shell ' + line[1:] |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 186 | else: |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 187 | return None, None, line |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 188 | i, n = 0, len(line) |
| 189 | while i < n and line[i] in self.identchars: i = i+1 |
Eric S. Raymond | 20e4423 | 2001-02-09 04:52:11 +0000 | [diff] [blame] | 190 | cmd, arg = line[:i], line[i:].strip() |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 191 | return cmd, arg, line |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 192 | |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 193 | def onecmd(self, line): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 194 | """Interpret the argument as though it had been typed in response |
| 195 | to the prompt. |
| 196 | |
| 197 | This may be overridden, but should not normally need to be; |
| 198 | see the precmd() and postcmd() methods for useful execution hooks. |
| 199 | The return value is a flag indicating whether interpretation of |
| 200 | commands by the interpreter should stop. |
| 201 | |
| 202 | """ |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 203 | cmd, arg, line = self.parseline(line) |
| 204 | if not line: |
| 205 | return self.emptyline() |
| 206 | if cmd is None: |
| 207 | return self.default(line) |
| 208 | self.lastcmd = line |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 209 | if cmd == '': |
| 210 | return self.default(line) |
| 211 | else: |
| 212 | try: |
| 213 | func = getattr(self, 'do_' + cmd) |
| 214 | except AttributeError: |
| 215 | return self.default(line) |
| 216 | return func(arg) |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 218 | def emptyline(self): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 219 | """Called when an empty line is entered in response to the prompt. |
| 220 | |
| 221 | If this method is not overridden, it repeats the last nonempty |
| 222 | command entered. |
| 223 | |
| 224 | """ |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 225 | if self.lastcmd: |
| 226 | return self.onecmd(self.lastcmd) |
Guido van Rossum | 8088407 | 1998-06-29 17:58:55 +0000 | [diff] [blame] | 227 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 228 | def default(self, line): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 229 | """Called on an input line when the command prefix is not recognized. |
| 230 | |
| 231 | If this method is not overridden, it prints an error message and |
| 232 | returns. |
| 233 | |
| 234 | """ |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 235 | self.stdout.write('*** Unknown syntax: %s\n'%line) |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 236 | |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 237 | def completedefault(self, *ignored): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 238 | """Method called to complete an input line when no command-specific |
| 239 | complete_*() method is available. |
| 240 | |
| 241 | By default, it returns an empty list. |
| 242 | |
| 243 | """ |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 244 | return [] |
| 245 | |
| 246 | def completenames(self, text, *ignored): |
| 247 | dotext = 'do_'+text |
| 248 | return [a[3:] for a in self.get_names() if a.startswith(dotext)] |
| 249 | |
| 250 | def complete(self, text, state): |
| 251 | """Return the next possible completion for 'text'. |
| 252 | |
| 253 | If a command has not been entered, then complete against command list. |
| 254 | Otherwise try to call complete_<command> to get list of completions. |
| 255 | """ |
| 256 | if state == 0: |
| 257 | import readline |
| 258 | origline = readline.get_line_buffer() |
| 259 | line = origline.lstrip() |
| 260 | stripped = len(origline) - len(line) |
| 261 | begidx = readline.get_begidx() - stripped |
| 262 | endidx = readline.get_endidx() - stripped |
| 263 | if begidx>0: |
| 264 | cmd, args, foo = self.parseline(line) |
| 265 | if cmd == '': |
| 266 | compfunc = self.completedefault |
| 267 | else: |
| 268 | try: |
| 269 | compfunc = getattr(self, 'complete_' + cmd) |
| 270 | except AttributeError: |
| 271 | compfunc = self.completedefault |
| 272 | else: |
| 273 | compfunc = self.completenames |
| 274 | self.completion_matches = compfunc(text, line, begidx, endidx) |
| 275 | try: |
| 276 | return self.completion_matches[state] |
| 277 | except IndexError: |
| 278 | return None |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 279 | |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 280 | def get_names(self): |
| 281 | # Inheritance says we have to look in class and |
| 282 | # base classes; order is not important. |
| 283 | names = [] |
| 284 | classes = [self.__class__] |
| 285 | while classes: |
Raymond Hettinger | 46ac8eb | 2002-06-30 03:39:14 +0000 | [diff] [blame] | 286 | aclass = classes.pop(0) |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 287 | if aclass.__bases__: |
| 288 | classes = classes + list(aclass.__bases__) |
| 289 | names = names + dir(aclass) |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 290 | return names |
| 291 | |
| 292 | def complete_help(self, *args): |
| 293 | return self.completenames(*args) |
| 294 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 295 | def do_help(self, arg): |
| 296 | if arg: |
| 297 | # XXX check arg syntax |
| 298 | try: |
| 299 | func = getattr(self, 'help_' + arg) |
Skip Montanaro | 1ce0073 | 2002-03-24 16:34:21 +0000 | [diff] [blame] | 300 | except AttributeError: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 301 | try: |
| 302 | doc=getattr(self, 'do_' + arg).__doc__ |
| 303 | if doc: |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 304 | self.stdout.write("%s\n"%str(doc)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 305 | return |
Skip Montanaro | 1ce0073 | 2002-03-24 16:34:21 +0000 | [diff] [blame] | 306 | except AttributeError: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 307 | pass |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 308 | self.stdout.write("%s\n"%str(self.nohelp % (arg,))) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 309 | return |
| 310 | func() |
| 311 | else: |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 312 | names = self.get_names() |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 313 | cmds_doc = [] |
| 314 | cmds_undoc = [] |
| 315 | help = {} |
| 316 | for name in names: |
| 317 | if name[:5] == 'help_': |
| 318 | help[name[5:]]=1 |
| 319 | names.sort() |
| 320 | # There can be duplicates if routines overridden |
| 321 | prevname = '' |
| 322 | for name in names: |
| 323 | if name[:3] == 'do_': |
| 324 | if name == prevname: |
| 325 | continue |
| 326 | prevname = name |
| 327 | cmd=name[3:] |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 328 | if cmd in help: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 329 | cmds_doc.append(cmd) |
| 330 | del help[cmd] |
| 331 | elif getattr(self, name).__doc__: |
| 332 | cmds_doc.append(cmd) |
| 333 | else: |
| 334 | cmds_undoc.append(cmd) |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 335 | self.stdout.write("%s\n"%str(self.doc_leader)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 336 | self.print_topics(self.doc_header, cmds_doc, 15,80) |
Alexandre Vassalotti | 7b955bd | 2007-06-07 22:37:45 +0000 | [diff] [blame] | 337 | self.print_topics(self.misc_header, list(help.keys()),15,80) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 338 | self.print_topics(self.undoc_header, cmds_undoc, 15,80) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 340 | def print_topics(self, header, cmds, cmdlen, maxcol): |
| 341 | if cmds: |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 342 | self.stdout.write("%s\n"%str(header)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 343 | if self.ruler: |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 344 | self.stdout.write("%s\n"%str(self.ruler * len(header))) |
Guido van Rossum | c706c28 | 2002-12-02 13:08:53 +0000 | [diff] [blame] | 345 | self.columnize(cmds, maxcol-1) |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 346 | self.stdout.write("\n") |
Guido van Rossum | c706c28 | 2002-12-02 13:08:53 +0000 | [diff] [blame] | 347 | |
| 348 | def columnize(self, list, displaywidth=80): |
| 349 | """Display a list of strings as a compact set of columns. |
| 350 | |
| 351 | Each column is only as wide as necessary. |
| 352 | Columns are separated by two spaces (one was not legible enough). |
| 353 | """ |
| 354 | if not list: |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 355 | self.stdout.write("<empty>\n") |
Guido van Rossum | c706c28 | 2002-12-02 13:08:53 +0000 | [diff] [blame] | 356 | return |
Alexandre Vassalotti | 7b955bd | 2007-06-07 22:37:45 +0000 | [diff] [blame] | 357 | |
Guido van Rossum | c706c28 | 2002-12-02 13:08:53 +0000 | [diff] [blame] | 358 | nonstrings = [i for i in range(len(list)) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 359 | if not isinstance(list[i], str)] |
Guido van Rossum | c706c28 | 2002-12-02 13:08:53 +0000 | [diff] [blame] | 360 | if nonstrings: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 361 | raise TypeError("list[i] not a string for i in %s" |
| 362 | % ", ".join(map(str, nonstrings))) |
Guido van Rossum | c706c28 | 2002-12-02 13:08:53 +0000 | [diff] [blame] | 363 | size = len(list) |
| 364 | if size == 1: |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 365 | self.stdout.write('%s\n'%str(list[0])) |
Guido van Rossum | c706c28 | 2002-12-02 13:08:53 +0000 | [diff] [blame] | 366 | return |
| 367 | # Try every row count from 1 upwards |
| 368 | for nrows in range(1, len(list)): |
| 369 | ncols = (size+nrows-1) // nrows |
| 370 | colwidths = [] |
| 371 | totwidth = -2 |
| 372 | for col in range(ncols): |
| 373 | colwidth = 0 |
| 374 | for row in range(nrows): |
| 375 | i = row + nrows*col |
| 376 | if i >= size: |
| 377 | break |
| 378 | x = list[i] |
| 379 | colwidth = max(colwidth, len(x)) |
| 380 | colwidths.append(colwidth) |
| 381 | totwidth += colwidth + 2 |
| 382 | if totwidth > displaywidth: |
| 383 | break |
| 384 | if totwidth <= displaywidth: |
| 385 | break |
| 386 | else: |
| 387 | nrows = len(list) |
| 388 | ncols = 1 |
| 389 | colwidths = [0] |
| 390 | for row in range(nrows): |
| 391 | texts = [] |
| 392 | for col in range(ncols): |
| 393 | i = row + nrows*col |
| 394 | if i >= size: |
| 395 | x = "" |
| 396 | else: |
| 397 | x = list[i] |
| 398 | texts.append(x) |
| 399 | while texts and not texts[-1]: |
| 400 | del texts[-1] |
| 401 | for col in range(len(texts)): |
| 402 | texts[col] = texts[col].ljust(colwidths[col]) |
Anthony Baxter | 983b008 | 2003-02-06 01:45:11 +0000 | [diff] [blame] | 403 | self.stdout.write("%s\n"%str(" ".join(texts))) |