Search in sources :

Example 11 with MissingArgumentException

use of org.apache.commons.cli.MissingArgumentException in project hadoop by apache.

the class QueueCLI method run.

@Override
public int run(String[] args) throws Exception {
    Options opts = new Options();
    opts.addOption(STATUS_CMD, true, "List queue information about given queue.");
    opts.addOption(HELP_CMD, false, "Displays help for all commands.");
    opts.getOption(STATUS_CMD).setArgName("Queue Name");
    CommandLine cliParser = null;
    try {
        cliParser = new GnuParser().parse(opts, args);
    } catch (MissingArgumentException ex) {
        sysout.println("Missing argument for options");
        printUsage(opts);
        return -1;
    }
    if (cliParser.hasOption(STATUS_CMD)) {
        if (args.length != 2) {
            printUsage(opts);
            return -1;
        }
        return listQueue(cliParser.getOptionValue(STATUS_CMD));
    } else if (cliParser.hasOption(HELP_CMD)) {
        printUsage(opts);
        return 0;
    } else {
        syserr.println("Invalid Command Usage : ");
        printUsage(opts);
        return -1;
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) GnuParser(org.apache.commons.cli.GnuParser)

Example 12 with MissingArgumentException

use of org.apache.commons.cli.MissingArgumentException in project hadoop by apache.

the class ApplicationCLI method run.

@Override
public int run(String[] args) throws Exception {
    Options opts = new Options();
    String title = null;
    if (args.length > 0 && args[0].equalsIgnoreCase(APPLICATION)) {
        title = APPLICATION;
        opts.addOption(STATUS_CMD, true, "Prints the status of the application.");
        opts.addOption(LIST_CMD, false, "List applications. " + "Supports optional use of -appTypes to filter applications " + "based on application type, -appStates to filter applications " + "based on application state and -appTags to filter applications " + "based on application tag.");
        opts.addOption(MOVE_TO_QUEUE_CMD, true, "Moves the application to a " + "different queue. Deprecated command. Use 'changeQueue' instead.");
        opts.addOption(QUEUE_CMD, true, "Works with the movetoqueue command to" + " specify which queue to move an application to.");
        opts.addOption(HELP_CMD, false, "Displays help for all commands.");
        Option appTypeOpt = new Option(APP_TYPE_CMD, true, "Works with -list to " + "filter applications based on " + "input comma-separated list of application types.");
        appTypeOpt.setValueSeparator(',');
        appTypeOpt.setArgs(Option.UNLIMITED_VALUES);
        appTypeOpt.setArgName("Types");
        opts.addOption(appTypeOpt);
        Option appStateOpt = new Option(APP_STATE_CMD, true, "Works with -list " + "to filter applications based on input comma-separated list of " + "application states. " + getAllValidApplicationStates());
        appStateOpt.setValueSeparator(',');
        appStateOpt.setArgs(Option.UNLIMITED_VALUES);
        appStateOpt.setArgName("States");
        opts.addOption(appStateOpt);
        Option appTagOpt = new Option(APP_TAG_CMD, true, "Works with -list to " + "filter applications based on input comma-separated list of " + "application tags.");
        appTagOpt.setValueSeparator(',');
        appTagOpt.setArgs(Option.UNLIMITED_VALUES);
        appTagOpt.setArgName("Tags");
        opts.addOption(appTagOpt);
        opts.addOption(APP_ID, true, "Specify Application Id to be operated");
        opts.addOption(UPDATE_PRIORITY, true, "update priority of an application. ApplicationId can be" + " passed using 'appId' option.");
        opts.addOption(UPDATE_LIFETIME, true, "update timeout of an application from NOW. ApplicationId can be" + " passed using 'appId' option. Timeout value is in seconds.");
        opts.addOption(CHANGE_APPLICATION_QUEUE, true, "Moves application to a new queue. ApplicationId can be" + " passed using 'appId' option. 'movetoqueue' command is" + " deprecated, this new command 'changeQueue' performs same" + " functionality.");
        Option killOpt = new Option(KILL_CMD, true, "Kills the application. " + "Set of applications can be provided separated with space");
        killOpt.setValueSeparator(' ');
        killOpt.setArgs(Option.UNLIMITED_VALUES);
        killOpt.setArgName("Application ID");
        opts.addOption(killOpt);
        opts.getOption(MOVE_TO_QUEUE_CMD).setArgName("Application ID");
        opts.getOption(QUEUE_CMD).setArgName("Queue Name");
        opts.getOption(STATUS_CMD).setArgName("Application ID");
        opts.getOption(APP_ID).setArgName("Application ID");
        opts.getOption(UPDATE_PRIORITY).setArgName("Priority");
        opts.getOption(UPDATE_LIFETIME).setArgName("Timeout");
        opts.getOption(CHANGE_APPLICATION_QUEUE).setArgName("Queue Name");
    } else if (args.length > 0 && args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
        title = APPLICATION_ATTEMPT;
        opts.addOption(STATUS_CMD, true, "Prints the status of the application attempt.");
        opts.addOption(LIST_CMD, true, "List application attempts for application.");
        opts.addOption(FAIL_CMD, true, "Fails application attempt.");
        opts.addOption(HELP_CMD, false, "Displays help for all commands.");
        opts.getOption(STATUS_CMD).setArgName("Application Attempt ID");
        opts.getOption(LIST_CMD).setArgName("Application ID");
        opts.getOption(FAIL_CMD).setArgName("Application Attempt ID");
    } else if (args.length > 0 && args[0].equalsIgnoreCase(CONTAINER)) {
        title = CONTAINER;
        opts.addOption(STATUS_CMD, true, "Prints the status of the container.");
        opts.addOption(LIST_CMD, true, "List containers for application attempt.");
        opts.addOption(HELP_CMD, false, "Displays help for all commands.");
        opts.getOption(STATUS_CMD).setArgName("Container ID");
        opts.getOption(LIST_CMD).setArgName("Application Attempt ID");
        opts.addOption(SIGNAL_CMD, true, "Signal the container. The available signal commands are " + java.util.Arrays.asList(SignalContainerCommand.values()) + " Default command is OUTPUT_THREAD_DUMP.");
        opts.getOption(SIGNAL_CMD).setArgName("container ID [signal command]");
        opts.getOption(SIGNAL_CMD).setArgs(3);
    }
    int exitCode = -1;
    CommandLine cliParser = null;
    try {
        cliParser = new GnuParser().parse(opts, args);
    } catch (MissingArgumentException ex) {
        sysout.println("Missing argument for options");
        printUsage(title, opts);
        return exitCode;
    }
    if (cliParser.hasOption(STATUS_CMD)) {
        if (args.length != 3) {
            printUsage(title, opts);
            return exitCode;
        }
        if (args[0].equalsIgnoreCase(APPLICATION)) {
            exitCode = printApplicationReport(cliParser.getOptionValue(STATUS_CMD));
        } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
            exitCode = printApplicationAttemptReport(cliParser.getOptionValue(STATUS_CMD));
        } else if (args[0].equalsIgnoreCase(CONTAINER)) {
            exitCode = printContainerReport(cliParser.getOptionValue(STATUS_CMD));
        }
        return exitCode;
    } else if (cliParser.hasOption(LIST_CMD)) {
        if (args[0].equalsIgnoreCase(APPLICATION)) {
            allAppStates = false;
            Set<String> appTypes = new HashSet<String>();
            if (cliParser.hasOption(APP_TYPE_CMD)) {
                String[] types = cliParser.getOptionValues(APP_TYPE_CMD);
                if (types != null) {
                    for (String type : types) {
                        if (!type.trim().isEmpty()) {
                            appTypes.add(StringUtils.toUpperCase(type).trim());
                        }
                    }
                }
            }
            EnumSet<YarnApplicationState> appStates = EnumSet.noneOf(YarnApplicationState.class);
            if (cliParser.hasOption(APP_STATE_CMD)) {
                String[] states = cliParser.getOptionValues(APP_STATE_CMD);
                if (states != null) {
                    for (String state : states) {
                        if (!state.trim().isEmpty()) {
                            if (state.trim().equalsIgnoreCase(ALLSTATES_OPTION)) {
                                allAppStates = true;
                                break;
                            }
                            try {
                                appStates.add(YarnApplicationState.valueOf(StringUtils.toUpperCase(state).trim()));
                            } catch (IllegalArgumentException ex) {
                                sysout.println("The application state " + state + " is invalid.");
                                sysout.println(getAllValidApplicationStates());
                                return exitCode;
                            }
                        }
                    }
                }
            }
            Set<String> appTags = new HashSet<String>();
            if (cliParser.hasOption(APP_TAG_CMD)) {
                String[] tags = cliParser.getOptionValues(APP_TAG_CMD);
                if (tags != null) {
                    for (String tag : tags) {
                        if (!tag.trim().isEmpty()) {
                            appTags.add(tag.trim());
                        }
                    }
                }
            }
            listApplications(appTypes, appStates, appTags);
        } else if (args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
            if (args.length != 3) {
                printUsage(title, opts);
                return exitCode;
            }
            listApplicationAttempts(cliParser.getOptionValue(LIST_CMD));
        } else if (args[0].equalsIgnoreCase(CONTAINER)) {
            if (args.length != 3) {
                printUsage(title, opts);
                return exitCode;
            }
            listContainers(cliParser.getOptionValue(LIST_CMD));
        }
    } else if (cliParser.hasOption(KILL_CMD)) {
        if (args.length < 3 || hasAnyOtherCLIOptions(cliParser, opts, KILL_CMD)) {
            printUsage(title, opts);
            return exitCode;
        }
        return killApplication(cliParser.getOptionValues(KILL_CMD));
    } else if (cliParser.hasOption(MOVE_TO_QUEUE_CMD)) {
        if (!cliParser.hasOption(QUEUE_CMD)) {
            printUsage(title, opts);
            return exitCode;
        }
        moveApplicationAcrossQueues(cliParser.getOptionValue(MOVE_TO_QUEUE_CMD), cliParser.getOptionValue(QUEUE_CMD));
    } else if (cliParser.hasOption(FAIL_CMD)) {
        if (!args[0].equalsIgnoreCase(APPLICATION_ATTEMPT)) {
            printUsage(title, opts);
            return exitCode;
        }
        failApplicationAttempt(cliParser.getOptionValue(FAIL_CMD));
    } else if (cliParser.hasOption(HELP_CMD)) {
        printUsage(title, opts);
        return 0;
    } else if (cliParser.hasOption(UPDATE_PRIORITY)) {
        if (!cliParser.hasOption(APP_ID)) {
            printUsage(title, opts);
            return exitCode;
        }
        updateApplicationPriority(cliParser.getOptionValue(APP_ID), cliParser.getOptionValue(UPDATE_PRIORITY));
    } else if (cliParser.hasOption(UPDATE_LIFETIME)) {
        if (!cliParser.hasOption(APP_ID)) {
            printUsage(title, opts);
            return exitCode;
        }
        long timeoutInSec = Long.parseLong(cliParser.getOptionValue(UPDATE_LIFETIME));
        updateApplicationTimeout(cliParser.getOptionValue(APP_ID), ApplicationTimeoutType.LIFETIME, timeoutInSec);
    } else if (cliParser.hasOption(CHANGE_APPLICATION_QUEUE)) {
        if (!cliParser.hasOption(APP_ID)) {
            printUsage(title, opts);
            return exitCode;
        }
        moveApplicationAcrossQueues(cliParser.getOptionValue(APP_ID), cliParser.getOptionValue(CHANGE_APPLICATION_QUEUE));
    } else if (cliParser.hasOption(SIGNAL_CMD)) {
        if (args.length < 3 || args.length > 4) {
            printUsage(title, opts);
            return exitCode;
        }
        final String[] signalArgs = cliParser.getOptionValues(SIGNAL_CMD);
        final String containerId = signalArgs[0];
        SignalContainerCommand command = SignalContainerCommand.OUTPUT_THREAD_DUMP;
        if (signalArgs.length == 2) {
            command = SignalContainerCommand.valueOf(signalArgs[1]);
        }
        signalToContainer(containerId, command);
    } else {
        syserr.println("Invalid Command Usage : ");
        printUsage(title, opts);
    }
    return 0;
}
Also used : Options(org.apache.commons.cli.Options) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) EnumSet(java.util.EnumSet) GnuParser(org.apache.commons.cli.GnuParser) YarnApplicationState(org.apache.hadoop.yarn.api.records.YarnApplicationState) CommandLine(org.apache.commons.cli.CommandLine) SignalContainerCommand(org.apache.hadoop.yarn.api.records.SignalContainerCommand) Option(org.apache.commons.cli.Option)

