11Embedded Python Shells
22======================
33
4- The ``py `` command will run its arguments as a Python command. Entered without
5- arguments, it enters an interactive Python session. The session can call
6- "back" to your application through the name defined in ``self.pyscript_name ``
7- (defaults to ``app ``). This wrapper provides access to execute commands in
8- your ``cmd2 `` application while maintaining isolation.
4+ Python (optional)
5+ ------------------
6+ If the ``cmd2.Cmd `` class is instantiated with ``include_py=True ``, then the
7+ optional ``py `` command will be present and run an interactive Python shell::
8+
9+ from cmd2 import Cmd
10+ class App(Cmd):
11+ def __init__(self):
12+ Cmd.__init__(self, include_py=True)
13+
14+ The Python shell can run CLI commands from you application using the object
15+ named in ``self.pyscript_name `` (defaults to ``app ``). This wrapper provides
16+ access to execute commands in your ``cmd2 `` application while maintaining
17+ isolation from the full `Cmd ` instance. For example, any application command
18+ can be run with ``app("command ...") ``.
919
1020You may optionally enable full access to to your application by setting
1121``self.self_in_py `` to ``True ``. Enabling this flag adds ``self `` to the
@@ -17,77 +27,29 @@ in the CLI's environment.
1727
1828Anything in ``self.py_locals `` is always available in the Python environment.
1929
20- The ``app `` object (or your custom name) provides access to application
21- commands through raw commands. For example, any application command call be
22- called with ``app("<command>") ``.
23-
24- More Python examples:
25-
26- ::
27-
28- (Cmd) py print("-".join("spelling"))
29- s-p-e-l-l-i-n-g
30- (Cmd) py
31- Python 3.9.0 (default, Nov 11 2020, 21:21:51)
32- [Clang 12.0.0 (clang-1200.0.32.21)] on darwin
33- Type "help", "copyright", "credits" or "license" for more information.
34-
35- End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, `exit()`.
36- Non-Python commands can be issued with: app("your command")
37- (CmdLineApp)
38-
39- End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, `exit()`.
40- Non-python commands can be issued with: app("your command")
41- Run python code from external script files with: run("script.py")
42-
43- >>> import os
44- >>> os.uname()
45- ('Linux', 'eee', '2.6.31-19-generic', '#56-Ubuntu SMP Thu Jan 28 01:26:53 UTC 2010', 'i686')
46- >>> app("say --piglatin {os}".format(os=os.uname()[0]))
47- inuxLay
48- >>> self.prompt
49- '(Cmd) '
50- >>> self.prompt = 'Python was here > '
51- >>> quit()
52- Python was here >
53-
54- The ``py `` command also allows you to run Python scripts via ``py
55- run('myscript.py') ``. This provides a more complicated and more powerful
56- scripting capability than that provided by the simple text file scripts
57- discussed in :ref: `features/scripting:Scripting `. Python scripts can include
30+ All of these parameters are also available to Python scripts which run in your
31+ application via the ``run_pyscript `` command:
32+
33+ - supports tab completion of file system paths
34+ - has the ability to pass command-line arguments to the scripts invoked
35+
36+ This command provides a more complicated and more powerful scripting capability
37+ than that provided by the simple text file scripts. Python scripts can include
5838conditional control flow logic. See the **python_scripting.py ** ``cmd2 ``
5939application and the **script_conditional.py ** script in the ``examples `` source
6040code directory for an example of how to achieve this in your own applications.
41+ See :ref: `features/scripting:Scripting ` for an explanation of both scripting
42+ methods in **cmd2 ** applications.
6143
62- Using ``py `` to run scripts directly is considered deprecated. The newer
63- ``run_pyscript `` command is superior for doing this in two primary ways:
64-
65- - it supports tab completion of file system paths
66- - it has the ability to pass command-line arguments to the scripts invoked
67-
68- There are no disadvantages to using ``run_pyscript `` as opposed to ``py
69- run() ``. A simple example of using ``run_pyscript `` is shown below along with
70- the arg_printer _ script::
44+ A simple example of using ``run_pyscript `` is shown below along with the
45+ arg_printer _ script::
7146
7247 (Cmd) run_pyscript examples/scripts/arg_printer.py foo bar baz
7348 Running Python script 'arg_printer.py' which was called with 3 arguments
7449 arg 1: 'foo'
7550 arg 2: 'bar'
7651 arg 3: 'baz'
7752
78- .. note ::
79-
80- If you want to be able to pass arguments with spaces to commands, then we
81- strongly recommend using one of the decorators, such as
82- ``with_argument_list ``. ``cmd2 `` will pass your **do_* ** methods a list of
83- arguments in this case.
84-
85- When using this decorator, you can then put arguments in quotes like so::
86-
87- $ examples/arg_print.py
88- (Cmd) lprint foo "bar baz"
89- lprint was called with the following list of arguments: ['foo', 'bar baz']
90-
9153.. _arg_printer :
9254 https://github.com/python-cmd2/cmd2/blob/master/examples/scripts/arg_printer.py
9355
@@ -96,13 +58,13 @@ IPython (optional)
9658------------------
9759
9860**If ** IPython _ is installed on the system **and ** the ``cmd2.Cmd `` class is
99- instantiated with ``use_ipython =True ``, then the optional ``ipy `` command will
100- be present ::
61+ instantiated with ``include_ipy =True ``, then the optional ``ipy `` command will
62+ run an interactive IPython shell ::
10163
10264 from cmd2 import Cmd
10365 class App(Cmd):
10466 def __init__(self):
105- Cmd.__init__(self, use_ipython =True)
67+ Cmd.__init__(self, include_ipy =True)
10668
10769The ``ipy `` command enters an interactive IPython _ session. Similar to an
10870interactive Python session, this shell can access your application instance via
0 commit comments