options, parser
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 16 Nov 2014 21:57:03 +0100
changeset 3e417b37e6c08
parent 2 0568271a1174
child 4 f3b4caf1d05d
options, parser
.hgignore
java/copy-image-resizer/nbproject/private/private.xml
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIOptions.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParser.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParserException.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIResizer.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIStarter.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CopyImageResizerException.java
     1.1 --- a/.hgignore	Sun Nov 16 19:41:41 2014 +0100
     1.2 +++ b/.hgignore	Sun Nov 16 21:57:03 2014 +0100
     1.3 @@ -4,4 +4,4 @@
     1.4  
     1.5  syntax: regexp
     1.6  
     1.7 -^java/copy-image-resizer/(dist|build|private)/
     1.8 +^java/copy-image-resizer/(dist|build|nbproject/private)/
     2.1 --- a/java/copy-image-resizer/nbproject/private/private.xml	Sun Nov 16 19:41:41 2014 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,9 +0,0 @@
     2.4 -<?xml version="1.0" encoding="UTF-8"?>
     2.5 -<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
     2.6 -    <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
     2.7 -    <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
     2.8 -        <group name="arbes">
     2.9 -            <file>file:/home/fiki/projekty/copy-image-resizer/copy-image-resizer/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIResizer.java</file>
    2.10 -        </group>
    2.11 -    </open-files>
    2.12 -</project-private>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIOptions.java	Sun Nov 16 21:57:03 2014 +0100
     3.3 @@ -0,0 +1,91 @@
     3.4 +/**
     3.5 + * copy-image-resizer
     3.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, either version 3 of the License, or
    3.11 + * (at your option) any later version.
    3.12 + *
    3.13 + * This program is distributed in the hope that it will be useful,
    3.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    3.16 + * GNU General Public License for more details.
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License
    3.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    3.20 + */
    3.21 +package cz.frantovo.copyImageResizer;
    3.22 +
    3.23 +import java.io.File;
    3.24 +import java.util.ArrayList;
    3.25 +import java.util.Collection;
    3.26 +
    3.27 +/**
    3.28 + *
    3.29 + * @author Ing. František Kučera (frantovo.cz)
    3.30 + */
    3.31 +public class CLIOptions {
    3.32 +
    3.33 +	/**
    3.34 +	 * Source directory
    3.35 +	 */
    3.36 +	private File input;
    3.37 +	/**
    3.38 +	 * Output directory
    3.39 +	 */
    3.40 +	private File output;
    3.41 +	/**
    3.42 +	 * Image resolutions to create
    3.43 +	 */
    3.44 +	private final Collection<SizeSpecification> sizes = new ArrayList<>();
    3.45 +
    3.46 +	public File getSourceDir() {
    3.47 +		return input;
    3.48 +	}
    3.49 +
    3.50 +	public void setInput(File input) {
    3.51 +		this.input = input;
    3.52 +	}
    3.53 +
    3.54 +	public File getDestinationDir() {
    3.55 +		return output;
    3.56 +	}
    3.57 +
    3.58 +	public void setOutput(File output) {
    3.59 +		this.output = output;
    3.60 +	}
    3.61 +
    3.62 +	public Collection<SizeSpecification> getSizes() {
    3.63 +		return sizes;
    3.64 +	}
    3.65 +
    3.66 +	public void addSize(SizeSpecification size) {
    3.67 +		sizes.add(size);
    3.68 +	}
    3.69 +
    3.70 +	public static class SizeSpecification {
    3.71 +
    3.72 +		private final int width;
    3.73 +		private final int height;
    3.74 +		private final String directory;
    3.75 +
    3.76 +		public SizeSpecification(int width, int height, String directory) {
    3.77 +			this.width = width;
    3.78 +			this.height = height;
    3.79 +			this.directory = directory;
    3.80 +		}
    3.81 +
    3.82 +		public int getWidth() {
    3.83 +			return width;
    3.84 +		}
    3.85 +
    3.86 +		public int getHeight() {
    3.87 +			return height;
    3.88 +		}
    3.89 +
    3.90 +		public String getDirectory() {
    3.91 +			return directory;
    3.92 +		}
    3.93 +	}
    3.94 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParser.java	Sun Nov 16 21:57:03 2014 +0100
     4.3 @@ -0,0 +1,139 @@
     4.4 +/**
     4.5 + * copy-image-resizer
     4.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, either version 3 of the License, or
    4.11 + * (at your option) any later version.
    4.12 + *
    4.13 + * This program is distributed in the hope that it will be useful,
    4.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    4.16 + * GNU General Public License for more details.
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License
    4.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    4.20 + */
    4.21 +package cz.frantovo.copyImageResizer;
    4.22 +
    4.23 +import java.io.File;
    4.24 +import java.util.Arrays;
    4.25 +import java.util.Collection;
    4.26 +
    4.27 +/**
    4.28 + *
    4.29 + * @author Ing. František Kučera (frantovo.cz)
    4.30 + */
    4.31 +public class CLIParser {
    4.32 +
    4.33 +	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    4.34 +		CLIOptions options = new CLIOptions();
    4.35 +
    4.36 +		for (int i = 0; i < args.length; i++) {
    4.37 +			String arg = args[i];
    4.38 +
    4.39 +			boolean matches = false;
    4.40 +
    4.41 +			for (Token t : Token.values()) {
    4.42 +				if (t.matches(arg)) {
    4.43 +					int parsedArgs = t.parse(args, i, options);
    4.44 +					i = i + parsedArgs;
    4.45 +					matches = true;
    4.46 +				}
    4.47 +			}
    4.48 +
    4.49 +			if (!matches) {
    4.50 +				throw new CLIParserException("Unknown option: " + arg);
    4.51 +			}
    4.52 +		}
    4.53 +
    4.54 +		return options;
    4.55 +	}
    4.56 +
    4.57 +	private static String fetchNext(String[] args, int index) throws CLIParserException {
    4.58 +		if (index < args.length) {
    4.59 +			return args[index];
    4.60 +		} else {
    4.61 +			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
    4.62 +		}
    4.63 +	}
    4.64 +
    4.65 +	private static enum Token {
    4.66 +
    4.67 +		INPUT_DIR("--input-dir") { // bash-completion: option
    4.68 +					@Override
    4.69 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    4.70 +						int originalIndex = index;
    4.71 +						String name = fetchNext(args, ++index);
    4.72 +						options.setInput(new File(name));
    4.73 +						return index - originalIndex;
    4.74 +					}
    4.75 +				},
    4.76 +		OUTPUT_DIR("--output-dir") { // bash-completion: option
    4.77 +					@Override
    4.78 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    4.79 +						int originalIndex = index;
    4.80 +						String name = fetchNext(args, ++index);
    4.81 +						options.setOutput(new File(name));
    4.82 +						return index - originalIndex;
    4.83 +					}
    4.84 +				},
    4.85 +		SIZE("--size") { // bash-completion: option
    4.86 +					@Override
    4.87 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    4.88 +						int originalIndex = index;
    4.89 +						int width = parseInt(fetchNext(args, ++index));
    4.90 +						int height = parseInt(fetchNext(args, ++index));
    4.91 +						String directory = width + "x" + height;
    4.92 +						options.addSize(new CLIOptions.SizeSpecification(width, height, directory));
    4.93 +						return index - originalIndex;
    4.94 +					}
    4.95 +				},
    4.96 +		SIZE_NAMED("--size-named") { // bash-completion: option
    4.97 +					@Override
    4.98 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    4.99 +						int originalIndex = index;
   4.100 +						int width = parseInt(fetchNext(args, ++index));
   4.101 +						int height = parseInt(fetchNext(args, ++index));
   4.102 +						String directory = fetchNext(args, ++index);
   4.103 +						options.addSize(new CLIOptions.SizeSpecification(width, height, directory));
   4.104 +						return index - originalIndex;
   4.105 +					}
   4.106 +				};
   4.107 +
   4.108 +		private final Collection<String> options;
   4.109 +
   4.110 +		private Token(String... options) {
   4.111 +			this.options = Arrays.asList(options);
   4.112 +		}
   4.113 +
   4.114 +		/**
   4.115 +		 * @param option e.g. „--input-file“
   4.116 +		 * @return whether option is this token
   4.117 +		 */
   4.118 +		public boolean matches(String option) {
   4.119 +			return options.contains(option);
   4.120 +		}
   4.121 +
   4.122 +		private static int parseInt(String number) throws CLIParserException {
   4.123 +			try {
   4.124 +				return Integer.parseInt(number);
   4.125 +			} catch (Exception e) {
   4.126 +				throw new CLIParserException("Unable to parse integer: " + number, e);
   4.127 +			}
   4.128 +		}
   4.129 +
   4.130 +		/**
   4.131 +		 * Parse String arguments and fill values into the options object.
   4.132 +		 *
   4.133 +		 * @param args CLI arguments
   4.134 +		 * @param index index of the option matched by this token, like „--input-file“
   4.135 +		 * @param options object to be filled
   4.136 +		 * @return number of parsed arguments – if option has no arguments (just boolean flag),
   4.137 +		 * return 0, otherwise return positive integer: number of eaten arguments.
   4.138 +		 * @throws CLIParserException
   4.139 +		 */
   4.140 +		public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;
   4.141 +	}
   4.142 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParserException.java	Sun Nov 16 21:57:03 2014 +0100
     5.3 @@ -0,0 +1,41 @@
     5.4 +/**
     5.5 + * copy-image-resizer
     5.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, either version 3 of the License, or
    5.11 + * (at your option) any later version.
    5.12 + *
    5.13 + * This program is distributed in the hope that it will be useful,
    5.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    5.16 + * GNU General Public License for more details.
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License
    5.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    5.20 + */
    5.21 +package cz.frantovo.copyImageResizer;
    5.22 +
    5.23 +/**
    5.24 + *
    5.25 + * @author Ing. František Kučera (frantovo.cz)
    5.26 + */
    5.27 +public class CLIParserException extends CopyImageResizerException {
    5.28 +
    5.29 +	public CLIParserException() {
    5.30 +	}
    5.31 +
    5.32 +	public CLIParserException(String message) {
    5.33 +		super(message);
    5.34 +	}
    5.35 +
    5.36 +	public CLIParserException(Throwable cause) {
    5.37 +		super(cause);
    5.38 +	}
    5.39 +
    5.40 +	public CLIParserException(String message, Throwable cause) {
    5.41 +		super(message, cause);
    5.42 +	}
    5.43 +
    5.44 +}
     6.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIResizer.java	Sun Nov 16 19:41:41 2014 +0100
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,34 +0,0 @@
     6.4 -/**
     6.5 - * copy-image-resizer
     6.6 - * Copyright © 2014 František Kučera (frantovo.cz)
     6.7 - *
     6.8 - * This program is free software: you can redistribute it and/or modify
     6.9 - * it under the terms of the GNU General Public License as published by
    6.10 - * the Free Software Foundation, either version 3 of the License, or
    6.11 - * (at your option) any later version.
    6.12 - *
    6.13 - * This program is distributed in the hope that it will be useful,
    6.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    6.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    6.16 - * GNU General Public License for more details.
    6.17 - *
    6.18 - * You should have received a copy of the GNU General Public License
    6.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
    6.20 - */
    6.21 -package cz.frantovo.copyImageResizer;
    6.22 -
    6.23 -/**
    6.24 - *
    6.25 - * @author Ing. František Kučera (frantovo.cz)
    6.26 - */
    6.27 -public class CLIResizer {
    6.28 -
    6.29 -	/**
    6.30 -	 * @param args the command line arguments
    6.31 -	 */
    6.32 -	public static void main(String[] args) {
    6.33 -		// TODO code application logic here
    6.34 -		System.out.println("TODO");
    6.35 -	}
    6.36 -	
    6.37 -}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIStarter.java	Sun Nov 16 21:57:03 2014 +0100
     7.3 @@ -0,0 +1,34 @@
     7.4 +/**
     7.5 + * copy-image-resizer
     7.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     7.7 + *
     7.8 + * This program is free software: you can redistribute it and/or modify
     7.9 + * it under the terms of the GNU General Public License as published by
    7.10 + * the Free Software Foundation, either version 3 of the License, or
    7.11 + * (at your option) any later version.
    7.12 + *
    7.13 + * This program is distributed in the hope that it will be useful,
    7.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    7.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    7.16 + * GNU General Public License for more details.
    7.17 + *
    7.18 + * You should have received a copy of the GNU General Public License
    7.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    7.20 + */
    7.21 +package cz.frantovo.copyImageResizer;
    7.22 +
    7.23 +/**
    7.24 + *
    7.25 + * @author Ing. František Kučera (frantovo.cz)
    7.26 + */
    7.27 +public class CLIStarter {
    7.28 +
    7.29 +	/**
    7.30 +	 * @param args the command line arguments
    7.31 +	 */
    7.32 +	public static void main(String[] args) {
    7.33 +		// TODO code application logic here
    7.34 +		System.out.println("TODO");
    7.35 +	}
    7.36 +	
    7.37 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CopyImageResizerException.java	Sun Nov 16 21:57:03 2014 +0100
     8.3 @@ -0,0 +1,41 @@
     8.4 +/**
     8.5 + * copy-image-resizer
     8.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     8.7 + *
     8.8 + * This program is free software: you can redistribute it and/or modify
     8.9 + * it under the terms of the GNU General Public License as published by
    8.10 + * the Free Software Foundation, either version 3 of the License, or
    8.11 + * (at your option) any later version.
    8.12 + *
    8.13 + * This program is distributed in the hope that it will be useful,
    8.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    8.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    8.16 + * GNU General Public License for more details.
    8.17 + *
    8.18 + * You should have received a copy of the GNU General Public License
    8.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    8.20 + */
    8.21 +package cz.frantovo.copyImageResizer;
    8.22 +
    8.23 +/**
    8.24 + *
    8.25 + * @author Ing. František Kučera (frantovo.cz)
    8.26 + */
    8.27 +public class CopyImageResizerException extends Exception {
    8.28 +
    8.29 +	public CopyImageResizerException() {
    8.30 +	}
    8.31 +
    8.32 +	public CopyImageResizerException(String message) {
    8.33 +		super(message);
    8.34 +	}
    8.35 +
    8.36 +	public CopyImageResizerException(Throwable cause) {
    8.37 +		super(cause);
    8.38 +	}
    8.39 +
    8.40 +	public CopyImageResizerException(String message, Throwable cause) {
    8.41 +		super(message, cause);
    8.42 +	}
    8.43 +
    8.44 +}