1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java Mon Nov 17 01:17:02 2014 +0100
1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java Mon Nov 17 17:02:48 2014 +0100
1.3 @@ -18,6 +18,7 @@
1.4 package cz.frantovo.copyImageResizer.cli;
1.5
1.6 import cz.frantovo.copyImageResizer.RecursiveOptions;
1.7 +import cz.frantovo.copyImageResizer.SizeSpecification;
1.8 import java.io.File;
1.9 import java.util.Arrays;
1.10 import java.util.Collection;
1.11 @@ -52,11 +53,21 @@
1.12 return options;
1.13 }
1.14
1.15 - private static String fetchNext(String[] args, int index) throws CLIParserException {
1.16 + /**
1.17 + *
1.18 + * @param args
1.19 + * @param index
1.20 + * @param option name of the option for error messages | if null, the previous argument is used
1.21 + * as option name
1.22 + * @return
1.23 + * @throws CLIParserException
1.24 + */
1.25 + private static String fetchNext(String[] args, int index, String option) throws CLIParserException {
1.26 if (index < args.length) {
1.27 return args[index];
1.28 } else {
1.29 - throw new CLIParserException("Expecting value for option: " + args[index - 1]);
1.30 + option = option == null ? args[index - 1] : option;
1.31 + throw new CLIParserException("Expecting value for option: " + option);
1.32 }
1.33 }
1.34
1.35 @@ -66,7 +77,7 @@
1.36 @Override
1.37 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
1.38 int originalIndex = index;
1.39 - String name = fetchNext(args, ++index);
1.40 + String name = fetchNext(args, ++index, args[originalIndex]);
1.41 options.setInput(new File(name));
1.42 return index - originalIndex;
1.43 }
1.44 @@ -75,7 +86,7 @@
1.45 @Override
1.46 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
1.47 int originalIndex = index;
1.48 - String name = fetchNext(args, ++index);
1.49 + String name = fetchNext(args, ++index, args[originalIndex]);
1.50 options.setOutput(new File(name));
1.51 return index - originalIndex;
1.52 }
1.53 @@ -84,10 +95,11 @@
1.54 @Override
1.55 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
1.56 int originalIndex = index;
1.57 - int width = parseInt(fetchNext(args, ++index));
1.58 - int height = parseInt(fetchNext(args, ++index));
1.59 + int width = parseInt(fetchNext(args, ++index, args[originalIndex]));
1.60 + int height = parseInt(fetchNext(args, ++index, args[originalIndex]));
1.61 + boolean resizeSmaller = parseBoolean(fetchNext(args, ++index, args[originalIndex]));
1.62 String directory = width + "x" + height;
1.63 - options.addSize(new RecursiveOptions.SizeSpecification(width, height, directory));
1.64 + options.addSize(new SizeSpecification(width, height, directory, resizeSmaller));
1.65 return index - originalIndex;
1.66 }
1.67 },
1.68 @@ -95,10 +107,11 @@
1.69 @Override
1.70 public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
1.71 int originalIndex = index;
1.72 - int width = parseInt(fetchNext(args, ++index));
1.73 - int height = parseInt(fetchNext(args, ++index));
1.74 - String directory = fetchNext(args, ++index);
1.75 - options.addSize(new RecursiveOptions.SizeSpecification(width, height, directory));
1.76 + int width = parseInt(fetchNext(args, ++index, args[originalIndex]));
1.77 + int height = parseInt(fetchNext(args, ++index, args[originalIndex]));
1.78 + boolean resizeSmaller = parseBoolean(fetchNext(args, ++index, args[originalIndex]));
1.79 + String directory = fetchNext(args, ++index, args[originalIndex]);
1.80 + options.addSize(new SizeSpecification(width, height, directory, resizeSmaller));
1.81 return index - originalIndex;
1.82 }
1.83 };
1.84 @@ -125,6 +138,17 @@
1.85 }
1.86 }
1.87
1.88 + private static boolean parseBoolean(String value) throws CLIParserException {
1.89 + switch (value) {
1.90 + case "true":
1.91 + return true;
1.92 + case "false":
1.93 + return false;
1.94 + default:
1.95 + throw new CLIParserException("Value „" + value + "“ is not a valid boolean. Expecting „true“ or „false“.");
1.96 + }
1.97 + }
1.98 +
1.99 /**
1.100 * Parse String arguments and fill values into the options object.
1.101 *