From a9b81341caa5c5143bac0458da5ca5e8557995ad Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Wed, 14 Apr 2021 18:42:56 +0200 Subject: [PATCH] tools: spiffsgen.py: make default arguments meaningful MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- components/spiffs/spiffsgen.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/components/spiffs/spiffsgen.py b/components/spiffs/spiffsgen.py index 6a65a288dc..672e98f4a8 100755 --- a/components/spiffs/spiffsgen.py +++ b/components/spiffs/spiffsgen.py @@ -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',