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 info.globalcode.sql.dk.InfoLister.InfoType;
21 import info.globalcode.sql.dk.configuration.Property;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
28 * Converts command line arguments from String array to object.
29 * Checks basic constraints (if only supported options are used and if they have correct number of
32 * @author Ing. František Kučera (frantovo.cz)
34 public class CLIParser {
36 public static final String TYPE_NAME_SEPARATOR = ":";
38 public CLIOptions parseOptions(String[] args) throws CLIParserException {
39 CLIOptions options = new CLIOptions();
41 List<SQLType> numberedTypes = new ArrayList<>();
42 Map<String, SQLType> namedTypes = new HashMap<>();
44 for (int i = 0; i < args.length; i++) {
48 String typesString = fetchNext(args, ++i);
50 for (String oneType : typesString.split(",")) {
51 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
52 if (sepatratorIndex == -1) {
53 numberedTypes.add(getType(oneType.toUpperCase()));
55 String namePart = oneType.substring(0, sepatratorIndex).trim();
56 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
57 namedTypes.put(namePart, getType(typePart.toUpperCase()));
61 case Tokens.NAME_PREFIX:
62 options.setNamePrefix(fetchNext(args, ++i));
64 case Tokens.NAME_SUFFIX:
65 options.setNameSuffix(fetchNext(args, ++i));
68 options.setDatabaseName(fetchNext(args, ++i));
71 options.setSql(fetchNext(args, ++i));
74 options.setBatch(true);
76 case Tokens.DATA: // --data is the last option
77 for (i++; i < args.length; i++) {
80 if (numberedTypes.isEmpty()) {
81 parameter = new Parameter(arg, null);
83 int paramIndex = options.getNumberedParameters().size();
86 paramType = numberedTypes.get(paramIndex);
87 } catch (IndexOutOfBoundsException e) {
88 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
89 } catch (NullPointerException e) {
90 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
92 parameter = new Parameter(arg, paramType);
94 options.addNumberedParameter(parameter);
97 case Tokens.DATA_NAMED:
98 for (i++; i < args.length; i++) {
99 String paramName = args[i];
100 String paramValue = fetchNext(args, ++i);
101 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
104 case Tokens.FORMATTER:
105 options.setFormatterName(fetchNext(args, ++i));
107 case Tokens.DB_PROPERTY:
108 options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
110 case Tokens.FORMATTER_PROPERTY:
111 options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
113 case Tokens.INFO_HELP:
114 options.addShowInfo(InfoType.HELP);
116 case Tokens.INFO_FORMATTERS:
117 options.addShowInfo(InfoType.FORMATTERS);
119 case Tokens.INFO_LICENSE:
120 options.addShowInfo(InfoType.LICENSE);
122 case Tokens.INFO_TYPES:
123 options.addShowInfo(InfoType.TYPES);
125 case Tokens.INFO_VERSION:
126 options.addShowInfo(InfoType.VERSION);
128 case Tokens.INFO_DATABASES:
129 options.addShowInfo(InfoType.DATABASES);
131 case Tokens.INFO_CONNECTION:
132 options.addShowInfo(InfoType.CONNECTION);
133 options.addDatabaseNamesToTest(fetchNext(args, ++i));
136 throw new CLIParserException("Unknown option: " + arg);
142 private String fetchNext(String[] args, int index) throws CLIParserException {
143 if (index < args.length) {
146 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
150 public static class Tokens {
152 // bash-completion:options:
153 public static final String DB = "--db"; // bash-completion:option // help: database name
154 public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
155 public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
156 public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
157 public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
158 public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
159 public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
160 public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
161 public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
162 public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
163 public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
164 public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
165 public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
166 public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
167 public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
168 public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
169 public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
170 public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
176 private SQLType getType(String typeString) throws CLIParserException {
178 return SQLType.valueOf(typeString.trim());
179 } catch (IllegalArgumentException e) {
180 throw new CLIParserException("Unsupported type: " + typeString, e);