Refactor main method to use commons-cli to parse arguments
This commit is contained in:
parent
ddf8047ebd
commit
b53e7a617d
2 changed files with 79 additions and 44 deletions
23
src/main/java/de/jotoho/waituntil/AppOptions.java
Normal file
23
src/main/java/de/jotoho/waituntil/AppOptions.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package de.jotoho.waituntil;
|
||||||
|
|
||||||
|
import org.apache.commons.cli.Option;
|
||||||
|
import org.apache.commons.cli.Options;
|
||||||
|
|
||||||
|
final class AppOptions {
|
||||||
|
// Disable Instance Creation
|
||||||
|
private AppOptions() {}
|
||||||
|
|
||||||
|
public final static Option help =
|
||||||
|
Option.builder().argName("h").longOpt("help").desc("Shows this help " +
|
||||||
|
"message and exits").build();
|
||||||
|
public final static Option version =
|
||||||
|
Option.builder().argName("v").longOpt("version").desc("Shows version information and exits").build();
|
||||||
|
|
||||||
|
private final static Options options = new Options()
|
||||||
|
.addOption(help)
|
||||||
|
.addOption(version);
|
||||||
|
|
||||||
|
public static Options getOptions() {
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,8 +18,8 @@ package de.jotoho.waituntil;
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.HashSet;
|
import org.apache.commons.cli.DefaultParser;
|
||||||
import java.util.Map;
|
import org.apache.commons.cli.ParseException;
|
||||||
|
|
||||||
import static de.jotoho.waituntil.GlobalConf.applicationOutputLanguage;
|
import static de.jotoho.waituntil.GlobalConf.applicationOutputLanguage;
|
||||||
|
|
||||||
|
@ -28,53 +28,65 @@ import static de.jotoho.waituntil.GlobalConf.applicationOutputLanguage;
|
||||||
// Author: Jonas Tobias Hopusch (@jotoho)
|
// Author: Jonas Tobias Hopusch (@jotoho)
|
||||||
|
|
||||||
public final class Main {
|
public final class Main {
|
||||||
public static void main(final String[] args) {
|
|
||||||
final var optionDictionary = Map.of("-h", "--help", "-v", "--version");
|
|
||||||
|
|
||||||
final var options = new HashSet<String>();
|
private static void printVersionInformation() {
|
||||||
final var words = new HashSet<String>();
|
final var thisPackage = Main.class.getPackage();
|
||||||
|
final var appVersion = thisPackage.getImplementationVersion() != null ? thisPackage.getImplementationVersion() : "version unknown";
|
||||||
|
System.out.println("waituntil " + appVersion);
|
||||||
|
System.out.println("""
|
||||||
|
|
||||||
for (final String arg : args) {
|
This program is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
if (arg.startsWith("--")) {
|
GNU General Public License as published by the Free Software Foundation, either version 3 of the
|
||||||
options.add(arg.substring(2));
|
License, or (at your option) any later version.
|
||||||
} else if (arg.startsWith("-")) {
|
|
||||||
if (optionDictionary.containsKey(arg))
|
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
options.add(optionDictionary.get(arg).substring(2));
|
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
else
|
See the GNU General Public License for more details.""");
|
||||||
System.err.println("Short-hand '$arg' does not exist. Ignoring!");
|
}
|
||||||
} else {
|
|
||||||
words.add(arg);
|
private static void printHelpInformation() {
|
||||||
}
|
switch (applicationOutputLanguage) {
|
||||||
|
case GlobalConf.langGerman -> System.out.println("Hilfe kommt noch. (Nicht implementiert)");
|
||||||
|
default -> System.out.println("Help is yet to come. (Not implemented)");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (options.contains("help")) {
|
public static void main(final String[] args) {
|
||||||
switch (applicationOutputLanguage) {
|
try {
|
||||||
case GlobalConf.langGerman -> System.out.println("Hilfe kommt noch. (Nicht implementiert)");
|
final var parsedArguments =
|
||||||
default -> System.out.println("Help is yet to come. (Not implemented)");
|
DefaultParser.builder()
|
||||||
}
|
.setStripLeadingAndTrailingQuotes(true)
|
||||||
} else if (options.contains("version")) {
|
.build()
|
||||||
final var thisPackage = Main.class.getPackage();
|
.parse(AppOptions.getOptions(), args);
|
||||||
final var appVersion = thisPackage.getImplementationVersion() != null
|
|
||||||
? thisPackage.getImplementationVersion()
|
final var userData = parsedArguments.getArgs();
|
||||||
: "version unknown";
|
|
||||||
System.out.println("waituntil " + appVersion);
|
if (parsedArguments.hasOption(AppOptions.help)) {
|
||||||
System.out.println("""
|
printHelpInformation();
|
||||||
|
} else if (parsedArguments.hasOption(AppOptions.version)) {
|
||||||
This program is free software: you can redistribute it and/or modify it under the terms of the
|
printVersionInformation();
|
||||||
GNU General Public License as published by the Free Software Foundation, either version 3 of the
|
} else if (userData.length == 0) {
|
||||||
License, or (at your option) any later version.
|
switch (applicationOutputLanguage) {
|
||||||
|
case GlobalConf.langGerman -> System.err.println("FATAL: Es wurde exact ein nicht-flag Argument erwartet. (" + userData.length + " erhalten)");
|
||||||
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
default -> System.err.println("FATAL: Expected one non-flag argument. (Got " + userData.length + ")");
|
||||||
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
}
|
||||||
See the GNU General Public License for more details.""");
|
System.exit(1);
|
||||||
} else if (words.size() == 1) {
|
} else if (userData.length > 1) {
|
||||||
final var target = TimeCalculator.calculateAndAnnounceTargetTime(words.iterator().next());
|
switch (applicationOutputLanguage) {
|
||||||
Sleep.waitUntilTimeStamp(target);
|
case GlobalConf.langGerman -> System.err.println("FATAL: " +
|
||||||
} else {
|
"Es wurde exact ein nicht-flag Argument erwartet. (" + userData.length + " erhalten)");
|
||||||
switch (applicationOutputLanguage) {
|
default -> System.err.println("FATAL: Expected one " +
|
||||||
case GlobalConf.langGerman -> System.err.println("FATAL: Es wurde exact ein nicht-flag Argument erwartet. (" + words.size() + " erhalten)");
|
"non-flag argument. (Got " + userData.length + ")");
|
||||||
default -> System.err.println("FATAL: Expected one non-flag argument. (Got " + words.size() + ")");
|
}
|
||||||
|
System.exit(1);
|
||||||
|
} else {
|
||||||
|
final var target =
|
||||||
|
TimeCalculator.calculateAndAnnounceTargetTime(userData[0]);
|
||||||
|
Sleep.waitUntilTimeStamp(target);
|
||||||
}
|
}
|
||||||
|
} catch (final ParseException e) {
|
||||||
|
System.getLogger("main").log(System.Logger.Level.ERROR, "Parsing " +
|
||||||
|
"of arguments failed and the program cannot continue.", e);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue