Add command line argument for history file with environment variable default and pass it to Coder class.

# Context:
USER: make a cmd line arg with environ default for history_file, pass it into Coder.__init__
ASSISTANT: To add a command line argument for the history file with an environment variable default, you can update the `argparse` section in the `main()` function. Here's the change:

```python
coder.py
<<<<<<< ORIGINAL
    parser = argparse.ArgumentParser(description="Chat with GPT about code")
    parser.add_argument(
        "files",
        metavar="FILE",
        nargs="+",
        help="a list of source code files",
    )
=======
    parser = argparse.ArgumentParser(description="Chat with GPT about code")
    parser.add_argument(
        "files",
        metavar="FILE",
        nargs="+",
        help="a list of source code files",
    )
    parser.add_argument(
        "--history-file",
        metavar="HISTORY_FILE",
        default=os.environ.get("CODER_HISTORY_FILE", ".coder.history"),
        help="Specify the history file (default: .coder.history or value from CODER_HISTORY_FILE environment variable)",
    )
>>>>>>> UPDATED
```

Next, pass the `history_file` argument to the `Coder` class in the `main()` function:

```python
coder.py
<<<<<<< ORIGINAL
    coder = Coder(args.model, fnames, pretty)
=======
    coder = Coder(args.model, fnames, pretty, args.history_file)
>>>>>>> UPDATED
```

Finally, update the `Coder` class to accept the `history_file` argument and use it in the `__init__` method:

```python
coder.py
<<<<<<< ORIGINAL
class Coder:
    fnames = dict()
    last_modified = 0
    repo = None
    history_file = ".coder.history"

    def __init__(self, main_model, files, pretty):
=======
class Coder:
    fnames = dict()
    last_modified = 0
    repo = None

    def __init__(self, main_model, files, pretty, history_file=".coder.history"):
        self.history_file = history_file
>>>>>>> UPDATED
```
Now, the history file can be specified using the `--history-file` command line argument or the `CODER_HISTORY_FILE` environment variable.
This commit is contained in:
Paul Gauthier 2023-05-08 23:45:55 -07:00
parent fed71fef4c
commit fd127076df

View file

@ -32,9 +32,9 @@ class Coder:
fnames = dict() fnames = dict()
last_modified = 0 last_modified = 0
repo = None repo = None
history_file = ".coder.history"
def __init__(self, main_model, files, pretty): def __init__(self, main_model, files, pretty, history_file=".coder.history"):
self.history_file = history_file
try: try:
readline.read_history_file(self.history_file) readline.read_history_file(self.history_file)
except FileNotFoundError: except FileNotFoundError:
@ -572,6 +572,12 @@ def main():
nargs="+", nargs="+",
help="a list of source code files", help="a list of source code files",
) )
parser.add_argument(
"--history-file",
metavar="HISTORY_FILE",
default=os.environ.get("CODER_HISTORY_FILE", ".coder.history"),
help="Specify the history file (default: .coder.history or value from CODER_HISTORY_FILE environment variable)",
)
parser.add_argument( parser.add_argument(
"--model", "--model",
metavar="MODEL", metavar="MODEL",
@ -615,7 +621,7 @@ def main():
fnames = args.files fnames = args.files
pretty = args.pretty pretty = args.pretty
coder = Coder(args.model, fnames, pretty) coder = Coder(args.model, fnames, pretty, args.history_file)
coder.commit(ask=not args.commit_dirty, prefix="WIP: ") coder.commit(ask=not args.commit_dirty, prefix="WIP: ")
if args.apply: if args.apply:
@ -629,4 +635,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
status = main() status = main()
sys.exit(status) sys.exit(status)