From fb3285ff772d77b76942a623740e9f4f091b12d7 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 19 Apr 2024 11:08:28 -0700 Subject: [PATCH] aider: Added a main function to allow running the script with a model name from the command line and printing the results. # Aider chat conversation: USER: add a main func so i can run this script with a model name on the cmd line and print the results ASSISTANT: Here are the changes to add a `main` function that allows running the script with a model name from the command line and printing the results: aider/models.py ```python <<<<<<< SEARCH ======= import sys def main(): if len(sys.argv) != 2: print("Usage: python models.py ") sys.exit(1) model_name = sys.argv[1] matching_models = fuzzy_match_models(model_name) if matching_models: print(f"Matching models for '{model_name}':") for model in matching_models: print(model) else: print(f"No matching models found for '{model_name}'.") if __name__ == "__main__": main() >>>>>>> REPLACE ``` The `main` function does the following: 1. It checks if the script is called with exactly one command-line argument (the model name). If not, it prints the usage instructions and exits with an error code. 2. It retrieves the model name from the command-line argument using `sys.argv[1]`. 3. It calls the `fuzzy_match_models` function with the provided model name to get the list of matching models. 4. If matching models are found, it prints a message indicating the matching models for the given model name and then prints each matching model on a separate line. 5. If no matching models are found, it prints a message indicating that no matching models were found for the given model name. The `if __name__ == "__main__":` block ensures that the `main` function is only executed when the script is run directly, not when it is imported as a module. With these changes, you can run the script from the command line by providing a model name as an argument, and it will print the matching models or a message if no matches are found. --- aider/models.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/aider/models.py b/aider/models.py index 1ba29572a..f2d85664a 100644 --- a/aider/models.py +++ b/aider/models.py @@ -251,3 +251,22 @@ def fuzzy_match_models(name): matching_models = difflib.get_close_matches(name, models, n=3, cutoff=0.8) return matching_models +import sys + +def main(): + if len(sys.argv) != 2: + print("Usage: python models.py ") + sys.exit(1) + + model_name = sys.argv[1] + matching_models = fuzzy_match_models(model_name) + + if matching_models: + print(f"Matching models for '{model_name}':") + for model in matching_models: + print(model) + else: + print(f"No matching models found for '{model_name}'.") + +if __name__ == "__main__": + main()