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 java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
28 * @author Ing. František Kučera (frantovo.cz)
30 public class CLIParser {
32 public static final String TYPE_NAME_SEPARATOR = ":";
34 public CLIOptions parseOptions(String[] args) throws CLIParserException {
35 CLIOptions options = new CLIOptions();
37 List<SQLType> numberedTypes = new ArrayList<>();
38 Map<String, SQLType> namedTypes = new HashMap<>();
40 for (int i = 0; i < args.length; i++) {
44 String typesString = fetchNext(args, ++i);
46 for (String oneType : typesString.split(",")) {
47 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
48 if (sepatratorIndex == -1) {
49 numberedTypes.add(getType(oneType));
51 String namePart = oneType.substring(0, sepatratorIndex).trim();
52 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
53 namedTypes.put(namePart, getType(typePart));
57 case Tokens.NAME_PREFIX:
58 options.setNamePrefix(fetchNext(args, ++i));
60 case Tokens.NAME_SUFFIX:
61 options.setNameSuffix(fetchNext(args, ++i));
64 options.setDatabaseName(fetchNext(args, ++i));
67 options.setSql(fetchNext(args, ++i));
70 options.setBatch(true);
72 case Tokens.DATA: // --data is the last option
73 for (i++; i < args.length; i++) {
76 if (numberedTypes.isEmpty()) {
77 parameter = new Parameter(arg, null);
79 int paramIndex = options.getNumberedParameters().size();
82 paramType = numberedTypes.get(paramIndex);
83 } catch (IndexOutOfBoundsException e) {
84 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
85 } catch (NullPointerException e) {
86 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
88 parameter = new Parameter(arg, paramType);
90 options.addNumberedParameter(parameter);
93 case Tokens.DATA_NAMED:
94 for (i++; i < args.length; i++) {
95 String paramName = args[i];
96 String paramValue = fetchNext(args, ++i);
97 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
100 case Tokens.FORMATTER:
101 options.setFormatterName(fetchNext(args, ++i));
103 case Tokens.INFO_HELP:
104 options.addShowInfo(InfoType.HELP);
106 case Tokens.INFO_FORMATTERS:
107 options.addShowInfo(InfoType.FORMATTERS);
109 case Tokens.INFO_LICENSE:
110 options.addShowInfo(InfoType.LICENSE);
112 case Tokens.INFO_TYPES:
113 options.addShowInfo(InfoType.TYPES);
115 case Tokens.INFO_VERSION:
116 options.addShowInfo(InfoType.VERSION);
118 case Tokens.INFO_DATABASES:
119 options.addShowInfo(InfoType.DATABASES);
121 case Tokens.INFO_CONNECTION:
122 options.addShowInfo(InfoType.CONNECTION);
123 options.addDatabaseNameToTest(fetchNext(args, ++i));
126 throw new CLIParserException("Unknown option: " + arg);
132 private String fetchNext(String[] args, int index) throws CLIParserException {
133 if (index < args.length) {
136 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
140 public static class Tokens {
142 // bash-completion:options:
143 public static final String DB = "--db"; // bash-completion:option
144 public static final String SQL = "--sql"; // bash-completion:option
145 public static final String BATCH = "--batch"; // bash-completion:option
146 public static final String DATA = "--data"; // bash-completion:option
147 public static final String DATA_NAMED = "--data-named"; // bash-completion:option
148 public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option
149 public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option
150 public static final String TYPES = "--types"; // bash-completion:option
151 public static final String FORMATTER = "--formatter"; // bash-completion:option
152 public static final String INFO_HELP = "--help"; // bash-completion:option
153 public static final String INFO_VERSION = "--version"; // bash-completion:option
154 public static final String INFO_LICENSE = "--license"; // bash-completion:option
155 public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option
156 public static final String INFO_TYPES = "--list-types"; // bash-completion:option
157 public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option
158 public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option
164 private SQLType getType(String typeString) throws CLIParserException {
166 return SQLType.valueOf(typeString.trim());
167 } catch (IllegalArgumentException e) {
168 throw new CLIParserException("Unsupported type: " + typeString, e);