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;
29 * @author Ing. František Kučera (frantovo.cz)
31 public class CLIParser {
33 public static final String TYPE_NAME_SEPARATOR = ":";
35 public CLIOptions parseOptions(String[] args) throws CLIParserException {
36 CLIOptions options = new CLIOptions();
38 List<SQLType> numberedTypes = new ArrayList<>();
39 Map<String, SQLType> namedTypes = new HashMap<>();
41 for (int i = 0; i < args.length; i++) {
45 String typesString = fetchNext(args, ++i);
47 for (String oneType : typesString.split(",")) {
48 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
49 if (sepatratorIndex == -1) {
50 numberedTypes.add(getType(oneType.toUpperCase()));
52 String namePart = oneType.substring(0, sepatratorIndex).trim();
53 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
54 namedTypes.put(namePart, getType(typePart.toUpperCase()));
58 case Tokens.NAME_PREFIX:
59 options.setNamePrefix(fetchNext(args, ++i));
61 case Tokens.NAME_SUFFIX:
62 options.setNameSuffix(fetchNext(args, ++i));
65 options.setDatabaseName(fetchNext(args, ++i));
68 options.setSql(fetchNext(args, ++i));
71 options.setBatch(true);
73 case Tokens.DATA: // --data is the last option
74 for (i++; i < args.length; i++) {
77 if (numberedTypes.isEmpty()) {
78 parameter = new Parameter(arg, null);
80 int paramIndex = options.getNumberedParameters().size();
83 paramType = numberedTypes.get(paramIndex);
84 } catch (IndexOutOfBoundsException e) {
85 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
86 } catch (NullPointerException e) {
87 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
89 parameter = new Parameter(arg, paramType);
91 options.addNumberedParameter(parameter);
94 case Tokens.DATA_NAMED:
95 for (i++; i < args.length; i++) {
96 String paramName = args[i];
97 String paramValue = fetchNext(args, ++i);
98 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
101 case Tokens.FORMATTER:
102 options.setFormatterName(fetchNext(args, ++i));
104 case Tokens.DB_PROPERTY:
105 options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
107 case Tokens.FORMATTER_PROPERTY:
108 options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
110 case Tokens.INFO_HELP:
111 options.addShowInfo(InfoType.HELP);
113 case Tokens.INFO_FORMATTERS:
114 options.addShowInfo(InfoType.FORMATTERS);
116 case Tokens.INFO_LICENSE:
117 options.addShowInfo(InfoType.LICENSE);
119 case Tokens.INFO_TYPES:
120 options.addShowInfo(InfoType.TYPES);
122 case Tokens.INFO_VERSION:
123 options.addShowInfo(InfoType.VERSION);
125 case Tokens.INFO_DATABASES:
126 options.addShowInfo(InfoType.DATABASES);
128 case Tokens.INFO_CONNECTION:
129 options.addShowInfo(InfoType.CONNECTION);
130 options.addDatabaseNameToTest(fetchNext(args, ++i));
133 throw new CLIParserException("Unknown option: " + arg);
139 private String fetchNext(String[] args, int index) throws CLIParserException {
140 if (index < args.length) {
143 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
147 public static class Tokens {
149 // bash-completion:options:
150 public static final String DB = "--db"; // bash-completion:option // help: database name
151 public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
152 public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
153 public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
154 public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
155 public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
156 public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
157 public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
158 public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
159 public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
160 public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
161 public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
162 public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
163 public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
164 public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
165 public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
166 public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
167 public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
173 private SQLType getType(String typeString) throws CLIParserException {
175 return SQLType.valueOf(typeString.trim());
176 } catch (IllegalArgumentException e) {
177 throw new CLIParserException("Unsupported type: " + typeString, e);