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.
This commit is contained in:
Paul Gauthier 2023-05-08 23:06:56 -07:00
parent 6fcf4ff0e7
commit 10192dddcd

View file

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