@@ -146,35 +146,68 @@ app's value of ``self.redirector`` to use a different string for output redirect
146146Python
147147======
148148
149- The ``py `` command will run its arguments as a Python
150- command. Entered without arguments, it enters an
151- interactive Python session. That session can call
152- "back" to your application with ``cmd("") ``. Through
153- ``self ``, it also has access to your application
154- instance itself which can be extremely useful for debugging.
155- (If giving end-users this level of introspection is inappropriate,
156- the ``locals_in_py `` parameter can be set to ``False `` and removed
157- from the settable dictionary. See see :ref: `parameters `)
149+ The ``py `` command will run its arguments as a Python command. Entered without
150+ arguments, it enters an interactive Python session. The session can call "back"
151+ to your application through the name defined in ``self.pyscript_name `` (defaults
152+ to ``app ``). This wrapper provides access to execute commands in your cmd2
153+ application while maintaining isolation.
154+
155+ You may optionally enable full access to to your application by setting
156+ ``locals_in_py `` to ``True ``. Enabling this flag adds ``self `` to the python
157+ session, which is a reference to your Cmd2 application. This can be useful for
158+ debugging your application. To prevent users from enabling this ability
159+ manually you'll need to remove ``locals_in_py `` from the ``settable `` dictionary.
160+ That session can call
161+
162+ The ``app `` object (or your custom name) provides access to application commands
163+ through either raw commands or through a python API wrapper. For example, any
164+ application command call be called with ``app("<command>") ``. All application
165+ commands are accessible as python objects and functions matching the command
166+ name. For example, the following are equivalent:
167+
168+ ::
169+
170+ >>> app('say --piglatin Blah')
171+ lahBay
172+ >>> app.say("Blah", piglatin=True)
173+ lahBay
174+
175+
176+ Sub-commands are also supported. The following pairs are equivalent:
177+
178+ ::
179+
180+ >>> app('command subcmd1 subcmd2 param1 --myflag --otherflag 3')
181+ >>> app.command.subcmd1.subcmd2('param1', myflag=True, otherflag=3)
182+
183+ >>> app('command subcmd1 param1 subcmd2 param2 --myflag --otherflag 3')
184+ >>> app.command.subcmd1('param1').subcmd2('param2', myflag=True, otherflag=3)
185+
186+
187+ More Python examples:
158188
159189::
160190
161191 (Cmd) py print("-".join("spelling"))
162192 s-p-e-l-l-i-n-g
163193 (Cmd) py
164- Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15 )
165- [GCC 4.4.1 ] on linux2
194+ Python 3.5.3 (default, Jan 19 2017, 14:11:04 )
195+ [GCC 6.3.0 20170118 ] on linux
166196 Type "help", "copyright", "credits" or "license" for more information.
167197 (CmdLineApp)
168198
199+ Invoke python command, shell, or script
200+
169201 py <command>: Executes a Python command.
170202 py: Enters interactive Python mode.
171- End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, 'exit()`.
172- Non-python commands can be issued with `cmd("your command")`.
203+ End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``.
204+ Non-python commands can be issued with ``app("your command")``.
205+ Run python code from external script files with ``run("script.py")``
173206
174207 >>> import os
175208 >>> os.uname()
176209 ('Linux', 'eee', '2.6.31-19-generic', '#56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010', 'i686')
177- >>> cmd ("say --piglatin {os}".format(os=os.uname()[0]))
210+ >>> app ("say --piglatin {os}".format(os=os.uname()[0]))
178211 inuxLay
179212 >>> self.prompt
180213 '(Cmd) '
0 commit comments