Mercurial > cpython-lru_cache-weakref
annotate Lib/cmd.py @ 8831:00a6bb65ff29 legacy-trunk
Getting rid of this ancient stuff.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 05 Aug 1998 21:10:09 +0000 |
| parents | de540166d732 |
| children | 16f8397d8f04 |
| rev | line source |
|---|---|
| 624 | 1 # A generic class to build line-oriented command interpreters |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
2 # |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
3 # Interpreters constructed with this class obey the following conventions: |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
4 # |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
5 # 1. End of file on input is processed as the command 'EOF'. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
6 # 2. A command is parsed out of each line by collecting the prefix composed |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
7 # of characters in the identchars member. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
8 # 3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
9 # is passed a single argument consisting of the remainder of the line. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
10 # 4. Typing an empty line repeats the last command. (Actually, it calls the |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
11 # method `emptyline', which may be overridden in a subclass.) |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
12 # 5. There is a predefined `help' method. Given an argument `topic', it |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
13 # calls the command `help_topic'. With no arguments, it lists all topics |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
14 # with defined help_ functions, broken into up to three topics; documented |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
15 # commands, miscellaneous help topics, and undocumented commands. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
16 # 6. The command '?' is a synonym for `help'. The command '!' is a synonym |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
17 # for `shell', if a do_shell method exists. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
18 # |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
19 # The `default' method may be overridden to intercept commands for which there |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
20 # is no do_ method. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
21 # |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
22 # The data member `self.ruler' sets the character used to draw separator lines |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
23 # in the help messages. If empty, no ruler line is drawn. It defaults to "=". |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
24 # |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
25 # If the value of `self.intro' is nonempty when the cmdloop method is called, |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
26 # it is printed out on interpreter startup. This value may be overridden |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
27 # via an optional argument to the cmdloop() method. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
28 # |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
29 # The data members `self.doc_header', `self.misc_header', and |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
30 # `self.undoc_header' set the headers used for the help function's |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
31 # listings of documented functions, miscellaneous topics, and undocumented |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
32 # functions respectively. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
33 # |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
34 # These interpreters use raw_input; thus, if the readline module is loaded, |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
35 # they automatically support Emacs-like command history and editing features. |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
36 # |
| 624 | 37 |
| 38 import string | |
| 39 import sys | |
| 40 import linecache | |
| 41 | |
| 42 PROMPT = '(Cmd) ' | |
| 43 IDENTCHARS = string.letters + string.digits + '_' | |
| 44 | |
| 45 class Cmd: | |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
46 prompt = PROMPT |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
47 identchars = IDENTCHARS |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
48 ruler = '=' |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
49 lastcmd = '' |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
50 intro = None |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
51 doc_header = "Documented commands (type help <topic>):" |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
52 misc_header = "Miscellaneous help topics:" |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
53 undoc_header = "Undocumented commands:" |
|
1315
acdc0b9a6c78
* aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
54 |
|
8615
e7822b64a33d
Gotta have an __init__() method -- pdb.py calls it!
Guido van Rossum <guido@python.org>
parents:
8584
diff
changeset
|
55 def __init__(self): pass |
|
e7822b64a33d
Gotta have an __init__() method -- pdb.py calls it!
Guido van Rossum <guido@python.org>
parents:
8584
diff
changeset
|
56 |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
57 def cmdloop(self, intro=None): |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
58 self.preloop() |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
59 if intro != None: |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
60 self.intro = intro |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
61 if self.intro: |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
62 print self.intro |
| 624 | 63 stop = None |
| 64 while not stop: | |
| 65 try: | |
| 66 line = raw_input(self.prompt) | |
| 67 except EOFError: | |
| 68 line = 'EOF' | |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
69 self.precmd() |
| 624 | 70 stop = self.onecmd(line) |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
71 self.postcmd() |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
72 self.postloop() |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
73 |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
74 def precmd(self): |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
75 pass |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
76 |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
77 def postcmd(self): |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
78 pass |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
79 |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
80 def preloop(self): |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
81 pass |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
82 |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
83 def postloop(self): |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
84 pass |
| 624 | 85 |
| 86 def onecmd(self, line): | |
| 87 line = string.strip(line) | |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
88 if line == '?': |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
89 line = 'help' |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
90 elif line == '!': |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
91 if hasattr(self, 'do_shell'): |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
92 line = 'shell' |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
93 else: |
|
8689
de540166d732
Fix another bug in ESR's changes. In order to work properly,
Guido van Rossum <guido@python.org>
parents:
8615
diff
changeset
|
94 return self.default(line) |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
95 elif not line: |
|
8689
de540166d732
Fix another bug in ESR's changes. In order to work properly,
Guido van Rossum <guido@python.org>
parents:
8615
diff
changeset
|
96 return self.emptyline() |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
97 self.lastcmd = line |
| 624 | 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: | |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
979
diff
changeset
|
105 func = getattr(self, 'do_' + cmd) |
| 624 | 106 except AttributeError: |
| 107 return self.default(line) | |
| 108 return func(arg) | |
| 109 | |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
110 def emptyline(self): |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
111 return self.onecmd(self.lastcmd) |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
112 |
| 624 | 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: | |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
979
diff
changeset
|
120 func = getattr(self, 'help_' + arg) |
| 624 | 121 except: |
| 122 print '*** No help on', `arg` | |
| 123 return | |
| 124 func() | |
| 125 else: | |
|
8410
c9e3ce8e74b5
Shouldn't use newdir.dir(), which no longer exists!
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
126 names = dir(self.__class__) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
127 cmds_doc = [] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
128 cmds_undoc = [] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
129 help = {} |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
130 for name in names: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
131 if name[:5] == 'help_': |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
132 help[name[5:]]=1 |
| 624 | 133 for name in names: |
| 134 if name[:3] == 'do_': | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
135 cmd=name[3:] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
136 if help.has_key(cmd): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
137 cmds_doc.append(cmd) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
138 del help[cmd] |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
139 else: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
140 cmds_undoc.append(cmd) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
141 print |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
142 self.print_topics(self.doc_header, cmds_doc, 15,80) |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
143 self.print_topics(self.misc_header, help.keys(),15,80) |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
144 self.print_topics(self.undoc_header, cmds_undoc, 15,80) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
145 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
146 def print_topics(self, header, cmds, cmdlen, maxcol): |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
147 if cmds: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
148 print header; |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
149 if self.ruler: |
|
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
150 print self.ruler * len(header) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
151 (cmds_per_line,junk)=divmod(maxcol,cmdlen) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
152 col=cmds_per_line |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
153 for cmd in cmds: |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
154 if col==0: print |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
155 print (("%-"+`cmdlen`+"s") % cmd), |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
156 col = (col+1) % cmds_per_line |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
157 print "\n" |
