
morfologik.tools.CliTool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of morfologik-tools Show documentation
Show all versions of morfologik-tools Show documentation
Morfologik Command Line Tools
package morfologik.tools;
import java.io.PrintStream;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.MissingCommandException;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
/**
* Base class for command-line applications.
*/
public abstract class CliTool implements Callable {
protected final static String ARG_OVERWRITE = "--overwrite";
protected final static String ARG_VALIDATE = "--validate";
@Parameter(
names = {"--exit"},
hidden = true,
arity = 1,
description = "Call System.exit() at the end of command processing.")
private boolean callSystemExit = true;
@Parameter(
names = {"-h", "--help"},
help = true,
hidden = true,
description = "Help about options and switches.")
private boolean help;
public CliTool() {
if (!getClass().isAnnotationPresent(Parameters.class)) {
throw new RuntimeException();
}
}
/**
* Call {@link System#exit(int)} at the end of command processing.
*
* @param flag Call {@link System#exit(int)} if true
.
*/
public void setCallSystemExit(boolean flag) {
this.callSystemExit = flag;
}
/**
* Parse and execute one of the commands.
*
* @param args Command line arguments (command and options).
* @param commands A list of commands.
*/
protected static void main(String[] args, CliTool... commands) {
if (commands.length == 1) {
main(args, commands[0]);
} else {
JCommander jc = new JCommander();
for (CliTool command : commands) {
jc.addCommand(command);
}
jc.addConverterFactory(new CustomParameterConverters());
jc.setProgramName("");
ExitStatus exitStatus = ExitStatus.SUCCESS;
try {
jc.parse(args);
final String commandName = jc.getParsedCommand();
if (commandName == null) {
helpDisplayCommandOptions(System.err, jc);
} else {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy