java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 16 Nov 2014 21:59:13 +0100
changeset 4 f3b4caf1d05d
parent 3 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParser.java@e417b37e6c08
child 5 5019e3e93a4e
permissions -rw-r--r--
move to java package: cli
franta-hg@3
     1
/**
franta-hg@3
     2
 * copy-image-resizer
franta-hg@3
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@3
     4
 *
franta-hg@3
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@3
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@3
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@3
     8
 * (at your option) any later version.
franta-hg@3
     9
 *
franta-hg@3
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@3
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@3
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@3
    13
 * GNU General Public License for more details.
franta-hg@3
    14
 *
franta-hg@3
    15
 * You should have received a copy of the GNU General Public License
franta-hg@3
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@3
    17
 */
franta-hg@4
    18
package cz.frantovo.copyImageResizer.cli;
franta-hg@3
    19
franta-hg@3
    20
import java.io.File;
franta-hg@3
    21
import java.util.Arrays;
franta-hg@3
    22
import java.util.Collection;
franta-hg@3
    23
franta-hg@3
    24
/**
franta-hg@3
    25
 *
franta-hg@3
    26
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@3
    27
 */
franta-hg@3
    28
public class CLIParser {
franta-hg@3
    29
franta-hg@3
    30
	public CLIOptions parseOptions(String[] args) throws CLIParserException {
franta-hg@3
    31
		CLIOptions options = new CLIOptions();
franta-hg@3
    32
franta-hg@3
    33
		for (int i = 0; i < args.length; i++) {
franta-hg@3
    34
			String arg = args[i];
franta-hg@3
    35
franta-hg@3
    36
			boolean matches = false;
franta-hg@3
    37
franta-hg@3
    38
			for (Token t : Token.values()) {
franta-hg@3
    39
				if (t.matches(arg)) {
franta-hg@3
    40
					int parsedArgs = t.parse(args, i, options);
franta-hg@3
    41
					i = i + parsedArgs;
franta-hg@3
    42
					matches = true;
franta-hg@3
    43
				}
franta-hg@3
    44
			}
franta-hg@3
    45
franta-hg@3
    46
			if (!matches) {
franta-hg@3
    47
				throw new CLIParserException("Unknown option: " + arg);
franta-hg@3
    48
			}
franta-hg@3
    49
		}
franta-hg@3
    50
franta-hg@3
    51
		return options;
franta-hg@3
    52
	}
franta-hg@3
    53
franta-hg@3
    54
	private static String fetchNext(String[] args, int index) throws CLIParserException {
franta-hg@3
    55
		if (index < args.length) {
franta-hg@3
    56
			return args[index];
franta-hg@3
    57
		} else {
franta-hg@3
    58
			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
franta-hg@3
    59
		}
franta-hg@3
    60
	}
franta-hg@3
    61
franta-hg@3
    62
	private static enum Token {
franta-hg@3
    63
franta-hg@3
    64
		INPUT_DIR("--input-dir") { // bash-completion: option
franta-hg@3
    65
					@Override
franta-hg@3
    66
					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
franta-hg@3
    67
						int originalIndex = index;
franta-hg@3
    68
						String name = fetchNext(args, ++index);
franta-hg@3
    69
						options.setInput(new File(name));
franta-hg@3
    70
						return index - originalIndex;
franta-hg@3
    71
					}
franta-hg@3
    72
				},
franta-hg@3
    73
		OUTPUT_DIR("--output-dir") { // bash-completion: option
franta-hg@3
    74
					@Override
franta-hg@3
    75
					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
franta-hg@3
    76
						int originalIndex = index;
franta-hg@3
    77
						String name = fetchNext(args, ++index);
franta-hg@3
    78
						options.setOutput(new File(name));
franta-hg@3
    79
						return index - originalIndex;
franta-hg@3
    80
					}
franta-hg@3
    81
				},
franta-hg@3
    82
		SIZE("--size") { // bash-completion: option
franta-hg@3
    83
					@Override
franta-hg@3
    84
					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
franta-hg@3
    85
						int originalIndex = index;
franta-hg@3
    86
						int width = parseInt(fetchNext(args, ++index));
franta-hg@3
    87
						int height = parseInt(fetchNext(args, ++index));
franta-hg@3
    88
						String directory = width + "x" + height;
franta-hg@3
    89
						options.addSize(new CLIOptions.SizeSpecification(width, height, directory));
franta-hg@3
    90
						return index - originalIndex;
franta-hg@3
    91
					}
franta-hg@3
    92
				},
franta-hg@3
    93
		SIZE_NAMED("--size-named") { // bash-completion: option
franta-hg@3
    94
					@Override
franta-hg@3
    95
					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
franta-hg@3
    96
						int originalIndex = index;
franta-hg@3
    97
						int width = parseInt(fetchNext(args, ++index));
franta-hg@3
    98
						int height = parseInt(fetchNext(args, ++index));
franta-hg@3
    99
						String directory = fetchNext(args, ++index);
franta-hg@3
   100
						options.addSize(new CLIOptions.SizeSpecification(width, height, directory));
franta-hg@3
   101
						return index - originalIndex;
franta-hg@3
   102
					}
franta-hg@3
   103
				};
franta-hg@3
   104
franta-hg@3
   105
		private final Collection<String> options;
franta-hg@3
   106
franta-hg@3
   107
		private Token(String... options) {
franta-hg@3
   108
			this.options = Arrays.asList(options);
franta-hg@3
   109
		}
franta-hg@3
   110
franta-hg@3
   111
		/**
franta-hg@3
   112
		 * @param option e.g. „--input-file“
franta-hg@3
   113
		 * @return whether option is this token
franta-hg@3
   114
		 */
franta-hg@3
   115
		public boolean matches(String option) {
franta-hg@3
   116
			return options.contains(option);
franta-hg@3
   117
		}
franta-hg@3
   118
franta-hg@3
   119
		private static int parseInt(String number) throws CLIParserException {
franta-hg@3
   120
			try {
franta-hg@3
   121
				return Integer.parseInt(number);
franta-hg@3
   122
			} catch (Exception e) {
franta-hg@3
   123
				throw new CLIParserException("Unable to parse integer: " + number, e);
franta-hg@3
   124
			}
franta-hg@3
   125
		}
franta-hg@3
   126
franta-hg@3
   127
		/**
franta-hg@3
   128
		 * Parse String arguments and fill values into the options object.
franta-hg@3
   129
		 *
franta-hg@3
   130
		 * @param args CLI arguments
franta-hg@3
   131
		 * @param index index of the option matched by this token, like „--input-file“
franta-hg@3
   132
		 * @param options object to be filled
franta-hg@3
   133
		 * @return number of parsed arguments – if option has no arguments (just boolean flag),
franta-hg@3
   134
		 * return 0, otherwise return positive integer: number of eaten arguments.
franta-hg@3
   135
		 * @throws CLIParserException
franta-hg@3
   136
		 */
franta-hg@3
   137
		public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;
franta-hg@3
   138
	}
franta-hg@3
   139
}