java/sql-dk/src/main/java/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 24 Oct 2019 21:43:08 +0200
branchv_0
changeset 250 aae5009bd0af
parent 246 277c18b48762
permissions -rw-r--r--
fix license version: GNU GPLv3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     1
/**
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     2
 * SQL-DK
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     4
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
250
aae5009bd0af fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 246
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
16
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     8
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    12
 * GNU General Public License for more details.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    13
 *
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    14
 * You should have received a copy of the GNU General Public License
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5b8fcd35d4d6 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    16
 */
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
package info.globalcode.sql.dk;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    19
import static info.globalcode.sql.dk.Functions.readString;
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
    20
import info.globalcode.sql.dk.InfoLister.InfoType;
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
    21
import info.globalcode.sql.dk.configuration.Property;
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    22
import java.io.IOException;
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    23
import java.io.InputStream;
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    24
import java.util.ArrayList;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    25
import java.util.HashMap;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    26
import java.util.List;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    27
import java.util.Map;
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    28
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
/**
246
277c18b48762 support custom relation names in the XML formatter (added --relation CLI option)
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    30
 * Converts command line arguments from String array to object. Checks basic constraints (if only
277c18b48762 support custom relation names in the XML formatter (added --relation CLI option)
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
    31
 * supported options are used and if they have correct number of parameters)
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
 *
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
 * @author Ing. František Kučera (frantovo.cz)
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
 */
