Skip to content
Snippets Groups Projects
Commit 6a52ab9d authored by Leonard Kupper's avatar Leonard Kupper
Browse files

Fix missing options for single -h parameter.

parent a6ecdff0
No related branches found
No related tags found
1 merge request!39App restructuring
......@@ -54,7 +54,6 @@ import java.io.Reader;
import java.net.URL;
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedDeque;
import static tec.units.ri.unit.Units.METRE;
......@@ -62,8 +61,8 @@ import static tec.units.ri.unit.Units.METRE;
/**
* Main class.
* Set up the application and run it.
* @author Georg Graßnick, Andrey Ruzhanskiy
* @version 2019.09.30
* @author Georg Graßnick, Andrey Ruzhanskiy, Leonard Kupper
* @version 2019.10.02
*/
public final class App {
......@@ -161,16 +160,13 @@ public final class App {
mLogger.info("Application started");
// Parse command line parameters
CommandLineParser cliParser = new CommandLineParser();
SettingsWriter settings = cliParser.parse(args);
SettingsReader settingsReader = settings;
// If requested, print help and exit
Optional<Boolean> printHelp = settingsReader.isTrue(SettingType.DISPLAY_HELP);
if (printHelp.isPresent() && printHelp.get()) {
if (CommandLineParser.checkForHelp(args)) {
// If requested, print help and exit
cliParser.printHelp();
return EXIT_SUCCESS;
}
SettingsWriter settings = cliParser.parse(args);
SettingsReader settingsReader = settings;
// Config Parsing
JavaPropertiesConfigurationParser configParser;
......
......@@ -3,11 +3,11 @@ package de.tudresden.inf.mci.brailleplot.commandline;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
/**
......@@ -19,13 +19,20 @@ public class CommandLineParser {
private Options mOptions;
public static Option helpOption = Option.builder("h")
.longOpt("help")
.required(false)
.hasArg(false)
.desc("Print help and exit")
.build();
public CommandLineParser() {
setupOptions();
}
private void setupOptions() {
mOptions = new Options();
mOptions.addOption("h", SettingType.DISPLAY_HELP.toString(), false, "Print help and exit")
mOptions.addOption(helpOption)
.addRequiredOption("c", SettingType.CSV_LOCATION.toString(), true, "Path to CSV")
.addRequiredOption("p", SettingType.PRINTER_CONFIG_PATH.toString(), true, "Path to printer configuration file")
.addRequiredOption("t", SettingType.DIAGRAM_TITLE.toString(), true, "Title of the diagram")
......@@ -57,6 +64,25 @@ public class CommandLineParser {
return new Settings(cmdLine);
}
public static boolean checkForHelp(final String[] args) {
boolean hasHelp = false;
Options options = new Options();
options.addOption(helpOption);
org.apache.commons.cli.CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
// Will occur if any other option than "help" is encountered
// For this case we can safely ignore it.
}
if (Objects.nonNull(cmd) && cmd.hasOption(helpOption.getOpt())) {
hasHelp = true;
}
return hasHelp;
}
/**
* Print usage information to the command line.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment