implemented checkArguments to prohibit crash on
missing required arguments
This commit is contained in:
@@ -22,6 +22,10 @@ public class LastFMToSpotify {
|
||||
// create hash map with user agent
|
||||
configuration = new HashMap<>();
|
||||
configuration.put("requests.useragent", "LastFMToSpotify/1.0-Snapshot (" + System.getProperty("os.name") + "; " + System.getProperty("os.arch") + ") Java/" + System.getProperty("java.version"));
|
||||
configuration.put("log.level", "1");
|
||||
if (!ArgumentHandler.checkArguments(args)) {
|
||||
return;
|
||||
}
|
||||
// parse arguments
|
||||
for (int a = 0; a < args.length; a++) {
|
||||
Arguments arg;
|
||||
|
||||
@@ -6,6 +6,10 @@ import org.jetbrains.annotations.Nullable;
|
||||
import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.LINE_SEPERATOR;
|
||||
import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.crypto.Data;
|
||||
|
||||
public class ArgumentHandler {
|
||||
|
||||
public static void handle(Arguments argument, @Nullable String value) {
|
||||
@@ -28,6 +32,27 @@ public class ArgumentHandler {
|
||||
handle(argument, null);
|
||||
}
|
||||
|
||||
public static boolean checkArguments(String[] args) {
|
||||
// check if all required arguments are given
|
||||
Arguments[] required = {Arguments.SECRET, Arguments.CLIENT, Arguments.TOKEN, Arguments.USER};
|
||||
for (Arguments argument : required) {
|
||||
boolean found = false;
|
||||
// check all aliases
|
||||
for (String alias : argument.getAliases()) {
|
||||
// if one alias is found check next argument
|
||||
if (List.of(args).contains(alias)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// else return false
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void help(String value) {
|
||||
if (value == null) {
|
||||
System.out.println("This is a list of all available commands. For more specific help on the argument run --help <argument>.");
|
||||
|
||||
Reference in New Issue
Block a user