java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 16 Dec 2013 20:01:37 +0100
branchv_0
changeset 16 5b8fcd35d4d6
parent 15 bbd335b5410c
child 29 d66858b4b563
permissions -rw-r--r--
license: GNU GPLv3+
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     1
/**
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     2
 * SQL-DK
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     4
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     8
 * (at your option) any later version.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     9
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    13
 * GNU General Public License for more details.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    14
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    15
 * You should have received a copy of the GNU General Public License
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    17
 */
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
package info.globalcode.sql.dk;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    20
import java.sql.Types;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    21
import java.util.ArrayList;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    22
import java.util.Collections;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    23
import java.util.HashMap;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    24
import java.util.List;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    25
import java.util.Map;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    26
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
/**
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
 *
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
 * @author Ing. František Kučera (frantovo.cz)
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
 */
5
26223eb63851 more OOP
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    31
public class CLIParser {
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    33
	public static final String TYPE_NAME_SEPARATOR = ":";
5
26223eb63851 more OOP
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    34
	private final Map<String, Integer> types;
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    35
5
26223eb63851 more OOP
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    36
	public CLIParser() {
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    37
		Map<String, Integer> m = new HashMap<>();
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    38
		m.put("int", Types.INTEGER);
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    39
		m.put("string", Types.VARCHAR);
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    40
		m.put("boolean", Types.BOOLEAN);
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    41
		/**
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    42
		 * TODO: more types
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    43
		 */
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    44
		types = Collections.unmodifiableMap(m);
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    45
	}
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    46
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    47
	public CLIOptions parseOptions(String[] args) throws CLIParserException {
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		CLIOptions options = new CLIOptions();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
		List<Integer> numberedTypes = new ArrayList<>();
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    51
		Map<String, Integer> namedTypes = new HashMap<>();
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    52
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
		for (int i = 0; i < args.length; i++) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
			String arg = args[i];
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    55
			switch (arg) {
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    56
				case Tokens.TYPES:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    57
					String typesString = fetchNext(args, ++i);
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    58
10
f528406f33f4 unit-test: types
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    59
					for (String oneType : typesString.split(",")) {
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    61
						if (sepatratorIndex == -1) {
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    62
							numberedTypes.add(getType(oneType));
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    63
						} else {
12
2dcb67d90fd2 unit-test: names, types, prefix
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    64
							String namePart = oneType.substring(0, sepatratorIndex).trim();
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    65
							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    66
							namedTypes.put(namePart, getType(typePart));
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    67
						}
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    68
					}
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    69
					break;
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    70
				case Tokens.NAME_PREFIX:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    71
					options.setNamePrefix(fetchNext(args, ++i));
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    72
					break;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
				case Tokens.DB:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    74
					options.setDatabaseName(fetchNext(args, ++i));
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
					break;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				case Tokens.SQL:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    77
					options.setSql(fetchNext(args, ++i));
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    78
					options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
					break;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
				case Tokens.SQL_UPDATE:
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
				case Tokens.SQL_INSERT:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    82
					options.setSql(fetchNext(args, ++i));
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    83
					options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
					break;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
				case Tokens.BATCH:
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    86
					options.setBatch(true);
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
					break;
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    88
				case Tokens.DATA: // --data is the last option
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    89
					for (i++; i < args.length; i++) {
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    90
						arg = args[i];
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    91
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    92
						if (arg.startsWith(options.getNamePrefix())) { // Named parameters:
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    93
							String paramName = arg.substring(options.getNamePrefix().length());
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    94
							String paramValue = fetchNext(args, ++i);
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    95
							options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    96
						} else { // Numbered parameters:
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    97
							Parameter parameter;
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    98
							if (numberedTypes.isEmpty()) {
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    99
								parameter = new Parameter(arg, null);
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   100
							} else {
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   101
								int paramIndex = options.getNumberedParameters().size();
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   102
								int paramType;
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   103
								try {
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   104
									paramType = numberedTypes.get(paramIndex);
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   105
								} catch (IndexOutOfBoundsException e) {
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   106
									throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   107
								} catch (NullPointerException e) {
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   108
									throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   109
								}
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   110
								parameter = new Parameter(arg, paramType);
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   111
							}
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   112
							options.addNumberedParameter(parameter);
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   113
						}
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   114
					}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
					break;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   116
				case Tokens.FORMATTER:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   117
					options.setFormatterName(fetchNext(args, ++i));
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   118
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   119
				case Tokens.INFO_HELP:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   120
					options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   121
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   122
				case Tokens.INFO_FORMATTERS:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   123
					options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   124
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   125
				case Tokens.INFO_LICENSE:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   126
					options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   127
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   128
				case Tokens.INFO_TYPES:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   129
					options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   130
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   131
				case Tokens.INFO_VERSION:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   132
					options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   133
					break;
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   134
				case Tokens.INFO_DATABASES:
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   135
					options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   136
					break;
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   137
				case Tokens.INFO_CONNECTION:
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   138
					options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   139
					options.setDatabaseNameToTest(fetchNext(args, ++i));
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   140
					break;
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   141
				default:
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   142
					throw new CLIParserException("Unknown option: " + arg);
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   143
			}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
		}
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   145
		return options;
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   146
	}
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   147
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   148
	private String fetchNext(String[] args, int index) throws CLIParserException {
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   149
		if (index < args.length) {
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   150
			return args[index];
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   151
		} else {
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   152
			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   153
		}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   155
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
	public static class Tokens {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
		public static final String DB = "--db";
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
		public static final String SQL = "--sql";
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
		public static final String SQL_UPDATE = "--sql-update";
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   161
		public static final String SQL_INSERT = "--sql-insert";
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
		public static final String BATCH = "--batch";
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
		public static final String DATA = "--data";
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   164
		public static final String NAME_PREFIX = "--name-prefix";
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
		public static final String TYPES = "--types";
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   166
		public static final String FORMATTER = "--formatter";
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   167
		public static final String INFO_HELP = "--help";
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   168
		public static final String INFO_VERSION = "--version";
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   169
		public static final String INFO_LICENSE = "--license";
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   170
		public static final String INFO_FORMATTERS = "--list-formatters";
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   171
		public static final String INFO_TYPES = "--list-types";
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   172
		public static final String INFO_DATABASES = "--list-databases";
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   173
		public static final String INFO_CONNECTION = "--test-connection";
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
		private Tokens() {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
		}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
	}
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   178
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   179
	private int getType(String typeString) throws CLIParserException {
10
f528406f33f4 unit-test: types
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   180
		Integer type = types.get(typeString.trim());
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   181
		if (type == null) {
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   182
			throw new CLIParserException("Unsupported type: " + typeString);
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   183
		} else {
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   184
			return type;
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   185
		}
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   186
	}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
}