franta-hg@16
|
1 |
/**
|
franta-hg@16
|
2 |
* SQL-DK
|
franta-hg@16
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@16
|
4 |
*
|
franta-hg@16
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@16
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@16
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@16
|
8 |
* (at your option) any later version.
|
franta-hg@16
|
9 |
*
|
franta-hg@16
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@16
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@16
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@16
|
13 |
* GNU General Public License for more details.
|
franta-hg@16
|
14 |
*
|
franta-hg@16
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@16
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@16
|
17 |
*/
|
franta-hg@1
|
18 |
package info.globalcode.sql.dk;
|
franta-hg@1
|
19 |
|
franta-hg@69
|
20 |
import info.globalcode.sql.dk.InfoLister.InfoType;
|
franta-hg@4
|
21 |
import java.util.ArrayList;
|
franta-hg@4
|
22 |
import java.util.HashMap;
|
franta-hg@4
|
23 |
import java.util.List;
|
franta-hg@4
|
24 |
import java.util.Map;
|
franta-hg@4
|
25 |
|
franta-hg@1
|
26 |
/**
|
franta-hg@1
|
27 |
*
|
franta-hg@1
|
28 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@1
|
29 |
*/
|
franta-hg@5
|
30 |
public class CLIParser {
|
franta-hg@1
|
31 |
|
franta-hg@4
|
32 |
public static final String TYPE_NAME_SEPARATOR = ":";
|
franta-hg@4
|
33 |
|
franta-hg@9
|
34 |
public CLIOptions parseOptions(String[] args) throws CLIParserException {
|
franta-hg@1
|
35 |
CLIOptions options = new CLIOptions();
|
franta-hg@1
|
36 |
|
franta-hg@68
|
37 |
List<SQLType> numberedTypes = new ArrayList<>();
|
franta-hg@68
|
38 |
Map<String, SQLType> namedTypes = new HashMap<>();
|
franta-hg@2
|
39 |
|
franta-hg@1
|
40 |
for (int i = 0; i < args.length; i++) {
|
franta-hg@1
|
41 |
String arg = args[i];
|
franta-hg@2
|
42 |
switch (arg) {
|
franta-hg@2
|
43 |
case Tokens.TYPES:
|
franta-hg@8
|
44 |
String typesString = fetchNext(args, ++i);
|
franta-hg@4
|
45 |
|
franta-hg@10
|
46 |
for (String oneType : typesString.split(",")) {
|
franta-hg@4
|
47 |
int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
|
franta-hg@4
|
48 |
if (sepatratorIndex == -1) {
|
franta-hg@4
|
49 |
numberedTypes.add(getType(oneType));
|
franta-hg@4
|
50 |
} else {
|
franta-hg@12
|
51 |
String namePart = oneType.substring(0, sepatratorIndex).trim();
|
franta-hg@4
|
52 |
String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
|
franta-hg@4
|
53 |
namedTypes.put(namePart, getType(typePart));
|
franta-hg@4
|
54 |
}
|
franta-hg@4
|
55 |
}
|
franta-hg@2
|
56 |
break;
|
franta-hg@2
|
57 |
case Tokens.NAME_PREFIX:
|
franta-hg@8
|
58 |
options.setNamePrefix(fetchNext(args, ++i));
|
franta-hg@2
|
59 |
break;
|
franta-hg@44
|
60 |
case Tokens.NAME_SUFFIX:
|
franta-hg@44
|
61 |
options.setNameSuffix(fetchNext(args, ++i));
|
franta-hg@44
|
62 |
break;
|
franta-hg@1
|
63 |
case Tokens.DB:
|
franta-hg@8
|
64 |
options.setDatabaseName(fetchNext(args, ++i));
|
franta-hg@1
|
65 |
break;
|
franta-hg@1
|
66 |
case Tokens.SQL:
|
franta-hg@8
|
67 |
options.setSql(fetchNext(args, ++i));
|
franta-hg@1
|
68 |
break;
|
franta-hg@1
|
69 |
case Tokens.BATCH:
|
franta-hg@2
|
70 |
options.setBatch(true);
|
franta-hg@1
|
71 |
break;
|
franta-hg@4
|
72 |
case Tokens.DATA: // --data is the last option
|
franta-hg@4
|
73 |
for (i++; i < args.length; i++) {
|
franta-hg@4
|
74 |
arg = args[i];
|
franta-hg@52
|
75 |
Parameter parameter;
|
franta-hg@52
|
76 |
if (numberedTypes.isEmpty()) {
|
franta-hg@52
|
77 |
parameter = new Parameter(arg, null);
|
franta-hg@52
|
78 |
} else {
|
franta-hg@52
|
79 |
int paramIndex = options.getNumberedParameters().size();
|
franta-hg@68
|
80 |
SQLType paramType;
|
franta-hg@52
|
81 |
try {
|
franta-hg@52
|
82 |
paramType = numberedTypes.get(paramIndex);
|
franta-hg@52
|
83 |
} catch (IndexOutOfBoundsException e) {
|
franta-hg@52
|
84 |
throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
|
franta-hg@52
|
85 |
} catch (NullPointerException e) {
|
franta-hg@52
|
86 |
throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
|
franta-hg@4
|
87 |
}
|
franta-hg@52
|
88 |
parameter = new Parameter(arg, paramType);
|
franta-hg@4
|
89 |
}
|
franta-hg@52
|
90 |
options.addNumberedParameter(parameter);
|
franta-hg@52
|
91 |
}
|
franta-hg@52
|
92 |
break;
|
franta-hg@52
|
93 |
case Tokens.DATA_NAMED:
|
franta-hg@52
|
94 |
for (i++; i < args.length; i++) {
|
franta-hg@52
|
95 |
String paramName = args[i];
|
franta-hg@52
|
96 |
String paramValue = fetchNext(args, ++i);
|
franta-hg@52
|
97 |
options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
|
franta-hg@4
|
98 |
}
|
franta-hg@1
|
99 |
break;
|
franta-hg@14
|
100 |
case Tokens.FORMATTER:
|
franta-hg@14
|
101 |
options.setFormatterName(fetchNext(args, ++i));
|
franta-hg@14
|
102 |
break;
|
franta-hg@14
|
103 |
case Tokens.INFO_HELP:
|
franta-hg@69
|
104 |
options.addShowInfo(InfoType.HELP);
|
franta-hg@14
|
105 |
break;
|
franta-hg@14
|
106 |
case Tokens.INFO_FORMATTERS:
|
franta-hg@69
|
107 |
options.addShowInfo(InfoType.FORMATTERS);
|
franta-hg@14
|
108 |
break;
|
franta-hg@14
|
109 |
case Tokens.INFO_LICENSE:
|
franta-hg@69
|
110 |
options.addShowInfo(InfoType.LICENSE);
|
franta-hg@14
|
111 |
break;
|
franta-hg@14
|
112 |
case Tokens.INFO_TYPES:
|
franta-hg@69
|
113 |
options.addShowInfo(InfoType.TYPES);
|
franta-hg@14
|
114 |
break;
|
franta-hg@14
|
115 |
case Tokens.INFO_VERSION:
|
franta-hg@69
|
116 |
options.addShowInfo(InfoType.VERSION);
|
franta-hg@14
|
117 |
break;
|
franta-hg@15
|
118 |
case Tokens.INFO_DATABASES:
|
franta-hg@69
|
119 |
options.addShowInfo(InfoType.DATABASES);
|
franta-hg@15
|
120 |
break;
|
franta-hg@15
|
121 |
case Tokens.INFO_CONNECTION:
|
franta-hg@69
|
122 |
options.addShowInfo(InfoType.CONNECTION);
|
franta-hg@74
|
123 |
options.addDatabaseNameToTest(fetchNext(args, ++i));
|
franta-hg@15
|
124 |
break;
|
franta-hg@4
|
125 |
default:
|
franta-hg@9
|
126 |
throw new CLIParserException("Unknown option: " + arg);
|
franta-hg@1
|
127 |
}
|
franta-hg@1
|
128 |
}
|
franta-hg@8
|
129 |
return options;
|
franta-hg@8
|
130 |
}
|
franta-hg@1
|
131 |
|
franta-hg@9
|
132 |
private String fetchNext(String[] args, int index) throws CLIParserException {
|
franta-hg@8
|
133 |
if (index < args.length) {
|
franta-hg@8
|
134 |
return args[index];
|
franta-hg@8
|
135 |
} else {
|
franta-hg@9
|
136 |
throw new CLIParserException("Expecting value for option: " + args[index - 1]);
|
franta-hg@8
|
137 |
}
|
franta-hg@1
|
138 |
}
|
franta-hg@1
|
139 |
|
franta-hg@1
|
140 |
public static class Tokens {
|
franta-hg@1
|
141 |
|
franta-hg@79
|
142 |
// bash-completion:options:
|
franta-hg@79
|
143 |
public static final String DB = "--db"; // bash-completion:option
|
franta-hg@79
|
144 |
public static final String SQL = "--sql"; // bash-completion:option
|
franta-hg@79
|
145 |
public static final String BATCH = "--batch"; // bash-completion:option
|
franta-hg@79
|
146 |
public static final String DATA = "--data"; // bash-completion:option
|
franta-hg@79
|
147 |
public static final String DATA_NAMED = "--data-named"; // bash-completion:option
|
franta-hg@79
|
148 |
public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option
|
franta-hg@79
|
149 |
public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option
|
franta-hg@79
|
150 |
public static final String TYPES = "--types"; // bash-completion:option
|
franta-hg@79
|
151 |
public static final String FORMATTER = "--formatter"; // bash-completion:option
|
franta-hg@79
|
152 |
public static final String INFO_HELP = "--help"; // bash-completion:option
|
franta-hg@79
|
153 |
public static final String INFO_VERSION = "--version"; // bash-completion:option
|
franta-hg@79
|
154 |
public static final String INFO_LICENSE = "--license"; // bash-completion:option
|
franta-hg@79
|
155 |
public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option
|
franta-hg@79
|
156 |
public static final String INFO_TYPES = "--list-types"; // bash-completion:option
|
franta-hg@79
|
157 |
public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option
|
franta-hg@79
|
158 |
public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option
|
franta-hg@1
|
159 |
|
franta-hg@1
|
160 |
private Tokens() {
|
franta-hg@1
|
161 |
}
|
franta-hg@1
|
162 |
}
|
franta-hg@4
|
163 |
|
franta-hg@68
|
164 |
private SQLType getType(String typeString) throws CLIParserException {
|
franta-hg@68
|
165 |
try {
|
franta-hg@68
|
166 |
return SQLType.valueOf(typeString.trim());
|
franta-hg@68
|
167 |
} catch (IllegalArgumentException e) {
|
franta-hg@68
|
168 |
throw new CLIParserException("Unsupported type: " + typeString, e);
|
franta-hg@8
|
169 |
}
|
franta-hg@4
|
170 |
}
|
franta-hg@1
|
171 |
}
|