Skip to content

Commit d2d3c2f

Browse files
committed
Updated some documentation.
1 parent 97668f9 commit d2d3c2f

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

cmd2/cmd2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,7 @@ def do_py(self, arg):
28712871
py <command>: Executes a Python command.
28722872
py: Enters interactive Python mode.
28732873
End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``.
2874-
Non-python commands can be issued with ``cmd("your command")``.
2874+
Non-python commands can be issued with ``pyscript_name("your command")``.
28752875
Run python code from external script files with ``run("script.py")``
28762876
"""
28772877
from .pyscript_bridge import PyscriptBridge
@@ -2909,7 +2909,6 @@ def onecmd_plus_hooks(cmd_plus_args):
29092909

29102910
if self.locals_in_py:
29112911
self.pystate['self'] = self
2912-
self.pystate['cmd'] = onecmd_plus_hooks
29132912

29142913
localvars = self.pystate
29152914
interp = InteractiveConsole(locals=localvars)
@@ -2932,9 +2931,10 @@ def quit():
29322931
keepstate = Statekeeper(sys, ('stdin', 'stdout'))
29332932
sys.stdout = self.stdout
29342933
sys.stdin = self.stdin
2934+
docstr = self.do_py.__doc__.replace('pyscript_name', self.pyscript_name)
29352935
interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" %
29362936
(sys.version, sys.platform, cprt, self.__class__.__name__,
2937-
self.do_py.__doc__))
2937+
docstr))
29382938
except EmbeddedConsoleExit:
29392939
pass
29402940
if keepstate is not None:

docs/freefeatures.rst

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,68 @@ app's value of ``self.redirector`` to use a different string for output redirect
146146
Python
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) '

docs/settingchanges.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ with::
143143
echo: False # Echo command issued into output
144144
editor: vim # Program used by ``edit``
145145
feedback_to_output: False # include nonessentials in `|`, `>` results
146-
locals_in_py: True # Allow access to your application in py via self
146+
locals_in_py: False # Allow access to your application in py via self
147147
prompt: (Cmd) # The prompt issued to solicit input
148148
quiet: False # Don't print nonessential feedback
149149
timing: False # Report execution times

0 commit comments

Comments
 (0)