5
26223eb63851 more OOP
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    35
public class CLIParser {
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    37
	public static final String TYPE_NAME_SEPARATOR = ":";
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    38
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    39
	public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException {
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
		CLIOptions options = new CLIOptions();
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
68
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
    42
		List<SQLType> numberedTypes = new ArrayList<>();
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
    43
		Map<String, SQLType> namedTypes = new HashMap<>();
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    44
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		for (int i = 0; i < args.length; i++) {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
			String arg = args[i];
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    47
			switch (arg) {
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    48
				case Tokens.TYPES:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    49
					String typesString = fetchNext(args, ++i);
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    50
10
f528406f33f4 unit-test: types
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    51
					for (String oneType : typesString.split(",")) {
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    52
						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    53
						if (sepatratorIndex == -1) {
93
5a4dbe6f962c Type names in --types option are case insensitive
František Kučera <franta-hg@frantovo.cz>
parents: 79
diff changeset
    54
							numberedTypes.add(getType(oneType.toUpperCase()));
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    55
						} else {
12
2dcb67d90fd2 unit-test: names, types, prefix
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    56
							String namePart = oneType.substring(0, sepatratorIndex).trim();
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    57
							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
93
5a4dbe6f962c Type names in --types option are case insensitive
František Kučera <franta-hg@frantovo.cz>
parents: 79
diff changeset
    58
							namedTypes.put(namePart, getType(typePart.toUpperCase()));
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    59
						}
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    60
					}
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    61
					break;
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    62
				case Tokens.NAME_PREFIX:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    63
					options.setNamePrefix(fetchNext(args, ++i));
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    64
					break;
44
67581ec4396e named parameters: suffix (default is empty)
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    65
				case Tokens.NAME_SUFFIX:
67581ec4396e named parameters: suffix (default is empty)
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    66
					options.setNameSuffix(fetchNext(args, ++i));
67581ec4396e named parameters: suffix (default is empty)
František Kučera <franta-hg@frantovo.cz>
parents: 37
diff changeset
    67
					break;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
				case Tokens.DB:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    69
					options.setDatabaseName(fetchNext(args, ++i));
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
					break;
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
				case Tokens.SQL:
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    72
					options.setSql(fetchNext(args, ++i));
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
					break;
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    74
				case Tokens.SQL_IN:
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    75
					try {
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    76
						options.setSql(readString(in));
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    77
					} catch (IOException e) {
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    78
						throw new CLIParserException("Unable to read SQL from the input stream", e);
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    79
					}
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
    80
					break;
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
				case Tokens.BATCH:
2
72da10f632b5 more code
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    82
					options.setBatch(true);
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
					break;
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    84
				case Tokens.DATA: // --data is the last option
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    85
					for (i++; i < args.length; i++) {
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    86
						arg = args[i];
52
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    87
						Parameter parameter;
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    88
						if (numberedTypes.isEmpty()) {
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    89
							parameter = new Parameter(arg, null);
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    90
						} else {
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    91
							int paramIndex = options.getNumberedParameters().size();
68
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
    92
							SQLType paramType;
52
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    93
							try {
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    94
								paramType = numberedTypes.get(paramIndex);
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    95
							} catch (IndexOutOfBoundsException e) {
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    96
								throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    97
							} catch (NullPointerException e) {
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
    98
								throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    99
							}
52
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   100
							parameter = new Parameter(arg, paramType);
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   101
						}
52
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   102
						options.addNumberedParameter(parameter);
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   103
					}
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   104
					break;
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   105
				case Tokens.DATA_NAMED:
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   106
					for (i++; i < args.length; i++) {
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   107
						String paramName = args[i];
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   108
						String paramValue = fetchNext(args, ++i);
e2ba2af0ef40 new option --data-named (then follows name/value pairs without prefix/suffix)
František Kučera <franta-hg@frantovo.cz>
parents: 49
diff changeset
   109
						options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   110
					}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
					break;
246
277c18b48762 support custom relation names in the XML formatter (added --relation CLI option)
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   112
				case Tokens.RELATION:
277c18b48762 support custom relation names in the XML formatter (added --relation CLI option)
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   113
					options.addRelationName(fetchNext(args, ++i));
277c18b48762 support custom relation names in the XML formatter (added --relation CLI option)
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   114
					break;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   115
				case Tokens.FORMATTER:
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   116
					options.setFormatterName(fetchNext(args, ++i));
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   117
					break;
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   118
				case Tokens.DB_PROPERTY:
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   119
					options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   120
					break;
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   121
				case Tokens.FORMATTER_PROPERTY:
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   122
					options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   123
					break;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   124
				case Tokens.INFO_HELP:
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
   125
					options.addShowInfo(InfoType.HELP);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   126
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   127
				case Tokens.INFO_FORMATTERS:
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
   128
					options.addShowInfo(InfoType.FORMATTERS);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   129
					break;
209
8dfe037b3274 property annotations: grounds for --list-formatter-properties
František Kučera <franta-hg@frantovo.cz>
parents: 200
diff changeset
   130
				case Tokens.INFO_FORMATTER_PROPERTIES:
8dfe037b3274 property annotations: grounds for --list-formatter-properties
František Kučera <franta-hg@frantovo.cz>
parents: 200
diff changeset
   131
					options.addShowInfo(InfoType.FORMATTER_PROPERTIES);
8dfe037b3274 property annotations: grounds for --list-formatter-properties
František Kučera <franta-hg@frantovo.cz>
parents: 200
diff changeset
   132
					options.addFormatterNameToListProperties(fetchNext(args, ++i));
8dfe037b3274 property annotations: grounds for --list-formatter-properties
František Kučera <franta-hg@frantovo.cz>
parents: 200
diff changeset
   133
					break;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   134
				case Tokens.INFO_LICENSE:
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
   135
					options.addShowInfo(InfoType.LICENSE);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   136
					break;
200
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   137
				case Tokens.INFO_JAVA_PROPERTIES:
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   138
					options.addShowInfo(InfoType.JAVA_PROPERTIES);
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   139
					break;
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   140
				case Tokens.INFO_ENVIRONMENT_VARIABLES:
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   141
					options.addShowInfo(InfoType.ENVIRONMENT_VARIABLES);
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   142
					break;
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   143
				case Tokens.INFO_TYPES:
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
   144
					options.addShowInfo(InfoType.TYPES);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   145
					break;
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   146
				case Tokens.INFO_VERSION:
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
   147
					options.addShowInfo(InfoType.VERSION);
14
189b1260b942 show info basics
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   148
					break;
158
770b5009ec42 InfoLister: print list of available JDBC drivers: --list-jdbc-drivers
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   149
				case Tokens.INFO_JDBC_DRIVERS:
770b5009ec42 InfoLister: print list of available JDBC drivers: --list-jdbc-drivers
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   150
					options.addShowInfo(InfoType.JDBC_DRIVERS);
770b5009ec42 InfoLister: print list of available JDBC drivers: --list-jdbc-drivers
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   151
					break;
159
9632b23df30c InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
František Kučera <franta-hg@frantovo.cz>
parents: 158
diff changeset
   152
				case Tokens.INFO_JDBC_PROPERTIES:
9632b23df30c InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
František Kučera <franta-hg@frantovo.cz>
parents: 158
diff changeset
   153
					options.addShowInfo(InfoType.JDBC_PROPERTIES);
9632b23df30c InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
František Kučera <franta-hg@frantovo.cz>
parents: 158
diff changeset
   154
					options.addDatabaseNameToListProperties(fetchNext(args, ++i));
9632b23df30c InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
František Kučera <franta-hg@frantovo.cz>
parents: 158
diff changeset
   155
					break;
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   156
				case Tokens.INFO_DATABASES:
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
   157
					options.addShowInfo(InfoType.DATABASES);
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   158
					break;
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   159
				case Tokens.INFO_CONNECTION:
69
0befec5034c2 InfoLister, InfoType: switch → enum
František Kučera <franta-hg@frantovo.cz>
parents: 68
diff changeset
   160
					options.addShowInfo(InfoType.CONNECTION);
159
9632b23df30c InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
František Kučera <franta-hg@frantovo.cz>
parents: 158
diff changeset
   161
					options.addDatabaseNameToTest(fetchNext(args, ++i));
15
bbd335b5410c show info basics: list databases + test connection
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   162
					break;
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   163
				default:
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   164
					throw new CLIParserException("Unknown option: " + arg);
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
			}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
		}
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   167
		return options;
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   168
	}
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   169
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   170
	private String fetchNext(String[] args, int index) throws CLIParserException {
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   171
		if (index < args.length) {
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   172
			return args[index];
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   173
		} else {
9
2ec52027b97f better exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   174
			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   175
		}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
	}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
	public static class Tokens {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
79
e19a13ed19a9 more bash completion tags
František Kučera <franta-hg@frantovo.cz>
parents: 77
diff changeset
   180
		// bash-completion:options:
96
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   181
		public static final String DB = "--db"; // bash-completion:option // help: database name
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   182
		public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
96
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   183
		public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
166
5488c2dcf680 SQL from STDIN: --sql-in
František Kučera <franta-hg@frantovo.cz>
parents: 159
diff changeset
   184
		public static final String SQL_IN = "--sql-in"; // bash-completion:option // help: SQL query/command
96
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   185
		public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   186
		public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   187
		public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   188
		public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   189
		public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   190
		public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
246
277c18b48762 support custom relation names in the XML formatter (added --relation CLI option)
František Kučera <franta-hg@frantovo.cz>
parents: 238
diff changeset
   191
		public static final String RELATION = "--relation"; // bash-completion:option // help: name of the output relation (result set)
96
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   192
		public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
107
8189a4a28cd8 database/formatter properties also as CLI options
František Kučera <franta-hg@frantovo.cz>
parents: 99
diff changeset
   193
		public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
96
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   194
		public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   195
		public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   196
		public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
200
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   197
		public static final String INFO_JAVA_PROPERTIES = "--list-java-properties"; // bash-completion:option // help: list of Java system properties
2e351d7c26c4 InfoLister: new listings: --list-java-properties and --list-environment-variables
František Kučera <franta-hg@frantovo.cz>
parents: 166
diff changeset
   198
		public static final String INFO_ENVIRONMENT_VARIABLES = "--list-environment-variables"; // bash-completion:option // help: list of environment variables
96
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   199
		public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
217
84bab99dda50 --list-formatter-properties: better help/description
František Kučera <franta-hg@frantovo.cz>
parents: 209
diff changeset
   200
		public static final String INFO_FORMATTER_PROPERTIES = "--list-formatter-properties"; // bash-completion:option // help: print list of available properties for given formatter
99
b969176512dd Data types help: not all available types are currently supported (will be later by type converters)
František Kučera <franta-hg@frantovo.cz>
parents: 96
diff changeset
   201
		public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
158
770b5009ec42 InfoLister: print list of available JDBC drivers: --list-jdbc-drivers
František Kučera <franta-hg@frantovo.cz>
parents: 155
diff changeset
   202
		public static final String INFO_JDBC_DRIVERS = "--list-jdbc-drivers"; // bash-completion:option // help: list of available JDBC drivers
159
9632b23df30c InfoLister: list configured and configurable JDBC driver properties – option: --list-jdbc-properties
František Kučera <franta-hg@frantovo.cz>
parents: 158
diff changeset
   203
		public static final String INFO_JDBC_PROPERTIES = "--list-jdbc-properties"; // bash-completion:option // help: list of available JDBC properties for given database
96
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   204
		public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
7ae30649b30b basic generated help
František Kučera <franta-hg@frantovo.cz>
parents: 93
diff changeset
   205
		public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   206
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   207
		private Tokens() {
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   208
		}
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   209
	}
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   210
68
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
   211
	private SQLType getType(String typeString) throws CLIParserException {
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
   212
		try {
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
   213
			return SQLType.valueOf(typeString.trim());
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
   214
		} catch (IllegalArgumentException e) {
574cd7fbb5b2 SQLType enum wrapper for java.sql.Types
František Kučera <franta-hg@frantovo.cz>
parents: 62
diff changeset
   215
			throw new CLIParserException("Unsupported type: " + typeString, e);
8
4507cb9a0cf1 more exceptions
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   216
		}
4
f5c3350f3d78 data/types CLI options parsing
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
   217
	}
1
f32dac78d13a WOW some classes LOL; TODO: refactor
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
}