java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java
changeset 4 f3b4caf1d05d
parent 3 e417b37e6c08
child 5 5019e3e93a4e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java	Sun Nov 16 21:59:13 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/**
     1.5 + * copy-image-resizer
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.copyImageResizer.cli;
    1.22 +
    1.23 +import java.io.File;
    1.24 +import java.util.Arrays;
    1.25 +import java.util.Collection;
    1.26 +
    1.27 +/**
    1.28 + *
    1.29 + * @author Ing. František Kučera (frantovo.cz)
    1.30 + */
    1.31 +public class CLIParser {
    1.32 +
    1.33 +	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    1.34 +		CLIOptions options = new CLIOptions();
    1.35 +
    1.36 +		for (int i = 0; i < args.length; i++) {
    1.37 +			String arg = args[i];
    1.38 +
    1.39 +			boolean matches = false;
    1.40 +
    1.41 +			for (Token t : Token.values()) {
    1.42 +				if (t.matches(arg)) {
    1.43 +					int parsedArgs = t.parse(args, i, options);
    1.44 +					i = i + parsedArgs;
    1.45 +					matches = true;
    1.46 +				}
    1.47 +			}
    1.48 +
    1.49 +			if (!matches) {
    1.50 +				throw new CLIParserException("Unknown option: " + arg);
    1.51 +			}
    1.52 +		}
    1.53 +
    1.54 +		return options;
    1.55 +	}
    1.56 +
    1.57 +	private static String fetchNext(String[] args, int index) throws CLIParserException {
    1.58 +		if (index < args.length) {
    1.59 +			return args[index];
    1.60 +		} else {
    1.61 +			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
    1.62 +		}
    1.63 +	}
    1.64 +
    1.65 +	private static enum Token {
    1.66 +
    1.67 +		INPUT_DIR("--input-dir") { // bash-completion: option
    1.68 +					@Override
    1.69 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.70 +						int originalIndex = index;
    1.71 +						String name = fetchNext(args, ++index);
    1.72 +						options.setInput(new File(name));
    1.73 +						return index - originalIndex;
    1.74 +					}
    1.75 +				},
    1.76 +		OUTPUT_DIR("--output-dir") { // bash-completion: option
    1.77 +					@Override
    1.78 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.79 +						int originalIndex = index;
    1.80 +						String name = fetchNext(args, ++index);
    1.81 +						options.setOutput(new File(name));
    1.82 +						return index - originalIndex;
    1.83 +					}
    1.84 +				},
    1.85 +		SIZE("--size") { // bash-completion: option
    1.86 +					@Override
    1.87 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.88 +						int originalIndex = index;
    1.89 +						int width = parseInt(fetchNext(args, ++index));
    1.90 +						int height = parseInt(fetchNext(args, ++index));
    1.91 +						String directory = width + "x" + height;
    1.92 +						options.addSize(new CLIOptions.SizeSpecification(width, height, directory));
    1.93 +						return index - originalIndex;
    1.94 +					}
    1.95 +				},
    1.96 +		SIZE_NAMED("--size-named") { // bash-completion: option
    1.97 +					@Override
    1.98 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.99 +						int originalIndex = index;
   1.100 +						int width = parseInt(fetchNext(args, ++index));
   1.101 +						int height = parseInt(fetchNext(args, ++index));
   1.102 +						String directory = fetchNext(args, ++index);
   1.103 +						options.addSize(new CLIOptions.SizeSpecification(width, height, directory));
   1.104 +						return index - originalIndex;
   1.105 +					}
   1.106 +				};
   1.107 +
   1.108 +		private final Collection<String> options;
   1.109 +
   1.110 +		private Token(String... options) {
   1.111 +			this.options = Arrays.asList(options);
   1.112 +		}
   1.113 +
   1.114 +		/**
   1.115 +		 * @param option e.g. „--input-file“
   1.116 +		 * @return whether option is this token
   1.117 +		 */
   1.118 +		public boolean matches(String option) {
   1.119 +			return options.contains(option);
   1.120 +		}
   1.121 +
   1.122 +		private static int parseInt(String number) throws CLIParserException {
   1.123 +			try {
   1.124 +				return Integer.parseInt(number);
   1.125 +			} catch (Exception e) {
   1.126 +				throw new CLIParserException("Unable to parse integer: " + number, e);
   1.127 +			}
   1.128 +		}
   1.129 +
   1.130 +		/**
   1.131 +		 * Parse String arguments and fill values into the options object.
   1.132 +		 *
   1.133 +		 * @param args CLI arguments
   1.134 +		 * @param index index of the option matched by this token, like „--input-file“
   1.135 +		 * @param options object to be filled
   1.136 +		 * @return number of parsed arguments – if option has no arguments (just boolean flag),
   1.137 +		 * return 0, otherwise return positive integer: number of eaten arguments.
   1.138 +		 * @throws CLIParserException
   1.139 +		 */
   1.140 +		public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;
   1.141 +	}
   1.142 +}