tools: spiffsgen.py: make default arguments meaningful

Without this, defaults of inverse options (--no-magic-len) were based
on the 'dest' value. In this case, dest='use_magic_len’, and the
default value is True. Which is confusing, because both
—use-magic-len and --no-magic-len show the same default value.

This adds a custom help formatter class which doesn’t add default
to the option help text if the help string already includes it.
This commit is contained in:
Ivan Grokhotkov 2021-04-14 18:42:56 +02:00
parent 9f20eeb1c0
commit a9b81341ca

View File

@ -490,13 +490,31 @@ class SpiffsFS():
return img
class CustomHelpFormatter(argparse.HelpFormatter):
"""
Similar to argparse.ArgumentDefaultsHelpFormatter, except it
doesn't add the default value if "(default:" is already present.
This helps in the case of options with action="store_false", like
--no-magic or --no-magic-len.
"""
def _get_help_string(self, action): # type: (argparse.Action) -> str
if action.help is None:
return ''
if '%(default)' not in action.help and '(default:' not in action.help:
if action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
return action.help + ' (default: %(default)s)'
return action.help
def main(): # type: () -> None
if sys.version_info[0] < 3:
print('WARNING: Support for Python 2 is deprecated and will be removed in future versions.', file=sys.stderr)
elif sys.version_info[0] == 3 and sys.version_info[1] < 6:
print('WARNING: Python 3 versions older than 3.6 are not supported.', file=sys.stderr)
parser = argparse.ArgumentParser(description='SPIFFS Image Generator',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
formatter_class=CustomHelpFormatter)
parser.add_argument('image_size',
help='Size of the created image')
@ -534,7 +552,7 @@ def main(): # type: () -> None
parser.add_argument('--no-magic',
dest='use_magic',
help='Inverse of --use-magic',
help='Inverse of --use-magic (default: --use-magic is enabled)',
action='store_false')
parser.add_argument('--use-magic-len',
@ -544,7 +562,7 @@ def main(): # type: () -> None
parser.add_argument('--no-magic-len',
dest='use_magic_len',
help='Inverse of --use-magic-len',
help='Inverse of --use-magic-len (default: --use-magic-len is enabled)',
action='store_false')
parser.add_argument('--follow-symlinks',