I've added some args to a script with argparse which function fine. Now I'm trying to format the --help
output. I've added metavar=''
to each one which has produced a much cleaner output, however there are spaces after the single flags making the text rag oddly.
Problems
Flag will display as -m , --model
instead of -m, --model
Flags with type=bool
with const
and nargs
display as -x [], --xip []
, having the extra space and []
added.
Not finding much info on how to clean this up. Did find discussions on python.org that the extra space is a known problem and using metavar=''
is not ideal.
example code:
import argparse
import colorama
from colorama import init, Fore, Style
init(autoreset=True)
parser = argparse.ArgumentParser(description=Fore.RED + 'Some descriptive Text and such' + Fore.WHITE, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
add_arg = parser.add_argument
add_arg('-m', '--model', type=str, default='model_name', help="some help text", metavar='')
add_arg('-d', '--derp', type=str, default='burp', help="some more help text", metavar='')
add_arg('-x', '--xip', type=bool, default=False, const=True, nargs='?', help="some other help text", metavar='')
args = parser.parse_args()
running python script.py -h
produces:
usage: script.py [-h] [-m] [-d] [-x ]
Some descriptive Text and such # <-- this line displays as RED
optional arguments:
-h, --help show this help message and exit
-m , --model some help text (default: model_name)
-d , --derp some more help text (default: burp)
-x [], --xip [] some other help text (default: False)
changing metavar to metavar='\b'
produces this:
usage: script.py [-h] [-m] [-d] [-x ]] # <- extra bracket ]
Some descriptive Text and such
optional arguments:
-h, --help show this help message and exit
-m, --model some help text (default: model_name) # indent broken
-d, --derp some more help text (default: burp) # indent broken
-x ], --xip ] some other help text (default: False) # indent broken
Coloring Output
I'd also like to know how to properly color the --help
output with colorama. Coloring the description is easy enough, but trying to add coloring to the flags and such produced expected errors. Adding color to help worked if added before, like help=Fore.YELLOW + 'some help text'
, but it colors everything after it, and can't add anymroe Fore
after it without errors. Adding it anywhere else produces an error.
Is there a way to define how to color flags and help text, maybe outside of where they are being set?
Besides that, are there any packages for argparse formatting? I found some older ones that didn't work quite right but nothing new.