Mercurial > python-cmd2
annotate docs/freefeatures.rst @ 328:7b2bca3951a7
locals_in_py
author | cat@eee |
---|---|
date | Fri, 12 Feb 2010 15:35:48 -0500 |
parents | 4172feeddf76 |
children | c69ad8418d39 |
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 | |
325
4172feeddf76
want to incorporate run() for tests - not yet working
catherine@dellzilla
parents:
324
diff
changeset
|
11 Commands can be loaded and run from text files. |
315 | 12 |
13 .. automethod:: cmd2.Cmd.do_load | |
14 | |
325
4172feeddf76
want to incorporate run() for tests - not yet working
catherine@dellzilla
parents:
324
diff
changeset
|
15 .. automethod:: cmd2.Cmd.do_save |
4172feeddf76
want to incorporate run() for tests - not yet working
catherine@dellzilla
parents:
324
diff
changeset
|
16 |
315 | 17 Output redirection |
18 ================== | |
19 | |
324 | 20 As in a Unix shell, output of a command can be redirected: |
21 | |
22 - sent to a file with ``>``, as in ``mycommand args > filename.txt`` | |
23 - piped (``|``) as input to operating-system commands, as in | |
24 ``mycommand args | wc`` | |
25 - sent to the paste buffer, ready for the next Copy operation, by | |
26 ending with a bare ``>``, as in ``mycommand args >``.. Redirecting | |
27 to paste buffer requires software to be installed on the operating | |
28 system, pywin32_ on Windows or xclip_ on *nix. | |
29 | |
30 .. _pywin32:: http://sourceforge.net/projects/pywin32/ | |
31 .. _xclip:: http://www.cyberciti.biz/faq/xclip-linux-insert-files-command-output-intoclipboard/ | |
32 | |
33 | |
325
4172feeddf76
want to incorporate run() for tests - not yet working
catherine@dellzilla
parents:
324
diff
changeset
|
34 Commands at invocation |
4172feeddf76
want to incorporate run() for tests - not yet working
catherine@dellzilla
parents:
324
diff
changeset
|
35 ====================== |
324 | 36 |
328 | 37 You can send commands to your app as you invoke it by |
38 including them as extra arguments to the program. | |
39 ``cmd2`` interprets each argument as a separate | |
40 command, so you should enclose each command in | |
41 quotation marks if it is more than a one-word command. | |
42 | |
43 :: | |
44 | |
45 cat@eee:~/proj/cmd2/example$ python example.py "say hello" "say Gracie" quit | |
46 hello | |
47 Gracie | |
48 cat@eee:~/proj/cmd2/example$ | |
315 | 49 |
50 Python | |
51 ====== | |
52 | |
328 | 53 :: |
54 | |
55 The ``py`` command will run its arguments as a Python | |
56 command. Entered without arguments, it enters an | |
57 interactive Python session. That session can call | |
58 "back" to your application with ``cmd("")``. Through | |
59 ``self``, it also has access to your application | |
60 instance itself. (If that thought terrifies you, | |
61 you can set the ``locals_in_py`` parameter to ``False``. | |
62 See see :ref:`parameters`) | |
63 | |
64 :: | |
65 | |
66 (Cmd) py print("-".join("spelling")) | |
67 s-p-e-l-l-i-n-g | |
68 (Cmd) py | |
69 Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15) | |
70 [GCC 4.4.1] on linux2 | |
71 Type "help", "copyright", "credits" or "license" for more information. | |
72 (CmdLineApp) | |
73 | |
74 py <command>: Executes a Python command. | |
75 py: Enters interactive Python mode. | |
76 End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, 'exit()`. | |
77 Non-python commands can be issued with `cmd("your command")`. | |
78 | |
79 >>> import os | |
80 >>> os.uname() | |
81 ('Linux', 'eee', '2.6.31-19-generic', '#56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010', 'i686') | |
82 >>> cmd("say --piglatin {os}".format(os=os.uname()[0])) | |
83 inuxLay | |
84 >>> self.prompt | |
85 '(Cmd) ' | |
86 >>> self.prompt = 'Python was here > ' | |
87 >>> quit() | |
88 Python was here > | |
89 | |
314 | 90 Searchable command history |
91 ========================== | |
92 | |
93 All cmd_-based applications have access to previous commands with | |
94 the up- and down- cursor keys. | |
95 | |
96 All cmd_-based applications on systems with the ``readline`` module | |
97 also provide `bash-like history list editing`_. | |
98 | |
99 .. _`bash-like history list editing`: http://www.talug.org/events/20030709/cmdline_history.html | |
100 | |
101 ``cmd2`` makes a third type of history access available, consisting of these commands: | |
102 | |
103 .. automethod:: cmd2.Cmd.do_history | |
104 | |
315 | 105 Transcript-based testing |
106 ======================== |