public void parseArgs()

in jisql/src/main/java/org/apache/util/sql/Jisql.java [611:782]


    public void parseArgs(String[] argv) throws Throwable {
        //
        // I'm sure that there has to be a better way but I couldn't find a
        // command lineparser that would let me ignore unknown arguments. so
        // walk through the list once to find the formatter. then, use the
        // command line parser to do it "for real"
        //
        String passwordValue = null;

        for (int argumentIndex = 0; argumentIndex < argv.length; argumentIndex++) {
            if ("-p".equalsIgnoreCase(argv[argumentIndex]) || "-password".equalsIgnoreCase(argv[argumentIndex])) {
                if (argv.length > argumentIndex + 1) {
                    passwordValue           = argv[argumentIndex + 1];
                    argv[argumentIndex + 1] = "";

                    break;
                }
            }
        }

        for (int argumentIndex = 0; argumentIndex < argv.length; argumentIndex++) {
            if (argv[argumentIndex].equals("-formatter")) {
                formatterClassName = argv[argumentIndex + 1];
                break;
            }
        }

        if (formatterClassName.compareToIgnoreCase("csv") == 0) {
            formatterClassName = csvFormatterClassName;
        } else if (formatterClassName.compareToIgnoreCase("xml") == 0) {
            formatterClassName = xmlFormatterClassName;
        } else if (formatterClassName.compareToIgnoreCase("default") == 0) {
            formatterClassName = defaultFormatterClassName;
        }

        formatter = (JisqlFormatter) Class.forName(formatterClassName).newInstance();

        OptionParser parser = new OptionParser();

        parser.posixlyCorrect(false);

        parser.accepts("c").withRequiredArg().ofType(String.class);
        parser.accepts("cstring").withRequiredArg().ofType(String.class);
        parser.accepts("debug");
        parser.accepts("driver").withRequiredArg().ofType(String.class);
        parser.accepts("driverinfo");
        parser.accepts("formatter").withRequiredArg().ofType(String.class);
        parser.accepts("help");
        parser.accepts("input").withRequiredArg().ofType(String.class);
        parser.accepts("password").withOptionalArg().ofType(String.class);
        parser.accepts("p").withOptionalArg().ofType(String.class);
        parser.accepts("pf").withRequiredArg().ofType(String.class);
        parser.accepts("query").withRequiredArg().ofType(String.class);
        parser.accepts("user").withRequiredArg().ofType(String.class);
        parser.accepts("u").withRequiredArg().ofType(String.class);

        formatter.setSupportedOptions(parser);

        OptionSet options = parser.parse(argv);

        if (options.has("help")) {
            usage();
            System.exit(1);
        }

        if (options.has("driver")) {
            driverName = (String) options.valueOf("driver");

            if (driverName.compareToIgnoreCase("jconnect4") == 0) {
                driverName = sybaseJConnect4DriverName;
            } else if (driverName.compareToIgnoreCase("jconnect5") == 0) {
                driverName = sybaseJConnect5DriverName;
            } else if (driverName.compareToIgnoreCase("jconnect6") == 0) {
                driverName = sybaseJConnect6DriverName;
            } else if (driverName.compareToIgnoreCase("oraclethin") == 0) {
                driverName = oracleThinDriverName;
            } else if (driverName.compareToIgnoreCase("db2app") == 0) {
                driverName = db2AppDriverName;
            } else if (driverName.compareToIgnoreCase("db2net") == 0) {
                driverName = db2NetDriverName;
            } else if (driverName.compareToIgnoreCase("cloudscape") == 0) {
                driverName = cloudscapeDriverName;
            } else if (driverName.compareToIgnoreCase("mssql") == 0) {
                driverName = msqlDriverName;
            } else if (driverName.compareToIgnoreCase("pointbase") == 0) {
                driverName = pointbaseDriverName;
            } else if (driverName.compareToIgnoreCase("postgresql") == 0) {
                driverName = postgresqlDriverName;
            } else if (driverName.compareToIgnoreCase("mysqlconj") == 0) {
                driverName = mySQLConnectJDriverName;
            } else if (driverName.compareToIgnoreCase("mysqlcaucho") == 0) {
                driverName = mySQLCauchoDriverName;
            } else if (driverName.compareToIgnoreCase("sapsajdbc4") == 0) {
                driverName = sapJDBC4SqlAnywhereDriverName;
            } else if (driverName.compareToIgnoreCase("sybasesajdbc4") == 0) {
                driverName = sybaseJDBC4SqlAnywhereDriverName;
            }
        }

        connectString = (String) options.valueOf("cstring");

        if (options.has("c")) {
            commandTerminator = (String) options.valueOf("c");
        }

        if (options.has("debug")) {
            printDebug = true;
        }

        if (options.has("user")) {
            userName = (String) options.valueOf("user");
        } else if (options.has("u")) {
            userName = (String) options.valueOf("u");
        }

        password = passwordValue;

        if (options.has("driverinfo")) {
            printDriverDetails = true;
        }

        if (options.has("input")) {
            inputFileName = (String) options.valueOf("input");
        }

        if (options.has("pf")) {
            passwordFileName = (String) options.valueOf("pf");
        }

        if (options.has("query")) {
            inputQuery = (String) options.valueOf("query");
        }

        if (driverName == null) {
            throw new Exception("driver name must exist");
        }

        if (connectString == null) {
            throw new Exception("connect string must exist");
        }

        if (userName == null) {
            throw new Exception("user name must exist");
        }

        if ((password == null) && (passwordFileName == null)) {
            password = "";
        } else if (password == null) {
            File passwordFile = new File(passwordFileName);

            if (!passwordFile.exists()) {
                throw new Exception("the password file \"" + passwordFileName + "\" does not exist");
            }

            if (!passwordFile.isFile()) {
                throw new Exception("the password file \"" + passwordFileName + "\" is not a normal file");
            }

            if (!passwordFile.canRead()) {
                throw new Exception("the password file \"" + passwordFileName + "\" is not readable");
            }

            try (BufferedReader reader = new BufferedReader(new FileReader(passwordFile))) {
                password = reader.readLine().trim();
            } catch (Exception e) {
                throw new Exception("An error occured reading the password file", e);
            }
            /* ignored */
        }

        formatter.consumeOptions(options);
    }