Example 13 with MissingArgumentException

use of org.apache.commons.cli.MissingArgumentException in project hadoop by apache.

the class RMAdminCLI method handleRemoveFromClusterNodeLabels.

private int handleRemoveFromClusterNodeLabels(String[] args, String cmd, boolean isHAEnabled) throws IOException, YarnException, ParseException {
    Options opts = new Options();
    opts.addOption("removeFromClusterNodeLabels", true, "Remove From cluster node labels.");
    opts.addOption("directlyAccessNodeLabelStore", false, "Directly access node label store.");
    int exitCode = -1;
    CommandLine cliParser = null;
    try {
        cliParser = new GnuParser().parse(opts, args);
    } catch (MissingArgumentException ex) {
        System.err.println(NO_LABEL_ERR_MSG);
        printUsage(args[0], isHAEnabled);
        return exitCode;
    }
    Set<String> labels = buildNodeLabelNamesFromStr(cliParser.getOptionValue("removeFromClusterNodeLabels"));
    if (cliParser.hasOption("directlyAccessNodeLabelStore")) {
        getNodeLabelManagerInstance(getConf()).removeFromClusterNodeLabels(labels);
    } else {
        ResourceManagerAdministrationProtocol adminProtocol = createAdminProtocol();
        RemoveFromClusterNodeLabelsRequest request = RemoveFromClusterNodeLabelsRequest.newInstance(labels);
        adminProtocol.removeFromClusterNodeLabels(request);
    }
    return 0;
}
Also used : Options(org.apache.commons.cli.Options) ResourceManagerAdministrationProtocol(org.apache.hadoop.yarn.server.api.ResourceManagerAdministrationProtocol) CommandLine(org.apache.commons.cli.CommandLine) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) GnuParser(org.apache.commons.cli.GnuParser) RemoveFromClusterNodeLabelsRequest(org.apache.hadoop.yarn.server.api.protocolrecords.RemoveFromClusterNodeLabelsRequest)

