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