diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-27 00:34:16 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-27 00:34:16 -0400 |
commit | 4e949387d6424b3f3d860c9fe0890d83acd58d09 (patch) | |
tree | ae22a0704f4349be68ddf6cf1ba26dd8efbc3b6a /cmd2/cmd2.py | |
parent | 15e722792f13d98e7276554ff82258f234b630cf (diff) | |
download | cmd2-git-4e949387d6424b3f3d860c9fe0890d83acd58d09.tar.gz |
Ported pyscript to argparse
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 32 |
1 files changed, 10 insertions, 22 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index d1173bdb..bf33201e 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3111,29 +3111,22 @@ class Cmd(cmd.Cmd): self._in_py = False return self._should_quit - @with_argument_list - def do_pyscript(self, arglist: List[str]) -> None: - """\nRun a Python script file inside the console - - Usage: pyscript <script_path> [script_arguments] - -Console commands can be executed inside this script with cmd("your command") -However, you cannot run nested "py" or "pyscript" commands from within this script -Paths or arguments that contain spaces must be enclosed in quotes -""" - if not arglist: - self.perror("pyscript command requires at least 1 argument ...", traceback_war=False) - self.do_help('pyscript') - return + pyscript_parser = ACArgumentParser() + setattr(pyscript_parser.add_argument('script_path', help='path to the script file'), + ACTION_ARG_CHOICES, ('path_complete',)) + pyscript_parser.add_argument('script_arguments', nargs=argparse.REMAINDER, + help='arguments being passed to script') - script_path = os.path.expanduser(arglist[0]) + @with_argparser(pyscript_parser) + def do_pyscript(self, args: argparse.Namespace) -> None: + """Run a Python script file inside the console""" + script_path = os.path.expanduser(args.script_path) # Save current command line arguments orig_args = sys.argv # Overwrite sys.argv to allow the script to take command line arguments - sys.argv = [script_path] - sys.argv.extend(arglist[1:]) + sys.argv = [script_path] + args.script_arguments # Run the script - use repr formatting to escape things which need to be escaped to prevent issues on Windows self.do_py("run({!r})".format(script_path)) @@ -3141,11 +3134,6 @@ Paths or arguments that contain spaces must be enclosed in quotes # Restore command line arguments to original state sys.argv = orig_args - def complete_pyscript(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: - """Enable tab-completion for pyscript command.""" - index_dict = {1: self.path_complete} - return self.index_based_complete(text, line, begidx, endidx, index_dict) - # Only include the do_ipy() method if IPython is available on the system if ipython_available: @with_argparser(ACArgumentParser()) |