feat: add argument parsing
This commit is contained in:
parent
4605b28d79
commit
f29cd6aa5d
@ -1,9 +1,61 @@
|
||||
package eirb.pg203;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
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 OpenWMapKey = "5719b35ddcb39846c228ea2e37357464";
|
||||
WeatherAPI weatherAPI = new WeatherAPI(WeatherAPIKey);
|
||||
@ -16,6 +68,6 @@ public class Main {
|
||||
display.addAPI(openWeatherMap);
|
||||
|
||||
// weatherAPI can't fetch for more than 3 days with free plan
|
||||
display.display(3, "Bordeaux");
|
||||
display.display(days, city);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user