From fd127076dffd74e0234ef5ba8c267912f33c868d Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 8 May 2023 23:45:55 -0700 Subject: [PATCH] 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. --- coder.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/coder.py b/coder.py index 7fb63caa3..8611a70be 100755 --- a/coder.py +++ b/coder.py @@ -32,9 +32,9 @@ class Coder: fnames = dict() last_modified = 0 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: readline.read_history_file(self.history_file) except FileNotFoundError: @@ -572,6 +572,12 @@ def main(): 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)", + ) parser.add_argument( "--model", metavar="MODEL", @@ -615,7 +621,7 @@ def main(): fnames = args.files 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: ") if args.apply: @@ -629,4 +635,4 @@ def main(): if __name__ == "__main__": status = main() - sys.exit(status) + sys.exit(status) \ No newline at end of file