InfoLister: new listings: --list-java-properties and --list-environment-variables
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 static info.globalcode.sql.dk.Functions.readString;
21 import info.globalcode.sql.dk.InfoLister.InfoType;
22 import info.globalcode.sql.dk.configuration.Property;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
31 * Converts command line arguments from String array to object.
32 * Checks basic constraints (if only supported options are used and if they have correct number of
35 * @author Ing. František Kučera (frantovo.cz)
37 public class CLIParser {
39 public static final String TYPE_NAME_SEPARATOR = ":";
41 public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException {
42 CLIOptions options = new CLIOptions();
44 List<SQLType> numberedTypes = new ArrayList<>();
45 Map<String, SQLType> namedTypes = new HashMap<>();
47 for (int i = 0; i < args.length; i++) {
51 String typesString = fetchNext(args, ++i);
53 for (String oneType : typesString.split(",")) {
54 int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
55 if (sepatratorIndex == -1) {
56 numberedTypes.add(getType(oneType.toUpperCase()));
58 String namePart = oneType.substring(0, sepatratorIndex).trim();
59 String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
60 namedTypes.put(namePart, getType(typePart.toUpperCase()));
64 case Tokens.NAME_PREFIX:
65 options.setNamePrefix(fetchNext(args, ++i));
67 case Tokens.NAME_SUFFIX:
68 options.setNameSuffix(fetchNext(args, ++i));
71 options.setDatabaseName(fetchNext(args, ++i));
74 options.setSql(fetchNext(args, ++i));
78 options.setSql(readString(in));
79 } catch (IOException e) {
80 throw new CLIParserException("Unable to read SQL from the input stream", e);
84 options.setBatch(true);
86 case Tokens.DATA: // --data is the last option
87 for (i++; i < args.length; i++) {
90 if (numberedTypes.isEmpty()) {
91 parameter = new Parameter(arg, null);
93 int paramIndex = options.getNumberedParameters().size();
96 paramType = numberedTypes.get(paramIndex);
97 } catch (IndexOutOfBoundsException e) {
98 throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
99 } catch (NullPointerException e) {
100 throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
102 parameter = new Parameter(arg, paramType);
104 options.addNumberedParameter(parameter);
107 case Tokens.DATA_NAMED:
108 for (i++; i < args.length; i++) {
109 String paramName = args[i];
110 String paramValue = fetchNext(args, ++i);
111 options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
114 case Tokens.FORMATTER:
115 options.setFormatterName(fetchNext(args, ++i));
117 case Tokens.DB_PROPERTY:
118 options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
120 case Tokens.FORMATTER_PROPERTY:
121 options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
123 case Tokens.INFO_HELP:
124 options.addShowInfo(InfoType.HELP);
126 case Tokens.INFO_FORMATTERS:
127 options.addShowInfo(InfoType.FORMATTERS);
129 case Tokens.INFO_LICENSE:
130 options.addShowInfo(InfoType.LICENSE);
132 case Tokens.INFO_JAVA_PROPERTIES:
133 options.addShowInfo(InfoType.JAVA_PROPERTIES);
135 case Tokens.INFO_ENVIRONMENT_VARIABLES:
136 options.addShowInfo(InfoType.ENVIRONMENT_VARIABLES);
138 case Tokens.INFO_TYPES:
139 options.addShowInfo(InfoType.TYPES);
141 case Tokens.INFO_VERSION:
142 options.addShowInfo(InfoType.VERSION);
144 case Tokens.INFO_JDBC_DRIVERS:
145 options.addShowInfo(InfoType.JDBC_DRIVERS);
147 case Tokens.INFO_JDBC_PROPERTIES:
148 options.addShowInfo(InfoType.JDBC_PROPERTIES);
149 options.addDatabaseNameToListProperties(fetchNext(args, ++i));
151 case Tokens.INFO_DATABASES:
152 options.addShowInfo(InfoType.DATABASES);
154 case Tokens.INFO_CONNECTION:
155 options.addShowInfo(InfoType.CONNECTION);
156 options.addDatabaseNameToTest(fetchNext(args, ++i));
159 throw new CLIParserException("Unknown option: " + arg);
165 private String fetchNext(String[] args, int index) throws CLIParserException {
166 if (index < args.length) {
169 throw new CLIParserException("Expecting value for option: " + args[index - 1]);
173 public static class Tokens {
175 // bash-completion:options:
176 public static final String DB = "--db"; // bash-completion:option // help: database name
177 public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
178 public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
179 public static final String SQL_IN = "--sql-in"; // bash-completion:option // help: SQL query/command
180 public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
181 public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
182 public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
183 public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
184 public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
185 public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
186 public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
187 public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
188 public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
189 public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
190 public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
191 public static final String INFO_JAVA_PROPERTIES = "--list-java-properties"; // bash-completion:option // help: list of Java system properties
192 public static final String INFO_ENVIRONMENT_VARIABLES = "--list-environment-variables"; // bash-completion:option // help: list of environment variables
193 public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
194 public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
195 public static final String INFO_JDBC_DRIVERS = "--list-jdbc-drivers"; // bash-completion:option // help: list of available JDBC drivers
196 public static final String INFO_JDBC_PROPERTIES = "--list-jdbc-properties"; // bash-completion:option // help: list of available JDBC properties for given database
197 public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
198 public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
204 private SQLType getType(String typeString) throws CLIParserException {
206 return SQLType.valueOf(typeString.trim());
207 } catch (IllegalArgumentException e) {
208 throw new CLIParserException("Unsupported type: " + typeString, e);