feat: add argument parsing

This commit is contained in:
Martin Eyben 2024-11-18 22:56:46 +01:00
parent 4605b28d79
commit f29cd6aa5d

View File

@ -1,9 +1,61 @@
package eirb.pg203; package eirb.pg203;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Main { public class Main {
public static void main(String[] args) throws IOException { private static class Option {
String flag, value;
public Option(String flag, String value){
this.flag = flag;
this.value = value;
}
@Override
public String toString() {
return flag + ':' + value;
}
public String getFlag() {
return flag;
}
public String getValue() {
return value;
}
}
public static void main(String[] args) throws IOException, IllegalArgumentException{
HashMap<String, Option> options = new HashMap<>();
List<String> argsList = new ArrayList<>();
for (int index = 0; index < args.length; index++) {
switch (args[index].charAt(0)) {
case '-':
if (index + 1 >= args.length)
throw new IllegalArgumentException("No arguments provided");
String opt = args[index].substring(1);
options.put(opt, new Option(opt, args[++index]));
break;
default:
argsList.add(args[index]);
}
}
String city;
if (!options.containsKey("l"))
throw new IllegalArgumentException("No city provided with -l");
city = options.get("l").getValue();
int days = 3;
System.out.println(options);
if (options.containsKey("d"))
days = Integer.parseInt(options.get("d").getValue());
String WeatherAPIKey = "cef8e1b6ea364994b5072423240111"; String WeatherAPIKey = "cef8e1b6ea364994b5072423240111";
String OpenWMapKey = "5719b35ddcb39846c228ea2e37357464"; String OpenWMapKey = "5719b35ddcb39846c228ea2e37357464";
WeatherAPI weatherAPI = new WeatherAPI(WeatherAPIKey); WeatherAPI weatherAPI = new WeatherAPI(WeatherAPIKey);
@ -16,6 +68,6 @@ public class Main {
display.addAPI(openWeatherMap); display.addAPI(openWeatherMap);
// weatherAPI can't fetch for more than 3 days with free plan // weatherAPI can't fetch for more than 3 days with free plan
display.display(3, "Bordeaux"); display.display(days, city);
} }
} }