Compare commits

...

4 commits

Author SHA1 Message Date
1c48b1861f
Rewrite some error messages for wrong argument amounts 2022-03-12 03:23:28 +01:00
b53e7a617d
Refactor main method to use commons-cli to parse arguments 2022-03-12 03:16:00 +01:00
ddf8047ebd
Disable modularization
The modularization file was heavily interfering with the addition and usage of the commons-cli
dependency which does not have a static module name.

Since the benefits it gave this very small application were miniscule, I have decided to
delete / comment out all relevant configuration
2022-03-12 03:13:30 +01:00
1a25963371
Correct incorrect group for commons-cli dependency 2022-03-12 03:12:58 +01:00
4 changed files with 85 additions and 50 deletions

View file

@ -13,7 +13,7 @@ repositories {
} }
dependencies { dependencies {
implementation(group="org.apache", name="commons-cli", version="1.5.0") implementation(group="commons-cli", name="commons-cli", version="1.5.0")
} }
fun versionBanner(): String { fun versionBanner(): String {
@ -40,8 +40,8 @@ tasks.jar {
attributes( attributes(
"Implementation-Title" to project.name, "Implementation-Title" to project.name,
"Implementation-Version" to project.version, "Implementation-Version" to project.version,
"Main-Class" to "de.jotoho.waituntil.Main", "Main-Class" to "de.jotoho.waituntil.Main"
"Main-Module" to "de.jotoho.waituntil.main" //"Main-Module" to "de.jotoho.waituntil.main"
) )
} }
} }
@ -49,5 +49,5 @@ tasks.jar {
application { application {
// Define the main class for the application. // Define the main class for the application.
mainClass.set("de.jotoho.waituntil.Main") mainClass.set("de.jotoho.waituntil.Main")
mainModule.set("de.jotoho.waituntil.main") //mainModule.set("de.jotoho.waituntil.main")
} }

View 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;
}
}

View file

@ -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,35 +28,10 @@ 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>();
for (final String arg : args) {
if (arg.startsWith("--")) {
options.add(arg.substring(2));
} else if (arg.startsWith("-")) {
if (optionDictionary.containsKey(arg))
options.add(optionDictionary.get(arg).substring(2));
else
System.err.println("Short-hand '$arg' does not exist. Ignoring!");
} else {
words.add(arg);
}
}
if (options.contains("help")) {
switch (applicationOutputLanguage) {
case GlobalConf.langGerman -> System.out.println("Hilfe kommt noch. (Nicht implementiert)");
default -> System.out.println("Help is yet to come. (Not implemented)");
}
} else if (options.contains("version")) {
final var thisPackage = Main.class.getPackage(); final var thisPackage = Main.class.getPackage();
final var appVersion = thisPackage.getImplementationVersion() != null final var appVersion = thisPackage.getImplementationVersion() != null ? thisPackage.getImplementationVersion() : "version unknown";
? thisPackage.getImplementationVersion()
: "version unknown";
System.out.println("waituntil " + appVersion); System.out.println("waituntil " + appVersion);
System.out.println(""" System.out.println("""
@ -67,14 +42,53 @@ public final class Main {
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details."""); See the GNU General Public License for more details.""");
} else if (words.size() == 1) {
final var target = TimeCalculator.calculateAndAnnounceTargetTime(words.iterator().next());
Sleep.waitUntilTimeStamp(target);
} else {
switch (applicationOutputLanguage) {
case GlobalConf.langGerman -> System.err.println("FATAL: Es wurde exact ein nicht-flag Argument erwartet. (" + words.size() + " erhalten)");
default -> System.err.println("FATAL: Expected one non-flag argument. (Got " + words.size() + ")");
} }
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)");
}
}
public static void main(final String[] args) {
try {
final var parsedArguments =
DefaultParser.builder()
.setStripLeadingAndTrailingQuotes(true)
.build()
.parse(AppOptions.getOptions(), args);
final var userData = parsedArguments.getArgs();
if (parsedArguments.hasOption(AppOptions.help)) {
printHelpInformation();
} else if (parsedArguments.hasOption(AppOptions.version)) {
printVersionInformation();
} else if (userData.length == 0) {
switch (applicationOutputLanguage) {
case GlobalConf.langGerman -> System.err.println("FATAL: " +
"Es wurde keine Uhrzeit angegeben.");
default -> System.err.println("FATAL: No target time was " +
"provided.");
}
System.exit(1);
} else if (userData.length > 1) {
switch (applicationOutputLanguage) {
case GlobalConf.langGerman -> System.err.println("FATAL: " +
"Zu viele Argumente wurden angegeben.");
default -> System.err.println("FATAL: Too many arguments " +
"provided.");
}
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);
} }
} }

View file

@ -1,2 +0,0 @@
module de.jotoho.waituntil.main {
}