Search in sources :

Example 21 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project hbase by apache.

the class AbstractHBaseTool method run.

@Override
public int run(String[] args) throws IOException {
    cmdLineArgs = args;
    if (conf == null) {
        LOG.error("Tool configuration is not initialized");
        throw new NullPointerException("conf");
    }
    CommandLine cmd;
    List<String> argsList = new ArrayList<>(args.length);
    for (String arg : args) {
        argsList.add(arg);
    }
    // For backward compatibility of args which can't be parsed as Option. See javadoc for
    // processOldArgs(..)
    processOldArgs(argsList);
    try {
        addOptions();
        if (isHelpCommand(args)) {
            printUsage();
            return EXIT_SUCCESS;
        }
        String[] remainingArgs = new String[argsList.size()];
        argsList.toArray(remainingArgs);
        cmd = new DefaultParser().parse(options, remainingArgs);
    } catch (MissingOptionException e) {
        LOG.error(e.getMessage());
        LOG.error("Use -h or --help for usage instructions.");
        return EXIT_FAILURE;
    } catch (ParseException e) {
        LOG.error("Error when parsing command-line arguments", e);
        LOG.error("Use -h or --help for usage instructions.");
        return EXIT_FAILURE;
    }
    processOptions(cmd);
    int ret;
    try {
        ret = doWork();
    } catch (Exception e) {
        LOG.error("Error running command-line tool", e);
        return EXIT_FAILURE;
    }
    return ret;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ArrayList(java.util.ArrayList) ParseException(org.apache.commons.cli.ParseException) MissingOptionException(org.apache.commons.cli.MissingOptionException) IOException(java.io.IOException) MissingOptionException(org.apache.commons.cli.MissingOptionException) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 22 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project hbase by apache.

the class AbstractHBaseTool method isHelpCommand.

private boolean isHelpCommand(String[] args) throws ParseException {
    Options helpOption = new Options().addOption(HELP_OPTION);
    // this parses the command line but doesn't throw an exception on unknown options
    CommandLine cl = new DefaultParser().parse(helpOption, args, true);
    return cl.getOptions().length != 0;
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 23 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project flink by apache.

the class CliFrontendParser method parseCancelCommand.

public static CancelOptions parseCancelCommand(String[] args) throws CliArgsException {
    try {
        DefaultParser parser = new DefaultParser();
        CommandLine line = parser.parse(CANCEL_OPTIONS, args, false);
        return new CancelOptions(line);
    } catch (ParseException e) {
        throw new CliArgsException(e.getMessage());
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 24 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project flink by apache.

the class CliFrontendParser method parseInfoCommand.

public static InfoOptions parseInfoCommand(String[] args) throws CliArgsException {
    try {
        DefaultParser parser = new DefaultParser();
        CommandLine line = parser.parse(INFO_OPTIONS, args, true);
        return new InfoOptions(line);
    } catch (ParseException e) {
        throw new CliArgsException(e.getMessage());
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 25 with DefaultParser

use of org.apache.commons.cli.DefaultParser in project storm by apache.

the class KafkaOffsetLagUtil method main.

public static void main(String[] args) {
    try {
        List<KafkaOffsetLagResult> results;
        Options options = buildOptions();
        CommandLineParser parser = new DefaultParser();
        CommandLine commandLine = parser.parse(options, args);
        if (!commandLine.hasOption(OPTION_TOPIC_LONG)) {
            printUsageAndExit(options, OPTION_TOPIC_LONG + " is required");
        }
        if (commandLine.hasOption(OPTION_OLD_CONSUMER_LONG)) {
            OldKafkaSpoutOffsetQuery oldKafkaSpoutOffsetQuery;
            if (commandLine.hasOption(OPTION_GROUP_ID_LONG) || commandLine.hasOption(OPTION_BOOTSTRAP_BROKERS_LONG) || commandLine.hasOption(OPTION_SECURITY_PROTOCOL_LONG)) {
                printUsageAndExit(options, OPTION_GROUP_ID_LONG + " or " + OPTION_BOOTSTRAP_BROKERS_LONG + " or " + OPTION_SECURITY_PROTOCOL_LONG + " is " + "not accepted with option " + OPTION_OLD_CONSUMER_LONG);
            }
            if (!commandLine.hasOption(OPTION_ZK_SERVERS_LONG) || !commandLine.hasOption(OPTION_ZK_COMMITTED_NODE_LONG)) {
                printUsageAndExit(options, OPTION_ZK_SERVERS_LONG + " and " + OPTION_ZK_COMMITTED_NODE_LONG + " are required  with " + OPTION_OLD_CONSUMER_LONG);
            }
            String[] topics = commandLine.getOptionValue(OPTION_TOPIC_LONG).split(",");
            if (topics != null && topics.length > 1) {
                printUsageAndExit(options, "Multiple topics not supported with option " + OPTION_OLD_CONSUMER_LONG + ". Either a single topic or a " + "wildcard string for matching topics is supported");
            }
            if (commandLine.hasOption(OPTION_ZK_BROKERS_ROOT_LONG)) {
                if (commandLine.hasOption(OPTION_PARTITIONS_LONG) || commandLine.hasOption(OPTION_LEADERS_LONG)) {
                    printUsageAndExit(options, OPTION_PARTITIONS_LONG + " or " + OPTION_LEADERS_LONG + " is not accepted with " + OPTION_ZK_BROKERS_ROOT_LONG);
                }
                oldKafkaSpoutOffsetQuery = new OldKafkaSpoutOffsetQuery(commandLine.getOptionValue(OPTION_TOPIC_LONG), commandLine.getOptionValue(OPTION_ZK_SERVERS_LONG), commandLine.getOptionValue(OPTION_ZK_COMMITTED_NODE_LONG), commandLine.hasOption(OPTION_TOPIC_WILDCARD_LONG), commandLine.getOptionValue(OPTION_ZK_BROKERS_ROOT_LONG));
            } else {
                if (commandLine.hasOption(OPTION_TOPIC_WILDCARD_LONG)) {
                    printUsageAndExit(options, OPTION_TOPIC_WILDCARD_LONG + " is not supported without " + OPTION_ZK_BROKERS_ROOT_LONG);
                }
                if (!commandLine.hasOption(OPTION_PARTITIONS_LONG) || !commandLine.hasOption(OPTION_LEADERS_LONG)) {
                    printUsageAndExit(options, OPTION_PARTITIONS_LONG + " and " + OPTION_LEADERS_LONG + " are required if " + OPTION_ZK_BROKERS_ROOT_LONG + " is not provided");
                }
                String[] partitions = commandLine.getOptionValue(OPTION_PARTITIONS_LONG).split(",");
                String[] leaders = commandLine.getOptionValue(OPTION_LEADERS_LONG).split(",");
                if (partitions.length != leaders.length) {
                    printUsageAndExit(options, OPTION_PARTITIONS_LONG + " and " + OPTION_LEADERS_LONG + " need to be of same size");
                }
                oldKafkaSpoutOffsetQuery = new OldKafkaSpoutOffsetQuery(commandLine.getOptionValue(OPTION_TOPIC_LONG), commandLine.getOptionValue(OPTION_ZK_SERVERS_LONG), commandLine.getOptionValue(OPTION_ZK_COMMITTED_NODE_LONG), commandLine.getOptionValue(OPTION_PARTITIONS_LONG), commandLine.getOptionValue(OPTION_LEADERS_LONG));
            }
            results = getOffsetLags(oldKafkaSpoutOffsetQuery);
        } else {
            String securityProtocol = commandLine.getOptionValue(OPTION_SECURITY_PROTOCOL_LONG);
            String[] oldSpoutOptions = { OPTION_TOPIC_WILDCARD_LONG, OPTION_PARTITIONS_LONG, OPTION_LEADERS_LONG, OPTION_ZK_SERVERS_LONG, OPTION_ZK_COMMITTED_NODE_LONG, OPTION_ZK_BROKERS_ROOT_LONG };
            for (String oldOption : oldSpoutOptions) {
                if (commandLine.hasOption(oldOption)) {
                    printUsageAndExit(options, oldOption + " is not accepted without " + OPTION_OLD_CONSUMER_LONG);
                }
            }
            if (!commandLine.hasOption(OPTION_GROUP_ID_LONG) || !commandLine.hasOption(OPTION_BOOTSTRAP_BROKERS_LONG)) {
                printUsageAndExit(options, OPTION_GROUP_ID_LONG + " and " + OPTION_BOOTSTRAP_BROKERS_LONG + " are required if " + OPTION_OLD_CONSUMER_LONG + " is not specified");
            }
            NewKafkaSpoutOffsetQuery newKafkaSpoutOffsetQuery = new NewKafkaSpoutOffsetQuery(commandLine.getOptionValue(OPTION_TOPIC_LONG), commandLine.getOptionValue(OPTION_BOOTSTRAP_BROKERS_LONG), commandLine.getOptionValue(OPTION_GROUP_ID_LONG), securityProtocol);
            results = getOffsetLags(newKafkaSpoutOffsetQuery);
        }
        Map<String, Map<Integer, KafkaPartitionOffsetLag>> keyedResult = keyByTopicAndPartition(results);
        System.out.print(JSONValue.toJSONString(keyedResult));
    } catch (Exception ex) {
        System.out.print("Unable to get offset lags for kafka. Reason: ");
        ex.printStackTrace(System.out);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) HashMap(java.util.HashMap) Map(java.util.Map) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)41 DefaultParser (org.apache.commons.cli.DefaultParser)41 CommandLineParser (org.apache.commons.cli.CommandLineParser)29 ParseException (org.apache.commons.cli.ParseException)28 Options (org.apache.commons.cli.Options)27 HelpFormatter (org.apache.commons.cli.HelpFormatter)13 Option (org.apache.commons.cli.Option)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 Config (com.twitter.heron.spi.common.Config)3 Path (java.nio.file.Path)3 Level (java.util.logging.Level)3 Topology (com.ibm.streamsx.topology.Topology)2 PackingException (com.twitter.heron.spi.packing.PackingException)2 File (java.io.File)2 PrintStream (java.io.PrintStream)2 Properties (java.util.Properties)2 Test (org.junit.Test)2 PropertyKey (alluxio.PropertyKey)1