java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 11 Jan 2014 18:37:57 +0100
branchv_0
changeset 157 468e25828d07
parent 155 eb3676c6929b
child 158 770b5009ec42
permissions -rw-r--r--
fix typo in .deb package description
     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_DATABASES:
   129 					options.addShowInfo(InfoType.DATABASES);
   130 					break;
   131 				case Tokens.INFO_CONNECTION:
   132 					options.addShowInfo(InfoType.CONNECTION);
   133 					options.addDatabaseNamesToTest(fetchNext(args, ++i));
   134 					break;
   135 				default:
   136 					throw new CLIParserException("Unknown option: " + arg);
   137 			}
   138 		}
   139 		return options;
   140 	}
   141 
   142 	private String fetchNext(String[] args, int index) throws CLIParserException {
   143 		if (index < args.length) {
   144 			return args[index];
   145 		} else {
   146 			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
   147 		}
   148 	}
   149 
   150 	public static class Tokens {
   151 
   152 		// bash-completion:options:
   153 		public static final String DB = "--db"; // bash-completion:option // help: database name
   154 		public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
   155 		public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
   156 		public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
   157 		public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
   158 		public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
   159 		public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
   160 		public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
   161 		public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
   162 		public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
   163 		public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
   164 		public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
   165 		public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
   166 		public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
   167 		public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
   168 		public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
   169 		public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
   170 		public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
   171 
   172 		private Tokens() {
   173 		}
   174 	}
   175 
   176 	private SQLType getType(String typeString) throws CLIParserException {
   177 		try {
   178 			return SQLType.valueOf(typeString.trim());
   179 		} catch (IllegalArgumentException e) {
   180 			throw new CLIParserException("Unsupported type: " + typeString, e);
   181 		}
   182 	}
   183 }