Add command line option parsing

This commit is contained in:
Paul Schaub 2018-11-14 20:38:51 +01:00
parent 8420a54c97
commit e91fed4459
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
11 changed files with 501 additions and 0 deletions

View file

@ -0,0 +1,56 @@
package de.vanitasvitae.imi.codes;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
public class Arguments {
static final Option STUDY_NUMBER = Option.builder("s")
.longOpt("study")
.desc("Three-letter code of the associated study")
.required()
.hasArg()
.argName("SNR")
.build();
static final Option SAMPLE_TYPE = Option.builder("t")
.longOpt("type")
.desc("Type of the sample (b = blood, u = urine)")
.required(true)
.hasArg()
.argName("sample type")
.build();
static final Option OUTPUT_DESTINATION = Option.builder("o")
.longOpt("OUTPUT_DESTINATION")
.desc("Filename for the generated html output")
.hasArg()
.argName("file name")
.build();
static final Option EXTERNAL_BROWSER = Option.builder("b")
.longOpt("EXTERNAL_BROWSER")
.desc("Open the generated HTML output in a an external browser")
.build();
static final Option HELP = Option.builder("h")
.longOpt("help")
.desc("Print this help text")
.build();
/**
* Assemble and return the available command line options.
*
* @return command line options.
*/
public static Options getCommandLineOptions() {
Options options = new Options();
options.addOption(STUDY_NUMBER);
options.addOption(SAMPLE_TYPE);
options.addOption(OUTPUT_DESTINATION);
options.addOption(EXTERNAL_BROWSER);
options.addOption(HELP);
return options;
}
}

View file

@ -0,0 +1,47 @@
package de.vanitasvitae.imi.codes;
import java.util.regex.Pattern;
public class InputValidator {
// Allowed characters
private static final String ALPHABET = "[a-zA-Z0-9]";
/*
REGEX pattern for study numbers. Allowed are all 3-letter numbers from the alphabet [a-zA-Z0-9].
*/
private static final Pattern STUDY_NUMBER_MATCHER = Pattern.compile(ALPHABET+"{3}");
/*
REGEX pattern for sample numbers. Allowed are four-letter numbers from the alphabet [a-zA-Z0-9].
*/
private static final Pattern SAMPLE_NUMBER_MATCHER = Pattern.compile(ALPHABET+"{4}");
/**
* Validate a given study number. Valid study numbers are 3-letter codes from the alphabet [a-zA-Z0-9].
*
* @param in input String
* @return unmodified input String if it matches.
* @throws IllegalArgumentException if the input String doesn't match the regex.
*/
public static String validateStudyNumber(String in) {
if (!STUDY_NUMBER_MATCHER.matcher(in).matches()) {
throw new IllegalArgumentException("Study number does not match REGEX.");
}
return in;
}
/**
* Validate a given sample type. Valid types are found in {@link SampleType}.#
*
* @param in input String
* @return parsed {@link SampleType}.
*
* @throws IllegalArgumentException if the given String doesn't match a {@link SampleType}.
* @throws NullPointerException in case the given String is {@code null}.
*/
public static SampleType validateSampleType(String in) {
return SampleType.valueOf(in);
}
}

View file

@ -0,0 +1,48 @@
package de.vanitasvitae.imi.codes;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
public class Main {
private static final String NAME_JAR = "imicodes";
private static final String HELP_HEADER = "Generate ID codes for sample tubes.";
private static final String HELP_FOOTER = "\nAuthor: Paul Schaub <paul.schaub@wwu.de>";
public static void main(String[] args) {
Options options = Arguments.getCommandLineOptions();
CommandLineParser parser = new DefaultParser();
CommandLine arguments;
try {
arguments = parser.parse(options, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
printHelp(options);
return;
}
if (arguments.hasOption(Arguments.HELP)) {
printHelp(options);
return;
}
}
/**
* Print a descriptive help text to the console.
*
* @param options {@link Options} for which we want to print a help text.
*/
private static void printHelp(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(NAME_JAR, HELP_HEADER, options, HELP_FOOTER, true);
}
}

View file

@ -0,0 +1,20 @@
package de.vanitasvitae.imi.codes;
public enum SampleType {
b("Blood"),
u("Urine"),
;
// Members of the enum
private final String name;
SampleType(String name) {
this.name = name;
}
public String getName() {
return name;
}
}