Example usage for org.apache.commons.cli UnrecognizedOptionException getCause

List of usage examples for org.apache.commons.cli UnrecognizedOptionException getCause

Introduction

In this page you can find the example usage for org.apache.commons.cli UnrecognizedOptionException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.kawanfw.sql.WebServer.java

/**
 * Starts or stops the AceQL Web Server.
 * // ww  w  .  jav  a2  s  . c  o  m
 * @param args   the arguments of Web Server start/stop.
 * 
 * @throws ParseException      if any Exception when parsing command line
 * @throws IOException      if any I/O Exception
 * @throws ConnectException      if server is unable to connect to specified or default 9090 port
 * @throws SqlConfigurationException if any error in configuration properties file
 */
public static void main(String[] args)
        throws ParseException, IOException, ConnectException, SqlConfigurationException {

    if (args.length > 0) {
        debug("args[0]: " + args[0] + ":");
    }

    if (args.length > 1) {
        debug("args[1]: " + args[1] + ":");
    }

    Options options = createOptions();
    CommandLineParser parser = new GnuParser();

    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (UnrecognizedOptionException e) {
        System.out.println(e.getMessage());
        System.out.println();
        printUsage(options);
        System.exit(-1);
    }

    if (cmd.hasOption("help")) {
        printUsage(options);
        System.exit(-1);
    }

    if (cmd.hasOption("version")) {
        System.out.println(Version.getServerVersion());
        System.out.println();
        System.exit(-1);
    }

    if (!cmd.hasOption("start") && !cmd.hasOption("stop")) {
        System.err.println("Missing start or stop option." + " " + SqlTag.PLEASE_CORRECT);
        System.out.println();
        printUsage(options);
        System.exit(-1);
    }

    int port = WebServerApi.DEFAULT_PORT;

    if (cmd.hasOption("port")) {
        String portStr = cmd.getOptionValue("port");

        try {
            port = Integer.parseInt(portStr);
        } catch (Exception e) {
            displayErrorAndExit("The port parameter is not numeric: " + portStr + ".", options);
        }
    }

    if (cmd.hasOption("start")) {

        if (!cmd.hasOption("host")) {
            displayErrorAndExit("Missing host option.", options);
        }

        String host = cmd.getOptionValue("host");

        File propertiesFile = null;

        if (!cmd.hasOption("properties")) {

            String aceqlHome = System.getenv("ACEQL_HOME");

            if (aceqlHome != null) {

                // Remove surrounding " if present
                aceqlHome = aceqlHome.replaceAll("\"", "");

                if (aceqlHome.endsWith(File.separator)) {
                    aceqlHome = StringUtils.substringBeforeLast(aceqlHome, File.separator);
                }
                propertiesFile = new File(
                        aceqlHome + File.separator + "conf" + File.separator + "aceql-server.properties");
            } else {
                displayErrorAndExit("Missing properties option.", options);
            }

        } else {
            propertiesFile = new File(cmd.getOptionValue("properties"));
        }

        WebServerApi webServerApi = new WebServerApi();
        try {
            webServerApi.startServer(host, port, propertiesFile);
        } catch (SqlConfigurationException e) {
            System.err.println(SqlTag.SQL_PRODUCT_START_FAILURE + " " + SqlTag.USER_CONFIGURATION_FAILURE + " "
                    + e.getMessage());
            System.err.println();
            System.exit((-1));
        }

        catch (ConnectException e) {
            System.err.println(SqlTag.SQL_PRODUCT_START_FAILURE + " " + e.getMessage());
            System.err.println();
            System.exit((-1));
        }

        catch (IOException e) {

            if (e instanceof UnknownHostException) {
                System.err.println(SqlTag.SQL_PRODUCT_START_FAILURE + " " + "Unknow host: " + e.getMessage());
            } else {
                System.err.println(SqlTag.SQL_PRODUCT_START_FAILURE + " " + e.getMessage());
            }

            if (e.getCause() != null) {
                e.printStackTrace();
            }

            System.err.println();
            System.exit(-1);
        } catch (Exception e) {
            System.err.println(SqlTag.SQL_PRODUCT_START_FAILURE);
            e.printStackTrace();
            System.err.println();
            System.exit(-1);
        }

    } else {

        WebServerApi webServerApi = new WebServerApi();
        try {
            webServerApi.stopServer(port);

            System.out.println("SQL Web server running on port " + port + " successfully stopped!");
            System.out.println();
            System.exit(-1);
        } catch (IOException e) {

            if (e instanceof ConnectException) {
                System.err.println(e.getMessage());
            } else {
                System.err.println("Impossible to stop the SQL Web server running on port " + port);
                System.err.println(e.getMessage());

                if (e.getCause() != null) {
                    System.err.println("Java Exception Stack Trace:");
                    e.printStackTrace();
                }

            }

            System.err.println();
            System.exit(-1);
        }
    }

}

From source file:uk.trainwatch.app.util.Main.java

private static int run(String... args) throws Exception {

    // Load all of the utility implementations
    ServiceLoader<Utility> loader = ServiceLoader.load(Utility.class);
    Map<String, Utility> tools = StreamSupport.stream(loader.spliterator(), false)
            .collect(Collectors.toMap(Utility::getName, Function.identity()));

    Supplier<String> toolNames = () -> tools.keySet().stream().sorted()
            .collect(Collectors.joining(", ", "Available tools: ", ""));

    Consumer<Utility> showHelp = u -> new HelpFormatter().printHelp(u.getName(), u.getOptions());

    if (args.length == 0) {
        LOG.log(Level.INFO, toolNames);
        return 1;
    }//from   w  w w  . ja  va  2s. c om

    String toolName = args[0];

    if ("-?".equals(toolName) || "--help".equals(toolName)) {
        HelpFormatter hf = new HelpFormatter();
        tools.keySet().stream().sorted().map(tools::get).filter(Objects::nonNull).forEach(showHelp);
    } else {
        Utility util = getUtility(tools, toolNames, toolName);
        if (util != null) {

            String toolArgs[] = Arrays.copyOfRange(args, 1, args.length);

            if (toolArgs.length == 0 || "-?".equals(toolArgs[0]) || "--help".equals(toolArgs[0])) {
                new HelpFormatter().printHelp(util.getName(), util.getOptions());
            } else {
                try {
                    CommandLineParser parser = new BasicParser();
                    CommandLine cmd = parser.parse(util.getOptions(), toolArgs);
                    if (util.parseArgs(cmd)) {
                        // Simple banner for identifying whats being run
                        LoggingUtils.logBanner("Utility: " + util.getName());
                        try {
                            CDIUtils.inject(util);
                            util.call();
                            return 0;
                        } finally {
                            LoggingUtils.logBanner("End run of " + util.getName());
                        }
                    } else {
                        LOG.log(Level.WARNING, () -> "Failed to parse args " + cmd);
                        showHelp.accept(util);
                    }
                } catch (UnrecognizedOptionException ex) {
                    LOG.log(Level.WARNING, ex, ex::getMessage);
                    showHelp.accept(util);
                } catch (UncheckedSQLException ex) {
                    LOG.log(Level.SEVERE, null, ex.getCause());
                } catch (Exception ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    return 1;
}