Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 1 | # A generic class to build line-oriented command interpreters |
| 2 | |
| 3 | import string |
| 4 | import sys |
| 5 | import linecache |
| 6 | |
| 7 | PROMPT = '(Cmd) ' |
| 8 | IDENTCHARS = string.letters + string.digits + '_' |
| 9 | |
| 10 | class Cmd: |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 11 | |
| 12 | def __init__(self): |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 13 | self.prompt = PROMPT |
| 14 | self.identchars = IDENTCHARS |
| 15 | self.lastcmd = '' |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 16 | |
| 17 | def cmdloop(self): |
| 18 | stop = None |
| 19 | while not stop: |
| 20 | try: |
| 21 | line = raw_input(self.prompt) |
| 22 | except EOFError: |
| 23 | line = 'EOF' |
| 24 | stop = self.onecmd(line) |
| 25 | |
| 26 | def onecmd(self, line): |
| 27 | line = string.strip(line) |
| 28 | if not line: |
| 29 | line = self.lastcmd |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 30 | else: |
| 31 | self.lastcmd = line |
| 32 | i, n = 0, len(line) |
| 33 | while i < n and line[i] in self.identchars: i = i+1 |
| 34 | cmd, arg = line[:i], string.strip(line[i:]) |
| 35 | if cmd == '': |
| 36 | return self.default(line) |
| 37 | else: |
| 38 | try: |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 39 | func = getattr(self, 'do_' + cmd) |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 40 | except AttributeError: |
| 41 | return self.default(line) |
| 42 | return func(arg) |
| 43 | |
| 44 | def default(self, line): |
| 45 | print '*** Unknown syntax:', line |
| 46 | |
| 47 | def do_help(self, arg): |
| 48 | if arg: |
| 49 | # XXX check arg syntax |
| 50 | try: |
Guido van Rossum | c629d34 | 1992-11-05 10:43:02 +0000 | [diff] [blame] | 51 | func = getattr(self, 'help_' + arg) |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 52 | except: |
| 53 | print '*** No help on', `arg` |
| 54 | return |
| 55 | func() |
| 56 | else: |
Guido van Rossum | 732282f | 1992-08-19 16:49:37 +0000 | [diff] [blame] | 57 | import newdir |
Guido van Rossum | 9b3bc71 | 1993-06-20 21:02:22 +0000 | [diff] [blame] | 58 | names = newdir.dir(self.__class__) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 59 | cmds_doc = [] |
| 60 | cmds_undoc = [] |
| 61 | help = {} |
| 62 | for name in names: |
| 63 | if name[:5] == 'help_': |
| 64 | help[name[5:]]=1 |
Guido van Rossum | b53e678 | 1992-01-24 01:12:17 +0000 | [diff] [blame] | 65 | for name in names: |
| 66 | if name[:3] == 'do_': |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 67 | cmd=name[3:] |
| 68 | if help.has_key(cmd): |
| 69 | cmds_doc.append(cmd) |
| 70 | del help[cmd] |
| 71 | else: |
| 72 | cmds_undoc.append(cmd) |
| 73 | print |
| 74 | self.print_topics("Documented commands (type help " \ |
| 75 | "<topic>):",cmds_doc, 15, 80) |
| 76 | self.print_topics("Miscellaneous help topics:", |
| 77 | help.keys(), 15, 80) |
| 78 | self.print_topics("Undocumented commands:", |
| 79 | cmds_undoc, 15, 80) |
| 80 | |
| 81 | def print_topics(self, header, cmds, cmdlen, maxcol): |
| 82 | if cmds: |
| 83 | print header; |
| 84 | print "="*len(header) |
| 85 | (cmds_per_line,junk)=divmod(maxcol,cmdlen) |
| 86 | col=cmds_per_line |
| 87 | for cmd in cmds: |
| 88 | if col==0: print |
| 89 | print (("%-"+`cmdlen`+"s") % cmd), |
| 90 | col = (col+1) % cmds_per_line |
| 91 | print "\n" |