Set a fixed width when updating CLI help on readme

By default it uses the terminal width, which caused the help text to change spuriously.
This commit is contained in:
Isaiah Odhner 2023-05-02 18:19:58 -04:00
parent 77fa6cb444
commit 1cdad17fe9
2 changed files with 15 additions and 3 deletions

View File

@ -88,8 +88,10 @@ options:
Language to use
--ascii-only-icons Use only ASCII characters for tool icons
--inspect-layout Inspect the layout with middle click, for development
--clear-screen Clear the screen before starting; useful for development, to avoid seeing fixed errors
--restart-on-changes Restart the app when the source code is changed, for development
--clear-screen Clear the screen before starting; useful for
development, to avoid seeing fixed errors
--restart-on-changes Restart the app when the source code is changed, for
development
--recode-samples Open and save each file in samples/, for testing
```

View File

@ -159,10 +159,20 @@ parser.add_argument('filename', nargs='?', default=None, help='File to open')
readme_help_start = re.compile(r"```\n.*--help\n")
readme_help_end = re.compile(r"```")
with open("README.md", "r+") as f:
# This is a hacky way to have to fix the width without creating a separate ArgumentParser,
# and without breaking the wrapping if you use --help.
# https://stackoverflow.com/questions/44333577/explain-lambda-argparse-helpformatterprog-width
# This lambda only works because python uses the same syntax for construction and function calls.
width = 80
old_formatter_class = parser.formatter_class
parser.formatter_class = lambda prog: argparse.HelpFormatter(prog, width=width)
help_text = parser.format_help()
parser.formatter_class = old_formatter_class
md = f.read()
start = readme_help_start.search(md).end()
end = readme_help_end.search(md, start).start()
md = md[:start] + parser.format_help() + md[end:]
md = md[:start] + help_text + md[end:]
f.seek(0)
f.write(md)
f.truncate()