Skip to content

Commit 63d5be9

Browse files
committed
Updated documentation and examples
Added information related to the new pyscript command. The old way of running Python scripts via "py run()" should be considered deprecated. The new "pyscript" command is superior in two significant ways: 1) It supports tab-completion of file system paths 2) It allows the user to pass command-line arguments to scripts
1 parent 4b5c73a commit 63d5be9

File tree

6 files changed

+58
-17
lines changed

6 files changed

+58
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ cmd2 provides the following features, in addition to those already existing in c
3636
- Simple transcript-based application testing
3737
- Unicode character support (*Python 3 only*)
3838
- Path completion for ``edit``, ``load``, ``save``, and ``shell`` commands
39+
- Integrated Python scripting capability via ``pyscript`` and ``py``
3940

4041
Instructions for implementing each feature follow.
4142

docs/freefeatures.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ conditional control flow logic. See the **python_scripting.py** ``cmd2`` applic
176176
the **script_conditional.py** script in the ``examples`` source code directory for an
177177
example of how to achieve this in your own applications.
178178

179+
Using ``py`` to run scripts directly is considered deprecated. The newer ``pyscript`` command
180+
is superior for doing this in two primary ways:
181+
182+
- it supports tab-completion of file system paths
183+
- it has the ability to pass command-line arguments to the scripts invoked
184+
185+
There are no disadvantages to using ``pyscript`` as opposed to ``py run()``. A simple example
186+
of using ``pyscript`` is shown below along with the **examples/arg_printer.py** script::
187+
188+
(Cmd) pyscript examples/arg_printer.py foo bar baz
189+
Running Python script 'arg_printer.py' which was called with 3 arguments
190+
arg 1: 'foo'
191+
arg 2: 'bar'
192+
arg 3: 'baz'
193+
194+
.. note::
195+
196+
If you want to be able to pass arguments with spaces to scripts, then we strongly recommend setting the
197+
cmd2 global variable ``USE_ARG_LIST`` to ``True`` in your application using the ``set_use_arg_list`` function.
198+
This passes all arguments to ``@options`` commands as a list of strings instead of a single string.
199+
200+
Once this option is set, you can then put arguments in quotes like so::
201+
202+
(Cmd) pyscript examples/arg_printer.py hello '23 fnord'
203+
Running Python script 'arg_printer.py' which was called with 2 arguments
204+
arg 1: 'hello'
205+
arg 2: '23 fnord'
206+
207+
179208
IPython (optional)
180209
==================
181210

docs/unfreefeatures.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ There are three functions which can globally effect how arguments are parsed for
204204
.. note::
205205

206206
Since optparse_ has been deprecated since Python 3.2, the ``cmd2`` developers plan to replace optparse_ with
207-
argparse_ in the next version of ``cmd2``. We will endeavor to keep the API as identical as possible when this
207+
argparse_ at some point in the future. We will endeavor to keep the API as identical as possible when this
208208
change occurs.
209209

210210
.. _optparse: https://docs.python.org/3/library/optparse.html

examples/arg_printer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
# coding=utf-8
3+
import os
34
import sys
4-
print("Running Python script {!r} which was called with {} arguments".format(sys.argv[0], len(sys.argv) - 1))
5+
print("Running Python script {!r} which was called with {} arguments".format(os.path.basename(sys.argv[0]),
6+
len(sys.argv) - 1))
57
for i, arg in enumerate(sys.argv[1:]):
68
print("arg {}: {!r}".format(i+1, arg))

examples/python_scripting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
1010
However, there comes a time when technical end users want more capability and power. In particular it is common that
1111
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 py command
13-
and the "py run('myscript.py')" syntax comes into play.
12+
from the previous command. This is where the ability to run Python scripts inside a cmd2 application via the pyscript
13+
command and the "pyscript <script> [arguments]" syntax comes into play.
1414
1515
This application and the "script_conditional.py" script serve as an example for one way in which this can be done.
1616
"""

examples/script_conditional.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,36 @@
44
55
To run it you should do the following:
66
./python_scripting.py
7-
py run('script_conditional.py')
7+
pyscript script_conditional.py directory_path
88
99
Note: The "cmd" function is defined within the cmd2 embedded Python environment and in there "self" is your cmd2
1010
application instance.
1111
"""
12+
import os
13+
import sys
1214

13-
# Try to change to a non-existent directory
14-
cmd('cd foobar')
15+
16+
if len(sys.argv) > 1:
17+
directory = sys.argv[1]
18+
print('Using specified directory: {!r}'.format(directory))
19+
else:
20+
directory = 'foobar'
21+
print('Using default directory: {!r}'.format(directory))
22+
23+
# Keep track of where we stared
24+
original_dir = os.getcwd()
25+
26+
# Try to change to the specified directory
27+
cmd('cd {}'.format(directory))
1528

1629
# Conditionally do something based on the results of the last command
1730
if self._last_result:
18-
print('Contents of foobar directory:')
19-
cmd('dir')
31+
print('\nContents of directory {!r}:'.format(directory))
32+
cmd('dir -l')
2033

2134
# Change back to where we were
22-
cmd('cd ..')
35+
print('Changing back to original directory: {!r}'.format(original_dir))
36+
cmd('cd {}'.format(original_dir))
2337
else:
24-
# Change to parent directory
25-
cmd('cd ..')
26-
print('Contents of parent directory:')
27-
cmd('dir')
28-
29-
# Change back to where we were
30-
cmd('cd examples')
38+
# cd command failed, print a warning
39+
print('Failed to change directory to {!r}'.format(directory))

0 commit comments

Comments
 (0)