Skip to content

Commit cc38f5d

Browse files
authored
Merge pull request #904 from python-cmd2/scripting_docs
Update scripting docs. Closes #765.
2 parents 94f3c4c + f230204 commit cc38f5d

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

docs/api/cmd.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ cmd2.Cmd
6363

6464
Set an introduction message which is displayed to the user before
6565
the :ref:`features/hooks:Command Processing Loop` begins.
66+
67+
.. attribute:: py_bridge_name
68+
69+
The symbol name which :ref:`features/scripting:Python Scripts` run
70+
using the :ref:`features/builtin_commands:run_pyscript` command can use
71+
to reference the parent ``cmd2`` application.

docs/features/builtin_commands.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ This command runs commands in a script file that is encoded as either ASCII
8383
or UTF-8 text. See :ref:`features/scripting:Command Scripts` for more
8484
information.
8585

86+
_relative_run_script
87+
~~~~~~~~~~~~~~~~~~~~
88+
89+
This command is hidden from the help that's visible to end users. It runs a
90+
script like :ref:`features/builtin_commands:run_script` but does so using a
91+
path relative to the script that is currently executing. This is useful when
92+
you have scripts that run other scripts. See :ref:`features/scripting:Running
93+
Command Scripts` for more information.
94+
8695
set
8796
~~~
8897

docs/features/scripting.rst

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ Creating Command Scripts
2121
Command scripts can be created in several ways:
2222

2323
- creating a text file using any method of your choice
24-
- using the built-in ``edit`` command to create or edit an existing text file
25-
- saving previously entered commands to a script file using ``history -s``. See
26-
:ref:`features/history:History` for more details.
24+
- using the built-in :ref:`features/builtin_commands:edit` command to
25+
create or edit an existing text file
26+
- saving previously entered commands to a script file using
27+
:ref:`history -s <features/history:For Users>`
2728

2829
If you create create a text file from scratch, just include one command per
2930
line, exactly as you would type it inside a ``cmd2`` application.
@@ -32,11 +33,15 @@ line, exactly as you would type it inside a ``cmd2`` application.
3233
Running Command Scripts
3334
~~~~~~~~~~~~~~~~~~~~~~~
3435

35-
Command script files can be executed using the built-in ``run_script`` command
36-
or ``@`` shortcut. Both ASCII and UTF-8 encoded unicode text files are
37-
supported. The ``run_script`` command supports tab completion of file system
38-
paths. There is a variant ``_relative_run_script`` command or ``@@``
39-
shortcut for use within a script which uses paths relative to the first script.
36+
Command script files can be executed using the built-in
37+
:ref:`features/builtin_commands:run_script` command or the ``@`` shortcut (if
38+
your application is using the default shortcuts). Both ASCII and UTF-8 encoded
39+
unicode text files are supported. The
40+
:ref:`features/builtin_commands:run_script` command supports tab completion of
41+
file system paths. There is a variant
42+
:ref:`features/builtin_commands:_relative_run_script` command or ``@@``
43+
shortcut (if using the default shortcuts) for use within a script which uses
44+
paths relative to the first script.
4045

4146

4247
Comments
@@ -64,26 +69,36 @@ Python Scripts
6469

6570
If you require logic flow, loops, branching, or other advanced features, you
6671
can write a python script which executes in the context of your ``cmd2`` app.
67-
This script is run using the ``run_pyscript`` command. A simple example of
68-
using ``run_pyscript`` is shown below along with the arg_printer_ script::
72+
This script is run using the :ref:`features/builtin_commands:run_pyscript`
73+
command. Here's a simple example that uses the arg_printer_ script::
6974

7075
(Cmd) run_pyscript examples/scripts/arg_printer.py foo bar 'baz 23'
7176
Running Python script 'arg_printer.py' which was called with 3 arguments
7277
arg 1: 'foo'
7378
arg 2: 'bar'
7479
arg 3: 'baz 23'
7580

76-
``run_pyscript`` supports tab completion of file system paths, and as shown
77-
above it has the ability to pass command-line arguments to the scripts invoked.
81+
:ref:`features/builtin_commands:run_pyscript` supports tab completion of file
82+
system paths, and as shown above it has the ability to pass command-line
83+
arguments to the scripts invoked.
7884

79-
Python scripts executed with ``run_pyscript`` can run ``cmd2`` application
80-
commands by using the syntax::
85+
Python scripts executed with :ref:`features/builtin_commands:run_pyscript` can
86+
run ``cmd2`` application commands by using the syntax::
8187

8288
app(‘command args’)
8389

8490
where:
8591

8692
* ``app`` is a configurable name which can be changed by setting the
87-
``py_bridge_name`` attribute of your ``cmd2.Cmd`` class instance
88-
* ``command`` and ``args`` are entered exactly like they would be entered on
89-
the command line of your ``cmd2`` application
93+
:data:`cmd2.Cmd.py_bridge_name` attribute
94+
* ``command`` and ``args`` are entered exactly like they would be entered by
95+
a user of your application.
96+
97+
.. _python_scripting:
98+
https://github.com/python-cmd2/cmd2/blob/master/examples/python_scripting.py
99+
100+
.. _conditional:
101+
https://github.com/python-cmd2/cmd2/blob/master/examples/scripts/conditional.py
102+
103+
See python_scripting_ example and associated conditional_ script for more
104+
information.

examples/python_scripting.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
#!/usr/bin/env python
22
# coding=utf-8
3-
"""A sample application for how Python scripting can provide conditional control flow of a cmd2 application.
4-
5-
cmd2's built-in scripting capability, which can be invoked via the "@" shortcut or "run_script" command, uses basic
6-
ASCII/UTF-8 text scripts and is very easy to use. Moreover, the trivial syntax of the script files, where there is one
7-
command per line and the line is exactly what the user would type inside the application, makes it so non-technical
8-
that end users can quickly learn to create scripts.
9-
10-
However, there comes a time when technical end users want more capability and power. In particular it is common that
11-
users will want to create a script with conditional control flow - where the next command run will depend on the results
12-
from the previous command. This is where the ability to run Python scripts inside a cmd2 application via the
13-
run_pyscript command and the "run_pyscript <script> [arguments]" syntax comes into play.
14-
15-
This application and the "scripts/conditional.py" script serve as an example for one way in which this can be done.
3+
"""A sample application for how Python scripting can provide conditional
4+
control flow of a cmd2 application.
5+
6+
cmd2's built-in scripting capability, which can be invoked via the "@" shortcut
7+
or "run_script" command, uses basic ASCII/UTF-8 text scripts and is very easy
8+
to use. Moreover, the trivial syntax of the script files, where there is one
9+
command per line and the line is exactly what the user would type inside the
10+
application, makes it so non-technical that end users can quickly learn to
11+
create scripts.
12+
13+
However, there comes a time when technical end users want more capability and
14+
power. In particular it is common that users will want to create a script with
15+
conditional control flow - where the next command run will depend on the
16+
results from the previous command. This is where the ability to run Python
17+
scripts inside a cmd2 application via the run_pyscript command and the
18+
"run_pyscript <script> [arguments]" syntax comes into play.
19+
20+
This application and the "examples/scripts/conditional.py" script serve as an
21+
example for one way in which this can be done.
1622
"""
1723
import argparse
1824
import os

0 commit comments

Comments
 (0)