annotate docs/freefeatures.rst @ 331:6306edc46a6e

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