Build a simple command-line interface (CLI) tool in Python that allows a user to get the current weather for a specified city.
The tool should:
-
Accept a city name as an argument.
-
Use the free Open-Meteo API to fetch the current temperature.
-
Print the result to the console in a clean, human-readable format.
-
Handle potential errors gracefully (e.g., city not found, API down).
There are no specific examples for this yet, as it's our first tool. We will establish the patterns with this feature.
-
Open-Meteo Geocoding API: Used to find the latitude and longitude for a given city.
-
Open-Meteo Weather API: Used to get the weather forecast for a given latitude and longitude.
-
Python's
argparselibrary: For creating the CLI. -
Python's
requestslibrary: For making HTTP requests.
-
The Open-Meteo API is a two-step process: first, you must get the coordinates for a city, then you use those coordinates to get the weather.
-
The API returns a lot of data. We only care about the
current_weatherobject and specifically thetemperaturefield. -
Error handling is crucial. The tool should provide a clear message if the city can't be found by the Geocoding API.