Example 14 with MissingArgumentException

use of org.apache.commons.cli.MissingArgumentException in project bookkeeper by apache.

the class BookieShellTest method testRecoverCmdMissingArgument.

@Test
public void testRecoverCmdMissingArgument() throws Exception {
    RecoverCmd cmd = (RecoverCmd) shell.commands.get("recover");
    CommandLine cmdLine = parseCommandLine(cmd);
    try {
        cmd.runCmd(cmdLine);
        fail("should fail running command when the arguments are missing");
    } catch (MissingArgumentException e) {
    // expected
    }
    PowerMockito.verifyNew(BookKeeperAdmin.class, never()).withArguments(any(ClientConfiguration.class));
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) RecoverCmd(org.apache.bookkeeper.bookie.BookieShell.RecoverCmd) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with MissingArgumentException

use of org.apache.commons.cli.MissingArgumentException in project accumulo by apache.

the class GrepCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
    try (final PrintFile printFile = getOutputFile(cl)) {
        final String tableName = OptUtil.getTableOpt(cl, shellState);
        if (cl.getArgList().isEmpty()) {
            throw new MissingArgumentException("No terms specified");
        }
        final Class<? extends Formatter> formatter = getFormatter(cl, tableName, shellState);
        final ScanInterpreter interpeter = getInterpreter(cl, tableName, shellState);
        // handle first argument, if present, the authorizations list to
        // scan with
        int numThreads = 20;
        if (cl.hasOption(numThreadsOpt.getOpt())) {
            numThreads = Integer.parseInt(cl.getOptionValue(numThreadsOpt.getOpt()));
        }
        final Authorizations auths = getAuths(cl, shellState);
        final BatchScanner scanner = shellState.getConnector().createBatchScanner(tableName, auths, numThreads);
        scanner.setRanges(Collections.singletonList(getRange(cl, interpeter)));
        scanner.setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
        setupSampling(tableName, cl, shellState, scanner);
        for (int i = 0; i < cl.getArgs().length; i++) {
            setUpIterator(Integer.MAX_VALUE - cl.getArgs().length + i, "grep" + i, cl.getArgs()[i], scanner, cl);
        }
        try {
            // handle columns
            fetchColumns(cl, scanner, interpeter);
            // output the records
            final FormatterConfig config = new FormatterConfig();
            config.setPrintTimestamps(cl.hasOption(timestampOpt.getOpt()));
            printRecords(cl, shellState, config, scanner, formatter, printFile);
        } finally {
            scanner.close();
        }
    }
    return 0;
}
Also used : ScanInterpreter(org.apache.accumulo.core.util.interpret.ScanInterpreter) Authorizations(org.apache.accumulo.core.security.Authorizations) FormatterConfig(org.apache.accumulo.core.util.format.FormatterConfig) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) BatchScanner(org.apache.accumulo.core.client.BatchScanner) PrintFile(org.apache.accumulo.shell.Shell.PrintFile)

Aggregations

MissingArgumentException (org.apache.commons.cli.MissingArgumentException)16 CommandLine (org.apache.commons.cli.CommandLine)14 Options (org.apache.commons.cli.Options)11 GnuParser (org.apache.commons.cli.GnuParser)9 Option (org.apache.commons.cli.Option)5 ParseException (org.apache.commons.cli.ParseException)4 UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 CommandLineParser (org.apache.commons.cli.CommandLineParser)3 MissingOptionException (org.apache.commons.cli.MissingOptionException)3 IOException (java.io.IOException)2 AlreadySelectedException (org.apache.commons.cli.AlreadySelectedException)2 DefaultParser (org.apache.commons.cli.DefaultParser)2 ResourceManagerAdministrationProtocol (org.apache.hadoop.yarn.server.api.ResourceManagerAdministrationProtocol)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1