| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import re |
| 5 |
|
| 6 |
import colorama |
| 7 |
|
| 8 |
_SECTION_COLOR = colorama.Fore.WHITE + colorama.Style.BRIGHT |
| 9 |
_ARGUMENT_COLOR = colorama.Fore.GREEN + colorama.Style.BRIGHT |
| 10 |
_PARAMETER_COLOR = colorama.Fore.GREEN |
| 11 |
_PROG_COLOR = colorama.Fore.CYAN + colorama.Style.BRIGHT |
| 12 |
_URL_COLOR = colorama.Fore.MAGENTA + colorama.Style.BRIGHT |
| 13 |
_RESET_COLOR = colorama.Style.RESET_ALL |
| 14 |
|
| 15 |
_SUBSTITUTIONS = ( |
| 16 |
("^([^ ].+):$", f"{_SECTION_COLOR}\\1{_RESET_COLOR}:"), |
| 17 |
( |
| 18 |
"(?<!\\w)(--?[a-z-]+) ([A-Z_]+)", |
| 19 |
f"{_ARGUMENT_COLOR}\\1{_RESET_COLOR} {_PARAMETER_COLOR}\\2{_RESET_COLOR}", |
| 20 |
), |
| 21 |
("(?<!\\w)((?!--?merged)--?[a-z-]+)", f"{_ARGUMENT_COLOR}\\1{_RESET_COLOR}"), |
| 22 |
("^(usage): ([^ ]+)\\b", f"{_SECTION_COLOR}\\1{_RESET_COLOR}: {_PROG_COLOR}\\2{_RESET_COLOR}"), |
| 23 |
("(https://[^ ]+[^ .])", f"{_URL_COLOR}\\1{_RESET_COLOR}"), |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def add_color_to_formatter_class(formatter_class): |
| 28 |
class_name = formatter_class.__name__.replace("Formatter", "ColorFormatter") |
| 29 |
|
| 30 |
class Class(formatter_class): |
| 31 |
def format_help(self): |
| 32 |
text = super().format_help() |
| 33 |
for pattern, replacement in _SUBSTITUTIONS: |
| 34 |
matcher = re.compile(pattern, flags=re.MULTILINE) |
| 35 |
text = matcher.sub(replacement, text) |
| 36 |
return text |
| 37 |
|
| 38 |
Class.__name__ = class_name |
| 39 |
|
| 40 |
return Class |