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
```
This commit is contained in:
Paul Gauthier 2024-06-06 11:38:45 -07:00
parent 0e5342fdb8
commit e7aafd9590
2 changed files with 23 additions and 0 deletions

View file

@ -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()

20
aider/args_formatter.py Normal file
View file

@ -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 ""