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