Search in sources :

Example 21 with OptionException

use of joptsimple.OptionException in project geode by apache.

the class Launcher method parseOptions.

private int parseOptions(final String... args) {
    OptionSet parsedOptions;
    try {
        parsedOptions = this.commandLineParser.parse(args);
    } catch (OptionException e) {
        System.err.println(CliStrings.format(MSG_INVALID_COMMAND_OR_OPTION, CliUtil.arrayToString(args)));
        return ExitShellRequest.FATAL_EXIT.getExitCode();
    }
    boolean launchShell = true;
    boolean onlyPrintUsage = parsedOptions.has(HELP_OPTION);
    if (parsedOptions.has(EXECUTE_OPTION) || onlyPrintUsage) {
        launchShell = false;
    }
    Gfsh gfsh = null;
    try {
        gfsh = Gfsh.getInstance(launchShell, args, new GfshConfig());
        this.startupTimeLogHelper.logStartupTime();
    } catch (ClassNotFoundException cnfex) {
        log(cnfex, gfsh);
    } catch (IOException ioex) {
        log(ioex, gfsh);
    } catch (IllegalStateException isex) {
        System.err.println("ERROR : " + isex.getMessage());
    }
    ExitShellRequest exitRequest = ExitShellRequest.NORMAL_EXIT;
    if (gfsh != null) {
        try {
            if (launchShell) {
                gfsh.start();
                gfsh.waitForComplete();
                exitRequest = gfsh.getExitShellRequest();
            } else if (onlyPrintUsage) {
                printUsage(gfsh, System.out);
            } else {
                @SuppressWarnings("unchecked") List<String> commandsToExecute = (List<String>) parsedOptions.valuesOf(EXECUTE_OPTION);
                // Execute all of the commands in the list, one at a time.
                for (int i = 0; i < commandsToExecute.size() && exitRequest == ExitShellRequest.NORMAL_EXIT; i++) {
                    String command = commandsToExecute.get(i);
                    // sanitize the output string to not show the password
                    System.out.println(GfshParser.LINE_SEPARATOR + "(" + (i + 1) + ") Executing - " + GfshHistory.redact(command) + GfshParser.LINE_SEPARATOR);
                    if (!gfsh.executeScriptLine(command) || gfsh.getLastExecutionStatus() != 0) {
                        exitRequest = ExitShellRequest.FATAL_EXIT;
                    }
                }
            }
        } catch (InterruptedException iex) {
            log(iex, gfsh);
        }
    }
    return exitRequest.getExitCode();
}
Also used : GfshConfig(org.apache.geode.management.internal.cli.shell.GfshConfig) Gfsh(org.apache.geode.management.internal.cli.shell.Gfsh) OptionException(joptsimple.OptionException) List(java.util.List) IOException(java.io.IOException) OptionSet(joptsimple.OptionSet) ExitShellRequest(org.springframework.shell.core.ExitShellRequest)

Example 22 with OptionException

use of joptsimple.OptionException in project bitsquare by bitsquare.

the class BitsquareExecutable method execute.

public void execute(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    parser.accepts(HELP_KEY, "This help text").forHelp();
    this.customizeOptionParsing(parser);
    OptionSet options;
    try {
        options = parser.parse(args);
        if (options.has(HELP_KEY)) {
            parser.printHelpOn(System.out);
            System.exit(EXIT_SUCCESS);
            return;
        }
    } catch (OptionException ex) {
        System.out.println("error: " + ex.getMessage());
        System.out.println();
        parser.printHelpOn(System.out);
        System.exit(EXIT_FAILURE);
        return;
    }
    this.doExecute(options);
}
Also used : OptionException(joptsimple.OptionException) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Example 23 with OptionException

use of joptsimple.OptionException in project bitsquare by bitsquare.

the class StatisticsMain method main.

public static void main(String[] args) throws Exception {
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Statistics").setDaemon(true).build();
    UserThread.setExecutor(Executors.newSingleThreadExecutor(threadFactory));
    // We don't want to do the full argument parsing here as that might easily change in update versions
    // So we only handle the absolute minimum which is APP_NAME, APP_DATA_DIR_KEY and USER_DATA_DIR
    BitsquareEnvironment.setDefaultAppName("Bitsquare_statistics");
    OptionParser parser = new OptionParser();
    parser.allowsUnrecognizedOptions();
    parser.accepts(AppOptionKeys.USER_DATA_DIR_KEY, description("User data directory", DEFAULT_USER_DATA_DIR)).withRequiredArg();
    parser.accepts(AppOptionKeys.APP_NAME_KEY, description("Application name", DEFAULT_APP_NAME)).withRequiredArg();
    OptionSet options;
    try {
        options = parser.parse(args);
    } catch (OptionException ex) {
        System.out.println("error: " + ex.getMessage());
        System.out.println();
        parser.printHelpOn(System.out);
        System.exit(EXIT_FAILURE);
        return;
    }
    BitsquareEnvironment bitsquareEnvironment = new BitsquareEnvironment(options);
    // need to call that before BitsquareAppMain().execute(args)
    BitsquareExecutable.initAppDir(bitsquareEnvironment.getProperty(AppOptionKeys.APP_DATA_DIR_KEY));
    // For some reason the JavaFX launch process results in us losing the thread context class loader: reset it.
    // In order to work around a bug in JavaFX 8u25 and below, you must include the following code as the first line of your realMain method:
    Thread.currentThread().setContextClassLoader(StatisticsMain.class.getClassLoader());
    new StatisticsMain().execute(args);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) OptionException(joptsimple.OptionException) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Aggregations

OptionException (joptsimple.OptionException)23 OptionSet (joptsimple.OptionSet)20 OptionParser (joptsimple.OptionParser)14 File (java.io.File)4 IOException (java.io.IOException)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)2 BitsquareEnvironment (io.bitsquare.app.BitsquareEnvironment)2 PrintWriter (java.io.PrintWriter)2 LinkedList (java.util.LinkedList)2 ThreadFactory (java.util.concurrent.ThreadFactory)2 BuiltinHelpFormatter (joptsimple.BuiltinHelpFormatter)2 VoldemortApplicationException (voldemort.VoldemortApplicationException)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 StringWriter (java.io.StringWriter)1 Socket (java.net.Socket)1