Getting Started
(work in progress)
1. First Steps
Consider this, you wrote a piece of code that allows you to convert all sorts of image files into one another.
Even more, it allows you to scale the resulting image in the process.
Now you figured, it would be nice to put this piece of code in a standalone command-line application.
So you create an application-skeleton like this:
public class ImageConverter{
private String inputFile;
private String outputFile ;
private Integer s;
private Integer t;
public ImageConverter(String[] args) {
// parse arguments
}
public void convert() {
convertImpl(inputFile, outputFile, s, t);
}
private void convertImpl(String inputFile, String outputFile, Integer s, Integer t) {
// ...
}
}
public class Main {
public static void main(String[] args) {
ImageConverter app = new ImageConverter(args);
app.convert();
}
}
|
2. Setting up a CommandLineParser
Pretty much the same as in JArgs, so I will cover this later.
3. Annotation-based configuration
@CommandLineApplication(appNameFromJar = true, enableHelp = true, showUsageOnExeption = true)
public class ImageConverter{
@CommandLineOption(shortForm = "i", longForm = "input", description = "inputfile")
private String inputFile = "";
@CommandLineOption(shortForm = "o", longForm = "output", description = "outputfile")
private String outputFile = "";
@CommandLineOption(shortForm = "s", longForm = "s", description = "(optional) image height")
@CommandLineValidator(min = "0")
private Integer s;
@CommandLineOption(shortForm = "t", longForm = "t", description = "(optional) image width")
@CommandLineValidator(min = "0")
private Integer t;
private ImageConverter() {
}
public void convert() {
convertImpl(inputFile, outputFile, s, t);
}
private void convertImpl(String inputFile, String outputFile, Integer s, Integer t) {
// ...
}
}
public class Main {
public static void main(String[] args) {
CommandLineApplicationParser<ImageConverter> clap = CommandLineApplicationParser.of(ImageConverter.class);
try {
ImageConverter app = clap.parse(args);
if (!clap.helpRequested()) {
app.convert();
}
} catch (Exception ignore) {
}
}
}
|