From e7aafd95909a81727c2a3cd16aa63cc29cff9850 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 6 Jun 2024 11:38:45 -0700 Subject: [PATCH] aider: Add argument formatter to print arguments in a specified format. # Aider chat conversation: USER: Add an args formatter that prints out args like this: ## Output Settings: ### --dark-mode Use colors suitable for a dark terminal background Default: False ### --light-mode Use colors suitable for a light terminal background Default: False ... ASSISTANT: To add an argument formatter that prints out arguments in the specified format, we need to: 1. Create a new formatter class. 2. Update the `get_help` function to use this new formatter. Here are the changes: 1. Create a new file `aider/args_formatter.py` with the custom formatter class. 2. Update `aider/args.py` to use this new formatter. Let's start with the new file: aider/args_formatter.py ```python <<<<<<< SEARCH ======= import argparse class CustomHelpFormatter(argparse.HelpFormatter): def start_section(self, heading): super().start_section(f"## {heading}:") def _format_action(self, action): parts = [] if action.help: parts.append(f"### --{action.dest}") parts.append(action.help) if action.default is not argparse.SUPPRESS: parts.append(f"Default: {action.default}") return "\n".join(parts) def _format_action_invocation(self, action): return "" def _format_args(self, action, default_metavar): return "" >>>>>>> REPLACE ``` Next, update `aider/args.py` to use this new formatter: aider/args.py ```python <<<<<<< SEARCH from aider import __version__, models ======= from aider import __version__, models from aider.args_formatter import CustomHelpFormatter >>>>>>> REPLACE ``` aider/args.py ```python <<<<<<< SEARCH description="aider is GPT powered coding in your terminal", ======= description="aider is GPT powered coding in your terminal", formatter_class=CustomHelpFormatter, >>>>>>> REPLACE ``` aider/args.py ```python <<<<<<< SEARCH parser = get_parser([], None) ======= parser = get_parser([], None) parser.formatter_class = CustomHelpFormatter >>>>>>> REPLACE ``` --- aider/args.py | 3 +++ aider/args_formatter.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 aider/args_formatter.py diff --git a/aider/args.py b/aider/args.py index 3b3e85b52..794c1627b 100644 --- a/aider/args.py +++ b/aider/args.py @@ -7,11 +7,13 @@ import sys import configargparse from aider import __version__, models +from aider.args_formatter import CustomHelpFormatter def get_parser(default_config_files, git_root): parser = configargparse.ArgumentParser( description="aider is GPT powered coding in your terminal", + formatter_class=CustomHelpFormatter, add_config_file_help=True, default_config_files=default_config_files, auto_env_var_prefix="AIDER_", @@ -455,6 +457,7 @@ def get_help(): os.environ["COLUMNS"] = "100" sys.argv[0] = "aider" parser = get_parser([], None) + parser.formatter_class = CustomHelpFormatter return parser.format_help() diff --git a/aider/args_formatter.py b/aider/args_formatter.py new file mode 100644 index 000000000..fc5991f84 --- /dev/null +++ b/aider/args_formatter.py @@ -0,0 +1,20 @@ +import argparse + +class CustomHelpFormatter(argparse.HelpFormatter): + def start_section(self, heading): + super().start_section(f"## {heading}:") + + def _format_action(self, action): + parts = [] + if action.help: + parts.append(f"### --{action.dest}") + parts.append(action.help) + if action.default is not argparse.SUPPRESS: + parts.append(f"Default: {action.default}") + return "\n".join(parts) + + def _format_action_invocation(self, action): + return "" + + def _format_args(self, action, default_metavar): + return ""