Merge "Switch to use commandline parser"

This commit is contained in:
Tianjie Xu
2018-10-29 22:23:31 +00:00
committed by Gerrit Code Review
3 changed files with 72 additions and 15 deletions
+5 -1
View File
@@ -17,7 +17,11 @@ java_library_host {
manifest: "ImageGenerator.mf", manifest: "ImageGenerator.mf",
static_libs: [
"commons-cli-1.2",
],
srcs: [ srcs: [
"ImageGenerator.java", "ImageGenerator.java",
], ],
} }
+65 -13
View File
@@ -38,6 +38,13 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
@@ -367,28 +374,73 @@ public class ImageGenerator {
ImageIO.write(mBufferedImage, "png", new File(outputPath)); ImageIO.write(mBufferedImage, "png", new File(outputPath));
} }
public static void printUsage() { public static void printUsage(Options options) {
System.out.println("Usage: java -jar path_to_jar imageWidth textName fontDirectory" new HelpFormatter().printHelp("java -jar path_to_jar [required_options]", options);
+ " resourceDirectory outputFilename");
}
public static Options createOptions() {
Options options = new Options();
options.addOption(OptionBuilder
.withLongOpt("image_width")
.withDescription("The initial width of the image in pixels.")
.hasArgs(1)
.isRequired()
.create());
options.addOption(OptionBuilder
.withLongOpt("text_name")
.withDescription("The description of the text string, e.g. recovery_erasing")
.hasArgs(1)
.isRequired()
.create());
options.addOption(OptionBuilder
.withLongOpt("font_dir")
.withDescription("The directory that contains all the support font format files, e.g."
+ " $OUT/system/fonts/")
.hasArgs(1)
.isRequired()
.create());
options.addOption(OptionBuilder
.withLongOpt("resource_dir")
.withDescription("The resource directory that contains all the translated strings in xml"
+ " format, e.g. bootable/recovery/tools/recovery_l10n/res/")
.hasArgs(1)
.isRequired()
.create());
options.addOption(OptionBuilder
.withLongOpt("output_file")
.withDescription("Path to the generated image")
.hasArgs(1)
.isRequired()
.create());
return options;
} }
public static void main(String[] args) throws NumberFormatException, IOException, public static void main(String[] args) throws NumberFormatException, IOException,
FontFormatException, LocalizedStringNotFoundException { FontFormatException, LocalizedStringNotFoundException {
if (args.length != 5) { Options options = createOptions();
printUsage(); CommandLine cmd;
System.err.println("We expect 5 arguments, get " + args.length); try {
System.exit(1); cmd = new GnuParser().parse(options, args);
} catch (ParseException e) {
System.err.println(e.getMessage());
printUsage(options);
return;
} }
// TODO(xunchang) switch to commandline parser int imageWidth = Integer.parseUnsignedInt(cmd.getOptionValue("image_width"));
int imageWidth = Integer.parseUnsignedInt(args[0]);
ImageGenerator imageGenerator = ImageGenerator imageGenerator = new ImageGenerator(imageWidth, cmd.getOptionValue("text_name"),
new ImageGenerator(imageWidth, args[1], DEFAULT_FONT_SIZE, args[2]); DEFAULT_FONT_SIZE, cmd.getOptionValue("font_dir"));
Map<Locale, String> localizedStringMap = Map<Locale, String> localizedStringMap =
imageGenerator.readLocalizedStringFromXmls(args[3]); imageGenerator.readLocalizedStringFromXmls(cmd.getOptionValue("resource_dir"));
imageGenerator.generateImage(localizedStringMap, args[4]); imageGenerator.generateImage(localizedStringMap, cmd.getOptionValue("output_file"));
} }
} }
+2 -1
View File
@@ -6,7 +6,8 @@ under recovery mode. And thus we don't need to do the manual work by running
emulators with different dpi. emulators with different dpi.
# Usage: # Usage:
`java -jar path_to_jar imageWidth textName fontDirectory resourceDirectory outputFilename` `java -jar path_to_jar --image_width imageWidth --text_name textName --font_dir fontDirectory
--resource_dir resourceDirectory --output_file outputFilename`
# Description of the parameters: # Description of the parameters:
1. `imageWidth`: The number of pixels per line; and the text strings will be 1. `imageWidth`: The number of pixels per line; and the text strings will be