MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / do_run_pyscript

Method do_run_pyscript

cmd2/cmd2.py:5035–5066  ·  view source on GitHub ↗

Run Python script within this application's environment. :return: True if running of commands should stop

(self, args: argparse.Namespace)

Source from the content-addressed store, hash-verified

5033
5034 @with_argparser(_build_run_pyscript_parser)
5035 def do_run_pyscript(self, args: argparse.Namespace) -> bool | None:
5036 """Run Python script within this application's environment.
5037
5038 :return: True if running of commands should stop
5039 """
5040 self.last_result = False
5041
5042 # Expand ~ before placing this path in sys.argv just as a shell would
5043 args.script_path = os.path.expanduser(args.script_path)
5044
5045 # Add some protection against accidentally running a non-Python file. The happens when users
5046 # mix up run_script and run_pyscript.
5047 if not args.script_path.endswith(".py"):
5048 self.pwarning(f"'{args.script_path}' does not have a .py extension")
5049 selection = self.select("Yes No", "Continue to try to run it as a Python script? ")
5050 if selection != "Yes":
5051 return None
5052
5053 # Save current command line arguments
5054 orig_args = sys.argv
5055
5056 try:
5057 # Overwrite sys.argv to allow the script to take command line arguments
5058 sys.argv = [args.script_path, *args.script_arguments]
5059
5060 # self.last_result will be set by _run_python()
5061 py_return = self._run_python(pyscript=args.script_path)
5062 finally:
5063 # Restore command line arguments to original state
5064 sys.argv = orig_args
5065
5066 return py_return
5067
5068 @staticmethod
5069 def _build_ipython_parser() -> Cmd2ArgumentParser:

Callers

nothing calls this directly

Calls 3

pwarningMethod · 0.95
selectMethod · 0.95
_run_pythonMethod · 0.95

Tested by

no test coverage detected