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