-
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
It appears that the help message is formatted by the https://github.com/python/cpython/blob/8122e8d5017be9f0683a49bc20d3c82e8b5398d6/Lib/argparse.py#L157 |
Beta Was this translation helpful? Give feedback.
-
I hacked it together with prog_name = 'prog'
prog_version = 'ver'
col_base = 'not bold white'
col_usage = 'yellow'
col_option = 'green'
col_caps = 'color(231)'
col_prog = 'cyan'
class RParse(argparse.ArgumentParser):
def _print_message(self, message, file=None):
if message:
if message.startswith('usage'):
message = f'[{col_prog}]{prog_name}[/{col_prog}] {prog_version}\n\n{message}'
message = re.sub(r'(-[a-z]+\s*|\[)([A-Z]+)(?=]|,|\s\s|\s\.)', r'\1[{}]\2[/{}]'.format(col_caps, col_caps), message)
message = re.sub(r'((-|--)[a-z]+)', r'[{}]\1[/{}]'.format(col_option, col_option), message)
message = message.replace('usage', f'[{col_usage}]USAGE[/{col_usage}]')
message = message.replace('options', f'[{col_usage}]FLAGS[/{col_usage}]')
message = message.replace(self.prog, f'[{col_prog}]{self.prog}[/{col_prog}]')
message = f'[{col_base}]{message.strip()}[/{col_base}]'
print(message) |
Beta Was this translation helpful? Give feedback.
-
If still interested in a rich help formatter for argparse, I created https://github.com/hamdanal/rich-argparse a while back and just uploaded it to PyPI. Also it is a single file that has a sole dependency (rich), so it can be copied to your project if you don't want to add yet another dependency. |
Beta Was this translation helpful? Give feedback.
-
Oh nice, I'll check it out. |
Beta Was this translation helpful? Give feedback.
If still interested in a rich help formatter for argparse, I created https://github.com/hamdanal/rich-argparse a while back and just uploaded it to PyPI. Also it is a single file that has a sole dependency (rich), so it can be copied to your project if you don't want to add yet another dependency.