implemented checkArguments to prohibit crash on

missing required arguments
This commit is contained in:
Morril
2023-01-23 20:05:15 +01:00
parent 4659e8f7d9
commit 47d64077b2
3 changed files with 32 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}

View File

@@ -22,6 +22,10 @@ public class LastFMToSpotify {
// create hash map with user agent // create hash map with user agent
configuration = new HashMap<>(); 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("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 // parse arguments
for (int a = 0; a < args.length; a++) { for (int a = 0; a < args.length; a++) {
Arguments arg; Arguments arg;

View File

@@ -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.LINE_SEPERATOR;
import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configuration; import static de.b00tload.tools.lastfmtospotifyplaylist.LastFMToSpotify.configuration;
import java.util.List;
import javax.xml.crypto.Data;
public class ArgumentHandler { public class ArgumentHandler {
public static void handle(Arguments argument, @Nullable String value) { public static void handle(Arguments argument, @Nullable String value) {
@@ -28,6 +32,27 @@ public class ArgumentHandler {
handle(argument, null); 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) { private static void help(String value) {
if (value == null) { if (value == null) {
System.out.println("This is a list of all available commands. For more specific help on the argument run --help <argument>."); System.out.println("This is a list of all available commands. For more specific help on the argument run --help <argument>.");