diff options
author | kmvanbrunt <kmvanbrunt@gmail.com> | 2018-10-12 17:43:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-12 17:43:59 -0400 |
commit | 3dcff6406ff772678d2e003a0fd56c5a64d39e60 (patch) | |
tree | 5ff4f56b0eee7191b6079039eadb722966e8565d /cmd2/cmd2.py | |
parent | b216987165d7783903e02db006cf5055f2615796 (diff) | |
parent | 53b976a7aeae88f811391820ca93aaa21b6501e9 (diff) | |
download | cmd2-git-3dcff6406ff772678d2e003a0fd56c5a64d39e60.tar.gz |
Merge pull request #576 from python-cmd2/extra_args
You can now call a macro with extra arguments
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index e09f428d..66ae0968 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2033,18 +2033,22 @@ class Cmd(cmd.Cmd): :param statement: the parsed statement from the command line :return: a flag indicating whether the interpretation of commands should stop """ + from itertools import islice + if statement.command not in self.macros.keys(): raise KeyError('{} is not a macro'.format(statement.command)) macro = self.macros[statement.command] - # For macros, every argument must be provided and there can be no extra arguments. - if len(statement.arg_list) != macro.required_arg_count: - self.perror("The macro '{}' expects {} argument(s)".format(statement.command, macro.required_arg_count), + # Make sure enough arguments were passed in + if len(statement.arg_list) < macro.minimum_arg_count: + self.perror("The macro '{}' expects at least {} argument(s)".format(statement.command, + macro.minimum_arg_count), traceback_war=False) return False - # Resolve the arguments in reverse + # Resolve the arguments in reverse and read their values from statement.argv since those + # are unquoted. Macro args should have been quoted when the macro was created. resolved = macro.value reverse_arg_list = sorted(macro.arg_list, key=lambda ma: ma.start_index, reverse=True) @@ -2059,6 +2063,10 @@ class Cmd(cmd.Cmd): parts = resolved.rsplit(to_replace, maxsplit=1) resolved = parts[0] + replacement + parts[1] + # Append extra arguments and use statement.arg_list since these arguments need their quotes preserved + for arg in islice(statement.arg_list, macro.minimum_arg_count, None): + resolved += ' ' + arg + # Run the resolved command return self.onecmd_plus_hooks(resolved) @@ -2407,7 +2415,7 @@ class Cmd(cmd.Cmd): # Set the macro result = "overwritten" if args.name in self.macros else "created" - self.macros[args.name] = Macro(name=args.name, value=value, required_arg_count=max_arg_num, arg_list=arg_list) + self.macros[args.name] = Macro(name=args.name, value=value, minimum_arg_count=max_arg_num, arg_list=arg_list) self.poutput("Macro '{}' {}".format(args.name, result)) def macro_delete(self, args: argparse.Namespace): @@ -2469,6 +2477,8 @@ class Cmd(cmd.Cmd): "Notes:\n" " To use the literal string {1} in your command, escape it this way: {{1}}.\n" "\n" + " Extra arguments passed when calling a macro are tacked onto resolved command.\n" + "\n" " An argument number can be repeated in a macro. In the following example the\n" " first argument will populate both {1} instances.\n" "\n" |