java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 04 Jan 2014 21:21:04 +0100
branchv_0
changeset 133 a1bf1465df89
parent 107 8189a4a28cd8
child 155 eb3676c6929b
permissions -rw-r--r--
Properties: setLastDefaults()
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@107
    21
import info.globalcode.sql.dk.configuration.Property;
franta-hg@4
    22
import java.util.ArrayList;
franta-hg@4
    23
import java.util.HashMap;
franta-hg@4
    24
import java.util.List;
franta-hg@4
    25
import java.util.Map;
franta-hg@4
    26
franta-hg@1
    27
/**
franta-hg@1
    28
 *
franta-hg@1
    29
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@1
    30
 */
franta-hg@5
    31
public class CLIParser {
franta-hg@1
    32
franta-hg@4
    33
	public static final String TYPE_NAME_SEPARATOR = ":";
franta-hg@4
    34
franta-hg@9
    35
	public CLIOptions parseOptions(String[] args) throws CLIParserException {
franta-hg@1
    36
		CLIOptions options = new CLIOptions();
franta-hg@1
    37
franta-hg@68
    38
		List<SQLType> numberedTypes = new ArrayList<>();
franta-hg@68
    39
		Map<String, SQLType> namedTypes = new HashMap<>();
franta-hg@2
    40
franta-hg@1
    41
		for (int i = 0; i < args.length; i++) {
franta-hg@1
    42
			String arg = args[i];
franta-hg@2
    43
			switch (arg) {
franta-hg@2
    44
				case Tokens.TYPES:
franta-hg@8
    45
					String typesString = fetchNext(args, ++i);
franta-hg@4
    46
franta-hg@10
    47
					for (String oneType : typesString.split(",")) {
franta-hg@4
    48
						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
franta-hg@4
    49
						if (sepatratorIndex == -1) {
franta-hg@93
    50
							numberedTypes.add(getType(oneType.toUpperCase()));
franta-hg@4
    51
						} else {
franta-hg@12
    52
							String namePart = oneType.substring(0, sepatratorIndex).trim();
franta-hg@4
    53
							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
franta-hg@93
    54
							namedTypes.put(namePart, getType(typePart.toUpperCase()));
franta-hg@4
    55
						}
franta-hg@4
    56
					}
franta-hg@2
    57
					break;
franta-hg@2
    58
				case Tokens.NAME_PREFIX:
franta-hg@8
    59
					options.setNamePrefix(fetchNext(args, ++i));
franta-hg@2
    60
					break;
franta-hg@44
    61
				case Tokens.NAME_SUFFIX:
franta-hg@44
    62
					options.setNameSuffix(fetchNext(args, ++i));
franta-hg@44
    63
					break;
franta-hg@1
    64
				case Tokens.DB:
franta-hg@8
    65
					options.setDatabaseName(fetchNext(args, ++i));
franta-hg@1
    66
					break;
franta-hg@1
    67
				case Tokens.SQL:
franta-hg@8
    68
					options.setSql(fetchNext(args, ++i));
franta-hg@1
    69
					break;
franta-hg@1
    70
				case Tokens.BATCH:
franta-hg@2
    71
					options.setBatch(true);
franta-hg@1
    72
					break;
franta-hg@4
    73
				case Tokens.DATA: // --data is the last option
franta-hg@4
    74
					for (i++; i < args.length; i++) {
franta-hg@4
    75
						arg = args[i];
franta-hg@52
    76
						Parameter parameter;
franta-hg@52
    77
						if (numberedTypes.isEmpty()) {
franta-hg@52
    78
							parameter = new Parameter(arg, null);
franta-hg@52
    79
						} else {
franta-hg@52
    80
							int paramIndex = options.getNumberedParameters().size();
franta-hg@68
    81
							SQLType paramType;
franta-hg@52
    82
							try {
franta-hg@52
    83
								paramType = numberedTypes.get(paramIndex);
franta-hg@52
    84
							} catch (IndexOutOfBoundsException e) {
franta-hg@52
    85
								throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
franta-hg@52
    86
							} catch (NullPointerException e) {
franta-hg@52
    87
								throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
franta-hg@4
    88
							}
franta-hg@52
    89
							parameter = new Parameter(arg, paramType);
franta-hg@4
    90
						}
franta-hg@52
    91
						options.addNumberedParameter(parameter);
franta-hg@52
    92
					}
franta-hg@52
    93
					break;
franta-hg@52
    94
				case Tokens.DATA_NAMED:
franta-hg@52
    95
					for (i++; i < args.length; i++) {
franta-hg@52
    96
						String paramName = args[i];
franta-hg@52
    97
						String paramValue = fetchNext(args, ++i);
franta-hg@52
    98
						options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
franta-hg@4
    99
					}
franta-hg@1
   100
					break;
franta-hg@14
   101
				case Tokens.FORMATTER:
franta-hg@14
   102
					options.setFormatterName(fetchNext(args, ++i));
franta-hg@14
   103
					break;
franta-hg@107
   104
				case Tokens.DB_PROPERTY:
franta-hg@107
   105
					options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
franta-hg@107
   106
					break;
franta-hg@107
   107
				case Tokens.FORMATTER_PROPERTY:
franta-hg@107
   108
					options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
franta-hg@107
   109
					break;
franta-hg@14
   110
				case Tokens.INFO_HELP:
franta-hg@69
   111
					options.addShowInfo(InfoType.HELP);
franta-hg@14
   112
					break;
franta-hg@14
   113
				case Tokens.INFO_FORMATTERS:
franta-hg@69
   114
					options.addShowInfo(InfoType.FORMATTERS);
franta-hg@14
   115
					break;
franta-hg@14
   116
				case Tokens.INFO_LICENSE:
franta-hg@69
   117
					options.addShowInfo(InfoType.LICENSE);
franta-hg@14
   118
					break;
franta-hg@14
   119
				case Tokens.INFO_TYPES:
franta-hg@69
   120
					options.addShowInfo(InfoType.TYPES);
franta-hg@14
   121
					break;
franta-hg@14
   122
				case Tokens.INFO_VERSION:
franta-hg@69
   123
					options.addShowInfo(InfoType.VERSION);
franta-hg@14
   124
					break;
franta-hg@15
   125
				case Tokens.INFO_DATABASES:
franta-hg@69
   126
					options.addShowInfo(InfoType.DATABASES);
franta-hg@15
   127
					break;
franta-hg@15
   128
				case Tokens.INFO_CONNECTION:
franta-hg@69
   129
					options.addShowInfo(InfoType.CONNECTION);
franta-hg@74
   130
					options.addDatabaseNameToTest(fetchNext(args, ++i));
franta-hg@15
   131
					break;
franta-hg@4
   132
				default:
franta-hg@9
   133
					throw new CLIParserException("Unknown option: " + arg);
franta-hg@1
   134
			}
franta-hg@1
   135
		}
franta-hg@8
   136
		return options;
franta-hg@8
   137
	}
franta-hg@1
   138
franta-hg@9
   139
	private String fetchNext(String[] args, int index) throws CLIParserException {
franta-hg@8
   140
		if (index < args.length) {
franta-hg@8
   141
			return args[index];
franta-hg@8
   142
		} else {
franta-hg@9
   143
			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
franta-hg@8
   144
		}
franta-hg@1
   145
	}
franta-hg@1
   146
franta-hg@1
   147
	public static class Tokens {
franta-hg@1
   148
franta-hg@79
   149
		// bash-completion:options:
franta-hg@96
   150
		public static final String DB = "--db"; // bash-completion:option // help: database name
franta-hg@107
   151
		public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
franta-hg@96
   152
		public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
franta-hg@96
   153
		public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
franta-hg@96
   154
		public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
franta-hg@96
   155
		public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
franta-hg@96
   156
		public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
franta-hg@96
   157
		public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
franta-hg@96
   158
		public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
franta-hg@96
   159
		public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
franta-hg@107
   160
		public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
franta-hg@96
   161
		public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
franta-hg@96
   162
		public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
franta-hg@96
   163
		public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
franta-hg@96
   164
		public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
franta-hg@99
   165
		public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
franta-hg@96
   166
		public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
franta-hg@96
   167
		public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
franta-hg@1
   168
franta-hg@1
   169
		private Tokens() {
franta-hg@1
   170
		}
franta-hg@1
   171
	}
franta-hg@4
   172
franta-hg@68
   173
	private SQLType getType(String typeString) throws CLIParserException {
franta-hg@68
   174
		try {
franta-hg@68
   175
			return SQLType.valueOf(typeString.trim());
franta-hg@68
   176
		} catch (IllegalArgumentException e) {
franta-hg@68
   177
			throw new CLIParserException("Unsupported type: " + typeString, e);
franta-hg@8
   178
		}
franta-hg@4
   179
	}
franta-hg@1
   180
}