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.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
27 * @author Ing. František Kučera (frantovo.cz)
29 public class CLIParser {
31 public static final String TYPE_NAME_SEPARATOR = ":";
33 public CLIOptions parseOptions(String[] args) throws CLIParserException {
34 CLIOptions options = new CLIOptions();
36 List<SQLType> numberedTypes = new ArrayList<>();
37 Map<String, SQLType> namedTypes = new HashMap<>();
39 for (int i = 0; i < args.length; i++) {
43 String typesString = fetchNext(args, ++i);
45 for (String oneType : typesString.split(",")) {
46 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
47 if (sepatratorIndex == -1) {
48 numberedTypes.add(getType(oneType));
50 String namePart = oneType.substring(0, sepatratorIndex).trim();
51 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
52 namedTypes.put(namePart, getType(typePart));
56 case Tokens.NAME_PREFIX:
57 options.setNamePrefix(fetchNext(args, ++i));
59 case Tokens.NAME_SUFFIX:
60 options.setNameSuffix(fetchNext(args, ++i));
63 options.setDatabaseName(fetchNext(args, ++i));
66 options.setSql(fetchNext(args, ++i));
69 options.setBatch(true);
71 case Tokens.DATA: // --data is the last option
72 for (i++; i < args.length; i++) {
75 if (numberedTypes.isEmpty()) {
76 parameter = new Parameter(arg, null);
78 int paramIndex = options.getNumberedParameters().size();
81 paramType = numberedTypes.get(paramIndex);
82 } catch (IndexOutOfBoundsException e) {
83 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
84 } catch (NullPointerException e) {
85 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
87 parameter = new Parameter(arg, paramType);
89 options.addNumberedParameter(parameter);
92 case Tokens.DATA_NAMED:
93 for (i++; i < args.length; i++) {
94 String paramName = args[i];
95 String paramValue = fetchNext(args, ++i);
96 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
99 case Tokens.FORMATTER:
100 options.setFormatterName(fetchNext(args, ++i));
102 case Tokens.INFO_HELP:
103 options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
105 case Tokens.INFO_FORMATTERS:
106 options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
108 case Tokens.INFO_LICENSE:
109 options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
111 case Tokens.INFO_TYPES:
112 options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
114 case Tokens.INFO_VERSION:
115 options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
117 case Tokens.INFO_DATABASES:
118 options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES);
120 case Tokens.INFO_CONNECTION:
121 options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION);
122 options.setDatabaseNameToTest(fetchNext(args, ++i));
125 throw new CLIParserException("Unknown option: " + arg);
131 private String fetchNext(String[] args, int index) throws CLIParserException {
132 if (index < args.length) {
135 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
139 public static class Tokens {
141 public static final String DB = "--db";
142 public static final String SQL = "--sql";
143 public static final String BATCH = "--batch";
144 public static final String DATA = "--data";
145 public static final String DATA_NAMED = "--data-named";
146 public static final String NAME_PREFIX = "--name-prefix";
147 public static final String NAME_SUFFIX = "--name-suffix";
148 public static final String TYPES = "--types";
149 public static final String FORMATTER = "--formatter";
150 public static final String INFO_HELP = "--help";
151 public static final String INFO_VERSION = "--version";
152 public static final String INFO_LICENSE = "--license";
153 public static final String INFO_FORMATTERS = "--list-formatters";
154 public static final String INFO_TYPES = "--list-types";
155 public static final String INFO_DATABASES = "--list-databases";
156 public static final String INFO_CONNECTION = "--test-connection";
162 private SQLType getType(String typeString) throws CLIParserException {
164 return SQLType.valueOf(typeString.trim());
165 } catch (IllegalArgumentException e) {
166 throw new CLIParserException("Unsupported type: " + typeString, e);