diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-04-21 00:04:49 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-04-21 00:04:49 -0400 |
commit | dd61db791b0886fd103c917e2323ae27ae58423f (patch) | |
tree | 250274e804f8ce0abac4adcfaac4eaaf981a28aa /cmd2 | |
parent | 0ccde8ba416e36af2484276d9763a8742214528d (diff) | |
download | cmd2-git-dd61db791b0886fd103c917e2323ae27ae58423f.tar.gz |
run_pyscript now passes a keyword arg to do_py instead of using a hidden argparse flag
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/cmd2.py | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 98969d10..34f53044 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3169,17 +3169,16 @@ class Cmd(cmd.Cmd): py_parser.add_argument('command', nargs=argparse.OPTIONAL, help="command to run") py_parser.add_argument('remainder', nargs=argparse.REMAINDER, help="remainder of command") - # This is a hidden flag for telling do_py to run a pyscript. It is intended only to be used by run_pyscript - # after it sets up sys.argv for the script being run. When this flag is present, it takes precedence over all - # other arguments. - py_parser.add_argument('--pyscript', help=argparse.SUPPRESS) - # Preserve quotes since we are passing these strings to Python @with_argparser(py_parser, preserve_quotes=True) - def do_py(self, args: argparse.Namespace) -> Optional[bool]: + def do_py(self, args: argparse.Namespace, *, pyscript: Optional[str] = None) -> Optional[bool]: """ Enter an interactive Python shell + :param args: Namespace of args on the command line + :param pyscript: optional path to a pyscript file to run. This is intended only to be used by run_pyscript + after it sets up sys.argv for the script. If populated, this takes precedence over all + other arguments. (Defaults to None) :return: True if running of commands should stop """ def py_quit(): @@ -3211,9 +3210,9 @@ class Cmd(cmd.Cmd): localvars['self'] = self # Handle case where we were called by run_pyscript - if args.pyscript: + if pyscript is not None: # Read the script file - expanded_filename = os.path.expanduser(utils.strip_quotes(args.pyscript)) + expanded_filename = os.path.expanduser(pyscript) try: with open(expanded_filename) as f: @@ -3320,7 +3319,7 @@ class Cmd(cmd.Cmd): sys.argv = [args.script_path] + args.script_arguments # noinspection PyTypeChecker - py_return = self.do_py('--pyscript {}'.format(utils.quote_string(args.script_path))) + py_return = self.do_py('', pyscript=args.script_path) finally: # Restore command line arguments to original state |