Mercurial > python-cmd2
annotate docs/freefeatures.rst @ 349:6116360f6e03
output redirection now wraps around precmd, postcmd
author | catherine@Drou |
---|---|
date | Tue, 16 Feb 2010 19:10:01 -0500 |
parents | 49dd1ce6cfd6 |
children | 52ab96d4f179 |
rev | line source |
---|---|
314 | 1 =================================== |
2 Features requiring no modifications | |
3 =================================== | |
4 | |
5 These features are provided "for free" to a cmd_-based application | |
6 simply by replacing ``import cmd`` with ``import cmd2 as cmd``. | |
7 | |
315 | 8 Script files |
9 ============ | |
10 | |
331 | 11 Text files can serve as scripts for your ``cmd2``-based |
12 application, with the ``load``, ``save``, and ``edit`` | |
13 commands. | |
315 | 14 |
15 .. automethod:: cmd2.Cmd.do_load | |
16 | |
325
4172feeddf76
want to incorporate run() for tests - not yet working
catherine@dellzilla
parents:
324
diff
changeset
|
17 .. automethod:: cmd2.Cmd.do_save |
4172feeddf76
want to incorporate run() for tests - not yet working
catherine@dellzilla
parents:
324
diff
changeset
|
18 |
331 | 19 .. automethod:: cmd2.Cmd.do_edit |
20 | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
21 Comments |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
22 ======== |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
23 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
24 Comments are omitted from the argument list |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
25 before it is passed to a ``do_`` method. By |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
26 default, both Python-style and C-style comments |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
27 are recognized; you may change this by overriding |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
28 ``app.commentGrammars`` with a different pyparsing_ |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
29 grammar. |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
30 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
31 Comments can be useful in :ref:`script`s. Used |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
32 in an interactive session, they may indicate |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
33 mental imbalance. |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
34 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
35 :: |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
36 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
37 def do_speak(self, arg): |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
38 self.stdout.write(arg + '\n') |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
39 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
40 :: |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
41 |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
42 (Cmd) speak it was /* not */ delicious! # Yuck! |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
43 it was delicious! |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
44 |
346 | 45 Commands at invocation |
46 ====================== | |
47 | |
48 You can send commands to your app as you invoke it by | |
49 including them as extra arguments to the program. | |
50 ``cmd2`` interprets each argument as a separate | |
51 command, so you should enclose each command in | |
52 quotation marks if it is more than a one-word command. | |
53 | |
54 :: | |
55 | |
56 cat@eee:~/proj/cmd2/example$ python example.py "say hello" "say Gracie" quit | |
57 hello | |
58 Gracie | |
59 cat@eee:~/proj/cmd2/example$ | |
60 | |
61 | |
315 | 62 Output redirection |
63 ================== | |
64 | |
324 | 65 As in a Unix shell, output of a command can be redirected: |
66 | |
67 - sent to a file with ``>``, as in ``mycommand args > filename.txt`` | |
68 - piped (``|``) as input to operating-system commands, as in | |
69 ``mycommand args | wc`` | |
70 - sent to the paste buffer, ready for the next Copy operation, by | |
71 ending with a bare ``>``, as in ``mycommand args >``.. Redirecting | |
72 to paste buffer requires software to be installed on the operating | |
73 system, pywin32_ on Windows or xclip_ on *nix. | |
74 | |
75 .. _pywin32:: http://sourceforge.net/projects/pywin32/ | |
76 .. _xclip:: http://www.cyberciti.biz/faq/xclip-linux-insert-files-command-output-intoclipboard/ | |
77 | |
315 | 78 Python |
79 ====== | |
80 | |
328 | 81 :: |
82 | |
83 The ``py`` command will run its arguments as a Python | |
84 command. Entered without arguments, it enters an | |
85 interactive Python session. That session can call | |
86 "back" to your application with ``cmd("")``. Through | |
87 ``self``, it also has access to your application | |
88 instance itself. (If that thought terrifies you, | |
89 you can set the ``locals_in_py`` parameter to ``False``. | |
90 See see :ref:`parameters`) | |
91 | |
92 :: | |
93 | |
94 (Cmd) py print("-".join("spelling")) | |
95 s-p-e-l-l-i-n-g | |
96 (Cmd) py | |
97 Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) | |
98 [GCC 4.4.1] on linux2 | |
99 Type "help", "copyright", "credits" or "license" for more information. | |
100 (CmdLineApp) | |
101 | |
102 py <command>: Executes a Python command. | |
103 py: Enters interactive Python mode. | |
104 End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, 'exit()`. | |
105 Non-python commands can be issued with `cmd("your command")`. | |
106 | |
107 >>> import os | |
108 >>> os.uname() | |
109 ('Linux', 'eee', '2.6.31-19-generic', '#56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010', 'i686') | |
110 >>> cmd("say --piglatin {os}".format(os=os.uname()[0])) | |
111 inuxLay | |
112 >>> self.prompt | |
113 '(Cmd) ' | |
114 >>> self.prompt = 'Python was here > ' | |
115 >>> quit() | |
116 Python was here > | |
117 | |
314 | 118 Searchable command history |
119 ========================== | |
120 | |
121 All cmd_-based applications have access to previous commands with | |
122 the up- and down- cursor keys. | |
123 | |
124 All cmd_-based applications on systems with the ``readline`` module | |
125 also provide `bash-like history list editing`_. | |
126 | |
127 .. _`bash-like history list editing`: http://www.talug.org/events/20030709/cmdline_history.html | |
128 | |
129 ``cmd2`` makes a third type of history access available, consisting of these commands: | |
130 | |
131 .. automethod:: cmd2.Cmd.do_history | |
132 | |
329 | 133 .. automethod:: cmd2.Cmd.do_list |
134 | |
135 .. automethod:: cmd2.Cmd.do_run | |
136 | |
137 Quitting the application | |
138 ======================== | |
139 | |
140 ``cmd2`` pre-defines a ``quit`` command for you (with | |
141 synonyms ``exit`` and simply ``q``). | |
142 It's trivial, but it's one less thing for you to remember. | |
143 | |
331 | 144 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
145 Abbreviated commands |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
146 ==================== |
331 | 147 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
148 ``cmd2`` apps will accept shortened command names |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
149 so long as there is no ambiguity. Thus, if |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
150 ``do_divide`` is defined, then ``divid``, ``div``, |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
151 or even ``d`` will suffice, so long as there are |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
152 no other commands defined beginning with *divid*, |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
153 *div*, or *d*. |
331 | 154 |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
155 This behavior can be turned off with ``app.abbrev`` (see :ref:`parameters`) |
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
156 |
332 | 157 Misc. pre-defined commands |
158 ========================== | |
159 | |
160 Several generically useful commands are defined | |
161 with automatically included ``do_`` methods. | |
162 | |
163 .. automethod:: cmd2.Cmd.do_quit | |
164 | |
165 .. automethod:: cmd2.Cmd.do_pause | |
166 | |
167 .. automethod:: cmd2.Cmd.do_shell | |
168 | |
169 ( ``!`` is a shortcut for ``shell``; thus ``!ls`` | |
170 is equivalent to ``shell ls``.) | |
171 | |
172 | |
315 | 173 Transcript-based testing |
174 ======================== | |
345
6fe1e75e3a67
transcript test wasn't running pre and post cmd hooks
catherine@Drou
parents:
332
diff
changeset
|
175 |
346 | 176 If the entire transcript (input and output) of a successful session of |
177 a ``cmd2``-based app is copied from the screen and pasted into a text | |
178 file, ``transcript.txt``, then a transcript test can be run against it:: | |
179 | |
180 python app.py --test transcript.txt | |
181 | |
182 Any deviations between the output prescribed in ``transcript.txt`` and | |
183 the actual output from a fresh run of the application will be reported | |
184 as a unit test failure. |