Mercurial > cpython-lru_cache-weakref
annotate Lib/cmd.py @ 81343:3a7cd8efc19f
#16852: merge with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Thu, 10 Jan 2013 03:29:45 +0200 |
| parents | b6b4d74b8d42 |
| children | 8d28d44f3a9a |
| 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 """ |
| 624 | 44 |
|
37045
081b4f0e81b7
Get rid of a bunch more raw_input references
Neal Norwitz <nnorwitz@gmail.com>
parents:
32528
diff
changeset
|
45 import string, sys |
| 624 | 46 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15543
diff
changeset
|
47 __all__ = ["Cmd"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15543
diff
changeset
|
48 |
| 624 | 49 PROMPT = '(Cmd) ' |
|
18781
01ae962df885
Use string.ascii_letters instead of string.letters (SF bug #226706).
Fred Drake <fdrake@acm.org>
parents:
17155
diff
changeset
|
50 IDENTCHARS = string.ascii_letters + string.digits + '_' |
| 624 | 51 |
| 52 class Cmd: | |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
53 """A simple framework for writing line-oriented command interpreters. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
54 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
55 These are often useful for test harnesses, administrative tools, and |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
56 prototypes that will later be wrapped in a more sophisticated interface. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
57 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
58 A Cmd instance or subclass instance is a line-oriented interpreter |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
59 framework. There is no good reason to instantiate Cmd itself; rather, |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
60 it's useful as a superclass of an interpreter class you define yourself |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
61 in order to inherit Cmd's methods and encapsulate action methods. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
62 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
63 """ |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
64 prompt = PROMPT |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
65 identchars = IDENTCHARS |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
66 ruler = '=' |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
67 lastcmd = '' |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
68 intro = None |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
69 doc_leader = "" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
70 doc_header = "Documented commands (type help <topic>):" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
71 misc_header = "Miscellaneous help topics:" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
72 undoc_header = "Undocumented commands:" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
73 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
|
74 use_rawinput = 1 |
|
1315
acdc0b9a6c78
* aifc.py: don't die on invalid MARK chunk
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
75 |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
76 def __init__(self, completekey='tab', stdin=None, stdout=None): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
77 """Instantiate a line-oriented interpreter framework. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
78 |
|
27963
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27713
diff
changeset
|
79 The optional argument 'completekey' is the readline name of a |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27713
diff
changeset
|
80 completion key; it defaults to the Tab key. If completekey is |
|
226463445697
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27713
diff
changeset
|
81 not None and the readline module is available, command completion |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
82 is done automatically. The optional arguments stdin and stdout |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
83 specify alternate input and output file objects; if not specified, |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
84 sys.stdin and sys.stdout are used. |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
85 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
86 """ |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
87 if stdin is not None: |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
88 self.stdin = stdin |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
89 else: |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
90 self.stdin = sys.stdin |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
91 if stdout is not None: |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
92 self.stdout = stdout |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
93 else: |
|
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
94 self.stdout = sys.stdout |
|
27238
3bfdbc3dfbce
Duh. cmdqueue should be an instance variable, not a class variable.
Guido van Rossum <guido@python.org>
parents:
26494
diff
changeset
|
95 self.cmdqueue = [] |
| 27627 | 96 self.completekey = completekey |
|
8615
e7822b64a33d
Gotta have an __init__() method -- pdb.py calls it!
Guido van Rossum <guido@python.org>
parents:
8584
diff
changeset
|
97 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
98 def cmdloop(self, intro=None): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
99 """Repeatedly issue a prompt, accept input, parse an initial prefix |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
100 off the received input, and dispatch to action methods, passing them |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
101 the remainder of the line as argument. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
102 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
103 """ |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
104 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
105 self.preloop() |
| 32457 | 106 if self.use_rawinput and self.completekey: |
| 107 try: | |
| 108 import readline | |
| 109 self.old_completer = readline.get_completer() | |
| 110 readline.set_completer(self.complete) | |
| 111 readline.parse_and_bind(self.completekey+": complete") | |
| 112 except ImportError: | |
| 113 pass | |
| 114 try: | |
| 115 if intro is not None: | |
| 116 self.intro = intro | |
| 117 if self.intro: | |
| 118 self.stdout.write(str(self.intro)+"\n") | |
| 119 stop = None | |
| 120 while not stop: | |
| 121 if self.cmdqueue: | |
| 122 line = self.cmdqueue.pop(0) | |
|
17155
1550872249e9
This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents:
16478
diff
changeset
|
123 else: |
| 32457 | 124 if self.use_rawinput: |
| 125 try: | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
37045
diff
changeset
|
126 line = input(self.prompt) |
| 32457 | 127 except EOFError: |
| 128 line = 'EOF' | |
|
17155
1550872249e9
This is SF patch #405952, by Anthony Baxter:
Guido van Rossum <guido@python.org>
parents:
16478
diff
changeset
|
129 else: |
| 32457 | 130 self.stdout.write(self.prompt) |
| 131 self.stdout.flush() | |
| 132 line = self.stdin.readline() | |
| 133 if not len(line): | |
| 134 line = 'EOF' | |
| 135 else: | |
|
63399
edda7596c831
Merged revisions 83380 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
43577
diff
changeset
|
136 line = line.rstrip('\r\n') |
| 32457 | 137 line = self.precmd(line) |
| 138 stop = self.onecmd(line) | |
| 139 stop = self.postcmd(stop, line) | |
| 140 self.postloop() | |
| 141 finally: | |
| 142 if self.use_rawinput and self.completekey: | |
| 143 try: | |
| 144 import readline | |
| 145 readline.set_completer(self.old_completer) | |
| 146 except ImportError: | |
| 147 pass | |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32457
diff
changeset
|
148 |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
149 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
150 def precmd(self, line): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
151 """Hook method executed just before the command line is |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
152 interpreted, but after the input prompt is generated and issued. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
153 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
154 """ |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
155 return line |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
156 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
157 def postcmd(self, stop, line): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
158 """Hook method executed just after a command dispatch is finished.""" |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
159 return stop |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
160 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
161 def preloop(self): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
162 """Hook method executed once when the cmdloop() method is called.""" |
| 32457 | 163 pass |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
164 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
165 def postloop(self): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
166 """Hook method executed once when the cmdloop() method is about to |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
167 return. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
168 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
169 """ |
| 32457 | 170 pass |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32457
diff
changeset
|
171 |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
172 def parseline(self, line): |
| 30755 | 173 """Parse the line into a command name and a string containing |
| 174 the arguments. Returns a tuple containing (command, args, line). | |
| 175 'command' and 'args' may be None if the line couldn't be parsed. | |
| 176 """ | |
|
16478
edc0c99a69d6
String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents:
16280
diff
changeset
|
177 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 elif line[0] == '!': |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
183 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
|
184 line = 'shell ' + line[1:] |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
185 else: |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
186 return None, None, line |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
187 i, n = 0, len(line) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
188 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
|
189 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
|
190 return cmd, arg, line |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18855
diff
changeset
|
191 |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
192 def onecmd(self, line): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
193 """Interpret the argument as though it had been typed in response |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
194 to the prompt. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
195 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
196 This may be overridden, but should not normally need to be; |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
197 see the precmd() and postcmd() methods for useful execution hooks. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
198 The return value is a flag indicating whether interpretation of |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
199 commands by the interpreter should stop. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
200 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
201 """ |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
202 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
|
203 if not line: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
204 return self.emptyline() |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
205 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
|
206 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
|
207 self.lastcmd = line |
|
73873
b6b4d74b8d42
Close #13500: Hitting EOF gets cmd.py into a infinite EOF on return loop
Jesus Cea <jcea@jcea.es>
parents:
68257
diff
changeset
|
208 if line == 'EOF' : |
|
b6b4d74b8d42
Close #13500: Hitting EOF gets cmd.py into a infinite EOF on return loop
Jesus Cea <jcea@jcea.es>
parents:
68257
diff
changeset
|
209 self.lastcmd = '' |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
210 if cmd == '': |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
211 return self.default(line) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
212 else: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
213 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
214 func = getattr(self, 'do_' + cmd) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
215 except AttributeError: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
216 return self.default(line) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
217 return func(arg) |
| 624 | 218 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
219 def emptyline(self): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
220 """Called when an empty line is entered in response to the prompt. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
221 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
222 If this method is not overridden, it repeats the last nonempty |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
223 command entered. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
224 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
225 """ |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
226 if self.lastcmd: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
227 return self.onecmd(self.lastcmd) |
|
8584
cb7fcfb56275
Improved by Eric Raymond.
Guido van Rossum <guido@python.org>
parents:
8410
diff
changeset
|
228 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
229 def default(self, line): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
230 """Called on an input line when the command prefix is not recognized. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
231 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
232 If this method is not overridden, it prints an error message and |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
233 returns. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
234 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
235 """ |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
236 self.stdout.write('*** Unknown syntax: %s\n'%line) |
| 624 | 237 |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
238 def completedefault(self, *ignored): |
|
23565
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
239 """Method called to complete an input line when no command-specific |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
240 complete_*() method is available. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
241 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
242 By default, it returns an empty list. |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
243 |
|
1a67fbc7a066
Patch 560023 adding docstrings. 2.2 Candidate (after verifying modules were not updated after 2.2).
Raymond Hettinger <python@rcn.com>
parents:
22493
diff
changeset
|
244 """ |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
245 return [] |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
246 |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
247 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
|
248 dotext = 'do_'+text |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
249 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
|
250 |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
251 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
|
252 """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
|
253 |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
254 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
|
255 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
|
256 """ |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
257 if state == 0: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
258 import readline |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
259 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
|
260 line = origline.lstrip() |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
261 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
|
262 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
|
263 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
|
264 if begidx>0: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
265 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
|
266 if cmd == '': |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
267 compfunc = self.completedefault |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
268 else: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
269 try: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
270 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
|
271 except AttributeError: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
272 compfunc = self.completedefault |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
273 else: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
274 compfunc = self.completenames |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
275 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
|
276 try: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
277 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
|
278 except IndexError: |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
279 return None |
|
19119
600a9a2b7a18
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
18855
diff
changeset
|
280 |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
281 def get_names(self): |
|
58222
f2d54096dc8c
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
282 # This method used to pull in base class attributes |
|
f2d54096dc8c
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
283 # at a time dir() didn't do it yet. |
|
f2d54096dc8c
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
284 return dir(self.__class__) |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
285 |
|
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
286 def complete_help(self, *args): |
|
58222
f2d54096dc8c
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
287 commands = set(self.completenames(*args)) |
|
f2d54096dc8c
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
288 topics = set(a[5:] for a in self.get_names() |
|
f2d54096dc8c
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
289 if a.startswith('help_' + args[0])) |
|
f2d54096dc8c
Merged revisions 77185-77188,77262,77313,77317,77331-77333,77337-77338 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
43577
diff
changeset
|
290 return list(commands | topics) |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
291 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
292 def do_help(self, arg): |
|
64576
f8c896ad787f
Add docstring to cmd.Cmd.do_help()
Raymond Hettinger <python@rcn.com>
parents:
63396
diff
changeset
|
293 'List available commands with "help" or detailed help with "help cmd".' |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
294 if arg: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
295 # XXX check arg syntax |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
296 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
297 func = getattr(self, 'help_' + arg) |
|
22493
66adb551cd38
tighten up except - in this context, AttributeError is the only exception
Skip Montanaro <skip@pobox.com>
parents:
22532
diff
changeset
|
298 except AttributeError: |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
299 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
300 doc=getattr(self, 'do_' + arg).__doc__ |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
301 if doc: |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
302 self.stdout.write("%s\n"%str(doc)) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
303 return |
|
22493
66adb551cd38
tighten up except - in this context, AttributeError is the only exception
Skip Montanaro <skip@pobox.com>
parents:
22532
diff
changeset
|
304 except AttributeError: |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
305 pass |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
306 self.stdout.write("%s\n"%str(self.nohelp % (arg,))) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
307 return |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
308 func() |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
309 else: |
|
18855
6f301b3cf323
Patch #416224: add readline completion to cmd.Cmd.
Martin v. Löwis <martin@v.loewis.de>
parents:
18781
diff
changeset
|
310 names = self.get_names() |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
311 cmds_doc = [] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
312 cmds_undoc = [] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
313 help = {} |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
314 for name in names: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
315 if name[:5] == 'help_': |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
316 help[name[5:]]=1 |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
317 names.sort() |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
318 # There can be duplicates if routines overridden |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
319 prevname = '' |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
320 for name in names: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
321 if name[:3] == 'do_': |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
322 if name == prevname: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
323 continue |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
324 prevname = name |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
325 cmd=name[3:] |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23565
diff
changeset
|
326 if cmd in help: |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
327 cmds_doc.append(cmd) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
328 del help[cmd] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
329 elif getattr(self, name).__doc__: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
330 cmds_doc.append(cmd) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
331 else: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
332 cmds_undoc.append(cmd) |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
333 self.stdout.write("%s\n"%str(self.doc_leader)) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
334 self.print_topics(self.doc_header, cmds_doc, 15,80) |
|
41985
ae6ba6188d38
Fix pdb help command.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
41554
diff
changeset
|
335 self.print_topics(self.misc_header, list(help.keys()),15,80) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
336 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
|
337 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
338 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
|
339 if cmds: |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
340 self.stdout.write("%s\n"%str(header)) |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10820
diff
changeset
|
341 if self.ruler: |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
342 self.stdout.write("%s\n"%str(self.ruler * len(header))) |
|
26494
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
343 self.columnize(cmds, maxcol-1) |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
344 self.stdout.write("\n") |
|
26494
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
345 |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
346 def columnize(self, list, displaywidth=80): |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
347 """Display a list of strings as a compact set of columns. |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
348 |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
349 Each column is only as wide as necessary. |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
350 Columns are separated by two spaces (one was not legible enough). |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
351 """ |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
352 if not list: |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
353 self.stdout.write("<empty>\n") |
|
26494
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
354 return |
|
41985
ae6ba6188d38
Fix pdb help command.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
41554
diff
changeset
|
355 |
|
26494
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
356 nonstrings = [i for i in range(len(list)) |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
43020
diff
changeset
|
357 if not isinstance(list[i], str)] |
|
26494
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
358 if nonstrings: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
41985
diff
changeset
|
359 raise TypeError("list[i] not a string for i in %s" |
|
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
41985
diff
changeset
|
360 % ", ".join(map(str, nonstrings))) |
|
26494
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
361 size = len(list) |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
362 if size == 1: |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
363 self.stdout.write('%s\n'%str(list[0])) |
|
26494
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
364 return |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
365 # Try every row count from 1 upwards |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
366 for nrows in range(1, len(list)): |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
367 ncols = (size+nrows-1) // nrows |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
368 colwidths = [] |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
369 totwidth = -2 |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
370 for col in range(ncols): |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
371 colwidth = 0 |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
372 for row in range(nrows): |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
373 i = row + nrows*col |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
374 if i >= size: |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
375 break |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
376 x = list[i] |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
377 colwidth = max(colwidth, len(x)) |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
378 colwidths.append(colwidth) |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
379 totwidth += colwidth + 2 |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
380 if totwidth > displaywidth: |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
381 break |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
382 if totwidth <= displaywidth: |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
383 break |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
384 else: |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
385 nrows = len(list) |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
386 ncols = 1 |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
387 colwidths = [0] |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
388 for row in range(nrows): |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
389 texts = [] |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
390 for col in range(ncols): |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
391 i = row + nrows*col |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
392 if i >= size: |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
393 x = "" |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
394 else: |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
395 x = list[i] |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
396 texts.append(x) |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
397 while texts and not texts[-1]: |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
398 del texts[-1] |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
399 for col in range(len(texts)): |
|
3f8019273549
Add a better columnizer to print_topics().
Guido van Rossum <guido@python.org>
parents:
24058
diff
changeset
|
400 texts[col] = texts[col].ljust(colwidths[col]) |
|
27713
aade4bc125f8
Updated version of [ 558544 ] cmd.py: add instance-specific stdin/out
Anthony Baxter <anthonybaxter@gmail.com>
parents:
27627
diff
changeset
|
401 self.stdout.write("%s\n"%str(" ".join(texts))) |
