3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import java.sql.Types;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
29 * @author Ing. František Kučera (frantovo.cz)
31 public class CLIParser {
33 public static final String TYPE_NAME_SEPARATOR = ":";
34 private final Map<String, Integer> types;
37 Map<String, Integer> m = new HashMap<>();
38 m.put("integer", Types.INTEGER);
39 m.put("varchar", Types.VARCHAR);
40 m.put("boolean", Types.BOOLEAN);
44 types = Collections.unmodifiableMap(m);
47 public CLIOptions parseOptions(String[] args) throws CLIParserException {
48 CLIOptions options = new CLIOptions();
50 List<Integer> numberedTypes = new ArrayList<>();
51 Map<String, Integer> namedTypes = new HashMap<>();
53 for (int i = 0; i < args.length; i++) {
57 String typesString = fetchNext(args, ++i);
59 for (String oneType : typesString.split(",")) {
60 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
61 if (sepatratorIndex == -1) {
62 numberedTypes.add(getType(oneType));
64 String namePart = oneType.substring(0, sepatratorIndex).trim();
65 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
66 namedTypes.put(namePart, getType(typePart));
70 case Tokens.NAME_PREFIX:
71 options.setNamePrefix(fetchNext(args, ++i));
73 case Tokens.NAME_SUFFIX:
74 options.setNameSuffix(fetchNext(args, ++i));
77 options.setDatabaseName(fetchNext(args, ++i));
80 options.setSql(fetchNext(args, ++i));
83 options.setBatch(true);
85 case Tokens.DATA: // --data is the last option
86 for (i++; i < args.length; i++) {
89 if (numberedTypes.isEmpty()) {
90 parameter = new Parameter(arg, null);
92 int paramIndex = options.getNumberedParameters().size();
95 paramType = numberedTypes.get(paramIndex);
96 } catch (IndexOutOfBoundsException e) {
97 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
98 } catch (NullPointerException e) {
99 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
101 parameter = new Parameter(arg, paramType);
103 options.addNumberedParameter(parameter);
106 case Tokens.DATA_NAMED:
107 for (i++; i < args.length; i++) {
108 String paramName = args[i];
109 String paramValue = fetchNext(args, ++i);
110 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
113 case Tokens.FORMATTER:
114 options.setFormatterName(fetchNext(args, ++i));
116 case Tokens.INFO_HELP:
117 options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
119 case Tokens.INFO_FORMATTERS:
120 options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
122 case Tokens.INFO_LICENSE:
123 options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
125 case Tokens.INFO_TYPES:
126 options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
128 case Tokens.INFO_VERSION:
129 options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
131 case Tokens.INFO_DATABASES:
132 options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
134 case Tokens.INFO_CONNECTION:
135 options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
136 options.setDatabaseNameToTest(fetchNext(args, ++i));
139 throw new CLIParserException("Unknown option: " + arg);
145 private String fetchNext(String[] args, int index) throws CLIParserException {
146 if (index < args.length) {
149 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
153 public static class Tokens {
155 public static final String DB = "--db";
156 public static final String SQL = "--sql";
157 public static final String BATCH = "--batch";
158 public static final String DATA = "--data";
159 public static final String DATA_NAMED = "--data-named";
160 public static final String NAME_PREFIX = "--name-prefix";
161 public static final String NAME_SUFFIX = "--name-suffix";
162 public static final String TYPES = "--types";
163 public static final String FORMATTER = "--formatter";
164 public static final String INFO_HELP = "--help";
165 public static final String INFO_VERSION = "--version";
166 public static final String INFO_LICENSE = "--license";
167 public static final String INFO_FORMATTERS = "--list-formatters";
168 public static final String INFO_TYPES = "--list-types";
169 public static final String INFO_DATABASES = "--list-databases";
170 public static final String INFO_CONNECTION = "--test-connection";
176 private int getType(String typeString) throws CLIParserException {
177 Integer type = types.get(typeString.trim());
179 throw new CLIParserException("Unsupported type: " + typeString);