diff options
-rw-r--r-- | cmd2/cmd2.py | 11 | ||||
-rw-r--r-- | cmd2/parsing.py | 22 |
2 files changed, 16 insertions, 17 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index e9957c62..75accb7f 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -48,8 +48,7 @@ from . import utils from . import plugin from .argparse_completer import AutoCompleter, ACArgumentParser, ACTION_ARG_CHOICES from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer -from .parsing import StatementParser, Statement, Macro, MacroArg, \ - macro_normal_arg_pattern, macro_escaped_arg_pattern, digit_pattern +from .parsing import StatementParser, Statement, Macro, MacroArg # Set up readline from .rl_utils import rl_type, RlType, rl_get_point, rl_set_prompt, vt100_support, rl_make_safe_prompt @@ -2409,7 +2408,7 @@ class Cmd(cmd.Cmd): # Find all normal arguments arg_list = [] - normal_matches = re.finditer(macro_normal_arg_pattern, value) + normal_matches = re.finditer(MacroArg.macro_normal_arg_pattern, value) max_arg_num = 0 num_set = set() @@ -2418,7 +2417,7 @@ class Cmd(cmd.Cmd): cur_match = normal_matches.__next__() # Get the number between the braces - cur_num = int(re.findall(digit_pattern, cur_match.group())[0]) + cur_num = int(re.findall(MacroArg.digit_pattern, cur_match.group())[0]) if cur_num < 1: self.perror("Argument numbers must be greater than 0", traceback_war=False) return @@ -2439,14 +2438,14 @@ class Cmd(cmd.Cmd): return # Find all escaped arguments - escaped_matches = re.finditer(macro_escaped_arg_pattern, value) + escaped_matches = re.finditer(MacroArg.macro_escaped_arg_pattern, value) while True: try: cur_match = escaped_matches.__next__() # Get the number between the braces - cur_num = int(re.findall(digit_pattern, cur_match.group())[0]) + cur_num = int(re.findall(MacroArg.digit_pattern, cur_match.group())[0]) arg_list.append(MacroArg(start_index=cur_match.start(), number=cur_num, is_escaped=True)) except StopIteration: diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 498834cf..696904ff 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -12,17 +12,6 @@ import attr from . import constants from . import utils -# Pattern used to find normal argument -# Match strings like: {5}, {{{{{4}, {2}}}}} -macro_normal_arg_pattern = re.compile(r'(?<!\{)\{\d+\}|\{\d+\}(?!\})') - -# Pattern used to find escaped arguments (2 or more braces on each side of digit) -# Match strings like: {{5}}, {{{{{4}}, {{2}}}}}, {{{4}}} -macro_escaped_arg_pattern = re.compile(r'\{{2}\d+\}{2}') - -# Finds a string of digits -digit_pattern = re.compile(r'\d+') - @attr.s(frozen=True) class MacroArg: @@ -40,6 +29,17 @@ class MacroArg: # Tells if this argument is escaped and therefore needs to be unescaped is_escaped = attr.ib(validator=attr.validators.instance_of(bool), type=bool) + # Pattern used to find normal argument + # Match strings like: {5}, {{{{{4}, {2}}}}} + macro_normal_arg_pattern = re.compile(r'(?<!\{)\{\d+\}|\{\d+\}(?!\})') + + # Pattern used to find escaped arguments (2 or more braces on each side of digit) + # Match strings like: {{5}}, {{{{{4}}, {{2}}}}}, {{{4}}} + macro_escaped_arg_pattern = re.compile(r'\{{2}\d+\}{2}') + + # Finds a string of digits + digit_pattern = re.compile(r'\d+') + @attr.s(frozen=True) class Macro: |