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("int", Types.INTEGER);
39 m.put("string", 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));
74 options.setDatabaseName(fetchNext(args, ++i));
77 options.setSql(fetchNext(args, ++i));
78 options.setCommandType(CLIOptions.COMMAND_TYPE.QUERY);
80 case Tokens.SQL_UPDATE:
81 case Tokens.SQL_INSERT:
82 options.setSql(fetchNext(args, ++i));
83 options.setCommandType(CLIOptions.COMMAND_TYPE.UPDATE);
86 options.setBatch(true);
88 case Tokens.DATA: // --data is the last option
89 for (i++; i < args.length; i++) {
92 if (arg.startsWith(options.getNamePrefix())) { // Named parameters:
93 String paramName = arg.substring(options.getNamePrefix().length());
94 String paramValue = fetchNext(args, ++i);
95 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
96 } else { // Numbered parameters:
98 if (numberedTypes.isEmpty()) {
99 parameter = new Parameter(arg, null);
101 int paramIndex = options.getNumberedParameters().size();
104 paramType = numberedTypes.get(paramIndex);
105 } catch (IndexOutOfBoundsException e) {
106 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
107 } catch (NullPointerException e) {
108 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
110 parameter = new Parameter(arg, paramType);
112 options.addNumberedParameter(parameter);
116 case Tokens.FORMATTER:
117 options.setFormatterName(fetchNext(args, ++i));
119 case Tokens.INFO_HELP:
120 options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
122 case Tokens.INFO_FORMATTERS:
123 options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
125 case Tokens.INFO_LICENSE:
126 options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
128 case Tokens.INFO_TYPES:
129 options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
131 case Tokens.INFO_VERSION:
132 options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
134 case Tokens.INFO_DATABASES:
135 options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
137 case Tokens.INFO_CONNECTION:
138 options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
139 options.setDatabaseNameToTest(fetchNext(args, ++i));
142 throw new CLIParserException("Unknown option: " + arg);
148 private String fetchNext(String[] args, int index) throws CLIParserException {
149 if (index < args.length) {
152 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
156 public static class Tokens {
158 public static final String DB = "--db";
159 public static final String SQL = "--sql";
160 public static final String SQL_UPDATE = "--sql-update";
161 public static final String SQL_INSERT = "--sql-insert";
162 public static final String BATCH = "--batch";
163 public static final String DATA = "--data";
164 public static final String NAME_PREFIX = "--name-prefix";
165 public static final String TYPES = "--types";
166 public static final String FORMATTER = "--formatter";
167 public static final String INFO_HELP = "--help";
168 public static final String INFO_VERSION = "--version";
169 public static final String INFO_LICENSE = "--license";
170 public static final String INFO_FORMATTERS = "--list-formatters";
171 public static final String INFO_TYPES = "--list-types";
172 public static final String INFO_DATABASES = "--list-databases";
173 public static final String INFO_CONNECTION = "--test-connection";
179 private int getType(String typeString) throws CLIParserException {
180 Integer type = types.get(typeString.trim());
182 throw new CLIParserException("Unsupported type: " + typeString);