franta-hg@16: /** franta-hg@16: * SQL-DK franta-hg@16: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@16: * franta-hg@16: * This program is free software: you can redistribute it and/or modify franta-hg@16: * it under the terms of the GNU General Public License as published by franta-hg@16: * the Free Software Foundation, either version 3 of the License, or franta-hg@16: * (at your option) any later version. franta-hg@16: * franta-hg@16: * This program is distributed in the hope that it will be useful, franta-hg@16: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@16: * GNU General Public License for more details. franta-hg@16: * franta-hg@16: * You should have received a copy of the GNU General Public License franta-hg@16: * along with this program. If not, see . franta-hg@16: */ franta-hg@1: package info.globalcode.sql.dk; franta-hg@1: franta-hg@4: import java.sql.Types; franta-hg@4: import java.util.ArrayList; franta-hg@4: import java.util.Collections; franta-hg@4: import java.util.HashMap; franta-hg@4: import java.util.List; franta-hg@4: import java.util.Map; franta-hg@4: franta-hg@1: /** franta-hg@1: * franta-hg@1: * @author Ing. František Kučera (frantovo.cz) franta-hg@1: */ franta-hg@5: public class CLIParser { franta-hg@1: franta-hg@4: public static final String TYPE_NAME_SEPARATOR = ":"; franta-hg@5: private final Map types; franta-hg@4: franta-hg@5: public CLIParser() { franta-hg@4: Map m = new HashMap<>(); franta-hg@4: m.put("int", Types.INTEGER); franta-hg@4: m.put("string", Types.VARCHAR); franta-hg@4: m.put("boolean", Types.BOOLEAN); franta-hg@4: /** franta-hg@4: * TODO: more types franta-hg@4: */ franta-hg@4: types = Collections.unmodifiableMap(m); franta-hg@4: } franta-hg@4: franta-hg@9: public CLIOptions parseOptions(String[] args) throws CLIParserException { franta-hg@1: CLIOptions options = new CLIOptions(); franta-hg@1: franta-hg@4: List numberedTypes = new ArrayList<>(); franta-hg@4: Map namedTypes = new HashMap<>(); franta-hg@2: franta-hg@1: for (int i = 0; i < args.length; i++) { franta-hg@1: String arg = args[i]; franta-hg@2: switch (arg) { franta-hg@2: case Tokens.TYPES: franta-hg@8: String typesString = fetchNext(args, ++i); franta-hg@4: franta-hg@10: for (String oneType : typesString.split(",")) { franta-hg@4: int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR); franta-hg@4: if (sepatratorIndex == -1) { franta-hg@4: numberedTypes.add(getType(oneType)); franta-hg@4: } else { franta-hg@12: String namePart = oneType.substring(0, sepatratorIndex).trim(); franta-hg@4: String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length()); franta-hg@4: namedTypes.put(namePart, getType(typePart)); franta-hg@4: } franta-hg@4: } franta-hg@2: break; franta-hg@2: case Tokens.NAME_PREFIX: franta-hg@8: options.setNamePrefix(fetchNext(args, ++i)); franta-hg@2: break; franta-hg@44: case Tokens.NAME_SUFFIX: franta-hg@44: options.setNameSuffix(fetchNext(args, ++i)); franta-hg@44: break; franta-hg@1: case Tokens.DB: franta-hg@8: options.setDatabaseName(fetchNext(args, ++i)); franta-hg@1: break; franta-hg@1: case Tokens.SQL: franta-hg@8: options.setSql(fetchNext(args, ++i)); franta-hg@1: break; franta-hg@1: case Tokens.BATCH: franta-hg@2: options.setBatch(true); franta-hg@1: break; franta-hg@4: case Tokens.DATA: // --data is the last option franta-hg@4: for (i++; i < args.length; i++) { franta-hg@4: arg = args[i]; franta-hg@4: franta-hg@44: if (arg.startsWith(options.getNamePrefix()) && arg.endsWith(options.getNameSuffix())) { // Named parameters: franta-hg@4: String paramName = arg.substring(options.getNamePrefix().length()); franta-hg@8: String paramValue = fetchNext(args, ++i); franta-hg@4: options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName))); franta-hg@8: } else { // Numbered parameters: franta-hg@8: Parameter parameter; franta-hg@8: if (numberedTypes.isEmpty()) { franta-hg@8: parameter = new Parameter(arg, null); franta-hg@8: } else { franta-hg@8: int paramIndex = options.getNumberedParameters().size(); franta-hg@8: int paramType; franta-hg@8: try { franta-hg@8: paramType = numberedTypes.get(paramIndex); franta-hg@8: } catch (IndexOutOfBoundsException e) { franta-hg@9: throw new CLIParserException("Missing type for parameter #" + paramIndex, e); franta-hg@8: } catch (NullPointerException e) { franta-hg@9: throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e); franta-hg@8: } franta-hg@8: parameter = new Parameter(arg, paramType); franta-hg@4: } franta-hg@8: options.addNumberedParameter(parameter); franta-hg@4: } franta-hg@4: } franta-hg@1: break; franta-hg@14: case Tokens.FORMATTER: franta-hg@14: options.setFormatterName(fetchNext(args, ++i)); franta-hg@14: break; franta-hg@14: case Tokens.INFO_HELP: franta-hg@14: options.addShowInfo(CLIOptions.INFO_TYPE.HELP); franta-hg@14: break; franta-hg@14: case Tokens.INFO_FORMATTERS: franta-hg@14: options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS); franta-hg@14: break; franta-hg@14: case Tokens.INFO_LICENSE: franta-hg@14: options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE); franta-hg@14: break; franta-hg@14: case Tokens.INFO_TYPES: franta-hg@14: options.addShowInfo(CLIOptions.INFO_TYPE.TYPES); franta-hg@14: break; franta-hg@14: case Tokens.INFO_VERSION: franta-hg@14: options.addShowInfo(CLIOptions.INFO_TYPE.VERSION); franta-hg@14: break; franta-hg@15: case Tokens.INFO_DATABASES: franta-hg@15: options.addShowInfo(CLIOptions.INFO_TYPE.DATABASES); franta-hg@15: break; franta-hg@15: case Tokens.INFO_CONNECTION: franta-hg@15: options.addShowInfo(CLIOptions.INFO_TYPE.CONNECTION); franta-hg@15: options.setDatabaseNameToTest(fetchNext(args, ++i)); franta-hg@15: break; franta-hg@4: default: franta-hg@9: throw new CLIParserException("Unknown option: " + arg); franta-hg@1: } franta-hg@1: } franta-hg@8: return options; franta-hg@8: } franta-hg@1: franta-hg@9: private String fetchNext(String[] args, int index) throws CLIParserException { franta-hg@8: if (index < args.length) { franta-hg@8: return args[index]; franta-hg@8: } else { franta-hg@9: throw new CLIParserException("Expecting value for option: " + args[index - 1]); franta-hg@8: } franta-hg@1: } franta-hg@1: franta-hg@1: public static class Tokens { franta-hg@1: franta-hg@1: public static final String DB = "--db"; franta-hg@1: public static final String SQL = "--sql"; franta-hg@1: public static final String BATCH = "--batch"; franta-hg@1: public static final String DATA = "--data"; franta-hg@2: public static final String NAME_PREFIX = "--name-prefix"; franta-hg@44: public static final String NAME_SUFFIX = "--name-suffix"; franta-hg@1: public static final String TYPES = "--types"; franta-hg@14: public static final String FORMATTER = "--formatter"; franta-hg@14: public static final String INFO_HELP = "--help"; franta-hg@14: public static final String INFO_VERSION = "--version"; franta-hg@14: public static final String INFO_LICENSE = "--license"; franta-hg@14: public static final String INFO_FORMATTERS = "--list-formatters"; franta-hg@14: public static final String INFO_TYPES = "--list-types"; franta-hg@15: public static final String INFO_DATABASES = "--list-databases"; franta-hg@15: public static final String INFO_CONNECTION = "--test-connection"; franta-hg@1: franta-hg@1: private Tokens() { franta-hg@1: } franta-hg@1: } franta-hg@4: franta-hg@9: private int getType(String typeString) throws CLIParserException { franta-hg@10: Integer type = types.get(typeString.trim()); franta-hg@8: if (type == null) { franta-hg@9: throw new CLIParserException("Unsupported type: " + typeString); franta-hg@8: } else { franta-hg@8: return type; franta-hg@8: } franta-hg@4: } franta-hg@1: }