Skip to content

Commit a8c6434

Browse files
authored
Merge pull request #125 from python-cmd2/script_arguments
Added new pyscript command
2 parents 0af8018 + 34c4f3c commit a8c6434

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ News
1111
* Enhancements
1212
* Added the ability to exclude commands from the help menu (**eof** included by default)
1313
* Redundant list command removed and features merged into history command
14+
* Added **pyscript** command which supports running Python scripts with arguments
1415

1516
0.7.2
1617
-----

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ example/exampleSession.txt:
155155
156156
Documented commands (type help <topic>):
157157
========================================
158-
_relative_load edit help list orate py run say shell show
159-
cmdenvironment eof history load pause quit save set shortcuts speak
158+
_relative_load help orate pyscript save shell speak
159+
cmdenvironment history pause quit say shortcuts
160+
edit load py run set show
160161
161162
(Cmd) help say
162163
Repeats what you tell me to.

cmd2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,42 @@ def quit():
15831583
self._in_py = False
15841584
return self._should_quit
15851585

1586+
# noinspection PyUnusedLocal
1587+
@options([], arg_desc='<script_path> [script_arguments]')
1588+
def do_pyscript(self, arg, opts=None):
1589+
"""\nRuns a python script file inside the console
1590+
1591+
Console commands can be executed inside this script with cmd("your command")
1592+
However, you cannot run nested "py" or "pyscript" commands from within this script
1593+
Paths or arguments that contain spaces must be enclosed in quotes
1594+
"""
1595+
if not arg:
1596+
self.perror("pyscript command requires at least 1 argument ...", traceback_war=False)
1597+
self.do_help('pyscript')
1598+
return
1599+
1600+
if not USE_ARG_LIST:
1601+
arg = shlex.split(arg, posix=POSIX_SHLEX)
1602+
1603+
# Get the absolute path of the script
1604+
script_path = os.path.abspath(os.path.expanduser(arg[0]))
1605+
1606+
# Save current command line arguments
1607+
orig_args = sys.argv
1608+
1609+
# Overwrite sys.argv to allow the script to take command line arguments
1610+
sys.argv = [script_path]
1611+
sys.argv.extend(arg[1:])
1612+
1613+
# Run the script
1614+
self.do_py("run('{}')".format(arg[0]))
1615+
1616+
# Restore command line arguments to original state
1617+
sys.argv = orig_args
1618+
1619+
# Enable tab completion of paths for pyscript command
1620+
complete_pyscript = path_complete
1621+
15861622
# Only include the do_ipy() method if IPython is available on the system
15871623
if ipython_available:
15881624
# noinspection PyMethodMayBeStatic,PyUnusedLocal

examples/arg_printer.py

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

examples/exampleSession.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
Documented commands (type help <topic>):
55
========================================
6-
_relative_load edit history orate py run say shell show
7-
cmdenvironment help load pause quit save set shortcuts speak
6+
_relative_load help orate pyscript save shell speak
7+
cmdenvironment history pause quit say shortcuts
8+
edit load py run set show
89

910
(Cmd) help say
1011
Repeats what you tell me to.

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
# Help text for base cmd2.Cmd application
1616
BASE_HELP = """Documented commands (type help <topic>):
1717
========================================
18-
_relative_load edit history pause quit save shell show
19-
cmdenvironment help load py run set shortcuts
18+
_relative_load edit history pause pyscript run set shortcuts
19+
cmdenvironment help load py quit save shell show
2020
"""
2121

2222
# Help text for the history command

tests/test_transcript.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ def test_base_with_transcript(_cmdline_app):
106106
107107
Documented commands (type help <topic>):
108108
========================================
109-
_relative_load edit history orate py run say shell show
110-
cmdenvironment help load pause quit save set shortcuts speak
109+
_relative_load help orate pyscript save shell speak
110+
cmdenvironment history pause quit say shortcuts
111+
edit load py run set show
111112
112113
(Cmd) help say
113114
Repeats what you tell me to.

tests/transcript.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
Documented commands (type help <topic>):
44
========================================
5-
_relative_load edit history orate py run say shell show
6-
cmdenvironment help load pause quit save set shortcuts speak
5+
_relative_load help orate pyscript save shell speak
6+
cmdenvironment history pause quit say shortcuts
7+
edit load py run set show
78

89
(Cmd) help say
910
Repeats what you tell me to.

0 commit comments

Comments
 (0)