From 10192dddcd597e65956b257c182edac9d9d45acd Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 8 May 2023 23:06:56 -0700 Subject: [PATCH] Add env_prefix variable and load default values from environment variables. # Context: USER: in main add a var called env_prefix="CODER_" then for every parser arg load the default from os.environ using that prefix and the uppercase, snakecase of the arg name as the env var name ASSISTANT: To add an `env_prefix` variable and load the default values for each argument from the environment variables, you can update the `main` function as follows: ```python coder.py <<<<<<< ORIGINAL def main(): 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( "-3", "--gpt-3-5-turbo", action="store_true", help="Only use gpt-3.5-turbo, not gpt-4", ) parser.add_argument( "--no-pretty", action="store_false", dest="pretty", default=True, help="Disable prettyd output of GPT responses", ) parser.add_argument( "--apply", metavar="FILE", help="Apply the changes from the given file instead of running the chat", ) parser.add_argument( "--commit", action="store_true", help="Commit dirty files without confirmation", ) ======= def main(): env_prefix = "CODER_" parser = argparse.ArgumentParser(description="Chat with GPT about code") parser.add_argument( "files", metavar="FILE", nargs="+", help="a list of source code files", default=os.environ.get(env_prefix + "FILES", "").split() ) parser.add_argument( "-3", "--gpt-3-5-turbo", action="store_true", help="Only use gpt-3.5-turbo, not gpt-4", default=bool(os.environ.get(env_prefix + "GPT_3_5_TURBO", False)) ) parser.add_argument( "--no-pretty", action="store_false", dest="pretty", default=True, help="Disable prettyd output of GPT responses", default=bool(os.environ.get(env_prefix + "NO_PRETTY", True)) ) parser.add_argument( "--apply", metavar="FILE", help="Apply the changes from the given file instead of running the chat", default=os.environ.get(env_prefix + "APPLY", None) ) parser.add_argument( "--commit", action="store_true", help="Commit dirty files without confirmation", default=bool(os.environ.get(env_prefix + "COMMIT", False)) ) >>>>>>> UPDATED ``` This change adds the `env_prefix` variable and sets the default values for each argument using the `os.environ.get()` function with the appropriate environment variable name. --- coder.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/coder.py b/coder.py index 64cbcfc98..e7924bbe9 100755 --- a/coder.py +++ b/coder.py @@ -569,15 +569,19 @@ class Coder: def main(): + env_prefix = "CODER_" + parser = argparse.ArgumentParser(description="Chat with GPT about code") parser.add_argument( - "files", metavar="FILE", nargs="+", help="a list of source code files" + "files", metavar="FILE", nargs="+", help="a list of source code files", + default=os.environ.get(env_prefix + "FILES", "").split() ) parser.add_argument( "-3", "--gpt-3-5-turbo", action="store_true", help="Only use gpt-3.5-turbo, not gpt-4", + default=bool(os.environ.get(env_prefix + "GPT_3_5_TURBO", False)) ) parser.add_argument( "--no-pretty", @@ -585,18 +589,20 @@ def main(): dest="pretty", default=True, help="Disable prettyd output of GPT responses", + default=bool(os.environ.get(env_prefix + "NO_PRETTY", True)) ) parser.add_argument( "--apply", metavar="FILE", help="Apply the changes from the given file instead of running the chat", + default=os.environ.get(env_prefix + "APPLY", None) ) parser.add_argument( "--commit", action="store_true", help="Commit dirty files without confirmation", + default=bool(os.environ.get(env_prefix + "COMMIT", False)) ) - args = parser.parse_args() use_gpt_4 = not args.gpt_3_5_turbo @@ -617,4 +623,4 @@ def main(): if __name__ == "__main__": status = main() - sys.exit(status) + sys.exit(status) \ No newline at end of file