annotate Lib/cmd.py @ 22360:c8a6ea00a9e1 legacy-trunk

restructure a bit to not rely on test case execution ordering add test case for bug #482460
author Skip Montanaro <skip@pobox.com>
date Sun, 17 Mar 2002 23:03:42 +0000
parents 600a9a2b7a18
children 9198fb0435fb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
1 """A generic class to build line-oriented command interpreters.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
3 Interpreters constructed with this class obey the following conventions:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
4
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
5 1. End of file on input is processed as the command 'EOF'.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
6 2. A command is parsed out of each line by collecting the prefix composed
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
7 of characters in the identchars member.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
8 3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
9 is passed a single argument consisting of the remainder of the line.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
10 4. Typing an empty line repeats the last command. (Actually, it calls the
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
11 method `emptyline', which may be overridden in a subclass.)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
12 5. There is a predefined `help' method. Given an argument `topic', it
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
13 calls the command `help_topic'. With no arguments, it lists all topics
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
14 with defined help_ functions, broken into up to three topics; documented
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
15 commands, miscellaneous help topics, and undocumented commands.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
16 6. The command '?' is a synonym for `help'. The command '!' is a synonym
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
17 for `shell', if a do_shell method exists.
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
18 7. If completion is enabled, completing commands will be done automatically,
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
19 and completing of commands args is done by calling complete_foo() with
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
20 arguments text, line, begidx, endidx. text is string we are matching
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
21 against, all returned matches must begin with it. line is the current
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
22 input line (lstripped), begidx and endidx are the beginning and end
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18855
diff changeset
23 indexes of the text being matched, which could be used to provide
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
24 different completion depending upon which position the argument is in.
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
25
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
26 The `default' method may be overridden to intercept commands for which there
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
27 is no do_ method.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
28
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
29 The `completedefault' method may be overridden to intercept completions for
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18855
diff changeset
30 commands that have no complete_ method.
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
31
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
32 The data member `self.ruler' sets the character used to draw separator lines
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
33 in the help messages. If empty, no ruler line is drawn. It defaults to "=".
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
34
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
35 If the value of `self.intro' is nonempty when the cmdloop method is called,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
36 it is printed out on interpreter startup. This value may be overridden
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
37 via an optional argument to the cmdloop() method.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
38
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
39 The data members `self.doc_header', `self.misc_header', and
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
40 `self.undoc_header' set the headers used for the help function's
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
41 listings of documented functions, miscellaneous topics, and undocumented
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
42 functions respectively.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
43
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
44 These interpreters use raw_input; thus, if the readline module is loaded,
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
45 they automatically support Emacs-like command history and editing features.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
46 """
624
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
17155
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
48 import string, sys
624
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
50 __all__ = ["Cmd"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15543
diff changeset
51
624
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 PROMPT = '(Cmd) '
18781
01ae962df885 Use string.ascii_letters instead of string.letters (SF bug #226706).
Fred Drake <fdrake@acm.org>
parents: 17155
diff changeset
53 IDENTCHARS = string.ascii_letters + string.digits + '_'
624
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 class Cmd:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
56 prompt = PROMPT
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
57 identchars = IDENTCHARS
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
58 ruler = '='
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
59 lastcmd = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
60 cmdqueue = []
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
61 intro = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
62 doc_leader = ""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
63 doc_header = "Documented commands (type help <topic>):"
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
64 misc_header = "Miscellaneous help topics:"
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
65 undoc_header = "Undocumented commands:"
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
66 nohelp = "*** No help on %s"
17155
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
67 use_rawinput = 1
1315
acdc0b9a6c78 * aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents: 1086
diff changeset
68
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18855
diff changeset
69 def __init__(self, completekey='tab'):
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
70 if completekey:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
71 try:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
72 import readline
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
73 readline.set_completer(self.complete)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
74 readline.parse_and_bind(completekey+": complete")
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
75 except ImportError:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
76 pass
8615
e7822b64a33d Gotta have an __init__() method -- pdb.py calls it!
Guido van Rossum <guido@python.org>
parents: 8584
diff changeset
77
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
78 def cmdloop(self, intro=None):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
79 self.preloop()
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 13533
diff changeset
80 if intro is not None:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
81 self.intro = intro
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
82 if self.intro:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
83 print self.intro
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
84 stop = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
85 while not stop:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
86 if self.cmdqueue:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
87 line = self.cmdqueue[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
88 del self.cmdqueue[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
89 else:
17155
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
90 if self.use_rawinput:
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
91 try:
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
92 line = raw_input(self.prompt)
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
93 except EOFError:
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
94 line = 'EOF'
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
95 else:
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
96 sys.stdout.write(self.prompt)
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
97 line = sys.stdin.readline()
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
98 if not len(line):
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
99 line = 'EOF'
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
100 else:
1550872249e9 This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents: 16478
diff changeset
101 line = line[:-1] # chop \n
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
102 line = self.precmd(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
103 stop = self.onecmd(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
104 stop = self.postcmd(stop, line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
105 self.postloop()
8584
cb7fcfb56275 Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents: 8410
diff changeset
106
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
107 def precmd(self, line):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
108 return line
8584
cb7fcfb56275 Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents: 8410
diff changeset
109
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
110 def postcmd(self, stop, line):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
111 return stop
8584
cb7fcfb56275 Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents: 8410
diff changeset
112
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
113 def preloop(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
114 pass
8584
cb7fcfb56275 Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents: 8410
diff changeset
115
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
116 def postloop(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
117 pass
624
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
119 def parseline(self, line):
16478
edc0c99a69d6 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16280
diff changeset
120 line = line.strip()
13533
1e7c32bddb8f Bug fix: ? and ! were not full aliases for `help' and `shell' as implied in
Eric S. Raymond <esr@thyrsus.com>
parents: 11691
diff changeset
121 if not line:
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
122 return None, None, line
13533
1e7c32bddb8f Bug fix: ? and ! were not full aliases for `help' and `shell' as implied in
Eric S. Raymond <esr@thyrsus.com>
parents: 11691
diff changeset
123 elif line[0] == '?':
1e7c32bddb8f Bug fix: ? and ! were not full aliases for `help' and `shell' as implied in
Eric S. Raymond <esr@thyrsus.com>
parents: 11691
diff changeset
124 line = 'help ' + line[1:]
1e7c32bddb8f Bug fix: ? and ! were not full aliases for `help' and `shell' as implied in
Eric S. Raymond <esr@thyrsus.com>
parents: 11691
diff changeset
125 elif line[0] == '!':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
126 if hasattr(self, 'do_shell'):
13533
1e7c32bddb8f Bug fix: ? and ! were not full aliases for `help' and `shell' as implied in
Eric S. Raymond <esr@thyrsus.com>
parents: 11691
diff changeset
127 line = 'shell ' + line[1:]
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
128 else:
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
129 return None, None, line
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
130 i, n = 0, len(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
131 while i < n and line[i] in self.identchars: i = i+1
16478
edc0c99a69d6 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16280
diff changeset
132 cmd, arg = line[:i], line[i:].strip()
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
133 return cmd, arg, line
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18855
diff changeset
134
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
135 def onecmd(self, line):
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
136 cmd, arg, line = self.parseline(line)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
137 if not line:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
138 return self.emptyline()
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
139 if cmd is None:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
140 return self.default(line)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
141 self.lastcmd = line
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
142 if cmd == '':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
143 return self.default(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
144 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
145 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
146 func = getattr(self, 'do_' + cmd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
147 except AttributeError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
148 return self.default(line)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
149 return func(arg)
624
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
151 def emptyline(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
152 if self.lastcmd:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
153 return self.onecmd(self.lastcmd)
8584
cb7fcfb56275 Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents: 8410
diff changeset
154
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
155 def default(self, line):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
156 print '*** Unknown syntax:', line
624
0e436200bf12 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
158 def completedefault(self, *ignored):
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
159 return []
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
160
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
161 def completenames(self, text, *ignored):
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
162 dotext = 'do_'+text
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
163 return [a[3:] for a in self.get_names() if a.startswith(dotext)]
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
164
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
165 def complete(self, text, state):
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
166 """Return the next possible completion for 'text'.
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
167
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
168 If a command has not been entered, then complete against command list.
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
169 Otherwise try to call complete_<command> to get list of completions.
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
170 """
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
171 if state == 0:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
172 import readline
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
173 origline = readline.get_line_buffer()
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
174 line = origline.lstrip()
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
175 stripped = len(origline) - len(line)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
176 begidx = readline.get_begidx() - stripped
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
177 endidx = readline.get_endidx() - stripped
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
178 if begidx>0:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
179 cmd, args, foo = self.parseline(line)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
180 if cmd == '':
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
181 compfunc = self.completedefault
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
182 else:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
183 try:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
184 compfunc = getattr(self, 'complete_' + cmd)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
185 except AttributeError:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
186 compfunc = self.completedefault
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
187 else:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
188 compfunc = self.completenames
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
189 self.completion_matches = compfunc(text, line, begidx, endidx)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
190 try:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
191 return self.completion_matches[state]
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
192 except IndexError:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
193 return None
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18855
diff changeset
194
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
195 def get_names(self):
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
196 # Inheritance says we have to look in class and
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
197 # base classes; order is not important.
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
198 names = []
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
199 classes = [self.__class__]
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
200 while classes:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
201 aclass = classes[0]
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
202 if aclass.__bases__:
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
203 classes = classes + list(aclass.__bases__)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
204 names = names + dir(aclass)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
205 del classes[0]
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
206 return names
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
207
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
208 def complete_help(self, *args):
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
209 return self.completenames(*args)
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
210
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
211 def do_help(self, arg):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
212 if arg:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
213 # XXX check arg syntax
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
214 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
215 func = getattr(self, 'help_' + arg)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
216 except:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
217 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
218 doc=getattr(self, 'do_' + arg).__doc__
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
219 if doc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
220 print doc
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
221 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
222 except:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
223 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
224 print self.nohelp % (arg,)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
225 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
226 func()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
227 else:
18855
6f301b3cf323 Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents: 18781
diff changeset
228 names = self.get_names()
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
229 cmds_doc = []
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
230 cmds_undoc = []
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
231 help = {}
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
232 for name in names:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
233 if name[:5] == 'help_':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
234 help[name[5:]]=1
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
235 names.sort()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
236 # There can be duplicates if routines overridden
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
237 prevname = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
238 for name in names:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
239 if name[:3] == 'do_':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
240 if name == prevname:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
241 continue
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
242 prevname = name
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
243 cmd=name[3:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
244 if help.has_key(cmd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
245 cmds_doc.append(cmd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
246 del help[cmd]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
247 elif getattr(self, name).__doc__:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
248 cmds_doc.append(cmd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
249 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
250 cmds_undoc.append(cmd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
251 print self.doc_leader
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
252 self.print_topics(self.doc_header, cmds_doc, 15,80)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
253 self.print_topics(self.misc_header, help.keys(),15,80)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
254 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
255
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
256 def print_topics(self, header, cmds, cmdlen, maxcol):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
257 if cmds:
16280
8b81a7e58a1d unnecessary semicolon
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16070
diff changeset
258 print header
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
259 if self.ruler:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
260 print self.ruler * len(header)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
261 (cmds_per_line,junk)=divmod(maxcol,cmdlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
262 col=cmds_per_line
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
263 for cmd in cmds:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
264 if col==0: print
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
265 print (("%-"+`cmdlen`+"s") % cmd),
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
266 col = (col+1) % cmds_per_line
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10820
diff changeset
267 print "\n"