hamdanal / rich-argparse

A rich help formatter for argparse
https://pypi.org/project/rich-argparse/
MIT License
129 stars 11 forks source link

Crash when defining multiple highlight regexes with the same style #36

Closed hamdanal closed 1 year ago

hamdanal commented 1 year ago

Since version 0.4.0 $ cat t.py

import argparse
from rich_argparse import RichHelpFormatter
RichHelpFormatter.highlights.append(r"'(?P<syntax>[^']*)'")
parser = argparse.ArgumentParser(formatter_class=RichHelpFormatter)
parser.print_help()

$python -m t

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/tmp/t/t.py", line 7, in <module>
    parser.print_help()
  File "/usr/lib/python3.10/argparse.py", line 2568, in print_help
    self._print_message(self.format_help(), file)
  File "/usr/lib/python3.10/argparse.py", line 2545, in format_help
    formatter.add_arguments(action_group._group_actions)
  File "/usr/lib/python3.10/argparse.py", line 289, in add_arguments
    self.add_argument(action)
  File "/tmp/t/venv/lib/python3.10/site-packages/rich_argparse.py", line 135, in add_argument
    self._current_section.rich_actions.extend(self._rich_format_action(action))
  File "/tmp/t/venv/lib/python3.10/site-packages/rich_argparse.py", line 239, in _rich_format_action
    help = self._rich_expand_help(action) if action.help and action.help.strip() else None
  File "/tmp/t/venv/lib/python3.10/site-packages/rich_argparse.py", line 221, in _rich_expand_help
    rich_help.highlight_regex("|".join(self.highlights), style_prefix="argparse.")
  File "/tmp/t/venv/lib/python3.10/site-packages/rich/text.py", line 579, in highlight_regex
    for match in re.finditer(re_highlight, plain):
  File "/usr/lib/python3.10/re.py", line 247, in finditer
    return _compile(pattern, flags).finditer(string)
  File "/usr/lib/python3.10/re.py", line 303, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/lib/python3.10/sre_compile.py", line 788, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/lib/python3.10/sre_parse.py", line 955, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/lib/python3.10/sre_parse.py", line 444, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "/usr/lib/python3.10/sre_parse.py", line 838, in _parse
    raise source.error(err.msg, len(name) + 1) from None
re.error: redefinition of group name 'syntax' as group 3; was group 2 at position 61