Example usage for org.apache.commons.cli OptionBuilder withLongOpt

List of usage examples for org.apache.commons.cli OptionBuilder withLongOpt

Introduction

In this page you can find the example usage for org.apache.commons.cli OptionBuilder withLongOpt.

Prototype

public static OptionBuilder withLongOpt(String newLongopt) 

Source Link

Document

The next Option created will have the following long option value.

Usage

From source file:org.apache.druid.examples.rabbitmq.RabbitMQProducerMain.java

public static void main(String[] args) throws Exception {
    // We use a List to keep track of option insertion order. See below.
    final List<Option> optionList = new ArrayList<Option>();

    optionList.add(OptionBuilder.withLongOpt("help").withDescription("display this help message").create("h"));
    optionList.add(OptionBuilder.withLongOpt("hostname").hasArg()
            .withDescription("the hostname of the AMQP broker [defaults to AMQP library default]").create("b"));
    optionList.add(OptionBuilder.withLongOpt("port").hasArg()
            .withDescription("the port of the AMQP broker [defaults to AMQP library default]").create("n"));
    optionList.add(OptionBuilder.withLongOpt("username").hasArg()
            .withDescription("username to connect to the AMQP broker [defaults to AMQP library default]")
            .create("u"));
    optionList.add(OptionBuilder.withLongOpt("password").hasArg()
            .withDescription("password to connect to the AMQP broker [defaults to AMQP library default]")
            .create("p"));
    optionList.add(OptionBuilder.withLongOpt("vhost").hasArg()
            .withDescription("name of virtual host on the AMQP broker [defaults to AMQP library default]")
            .create("v"));
    optionList.add(OptionBuilder.withLongOpt("exchange").isRequired().hasArg()
            .withDescription("name of the AMQP exchange [required - no default]").create("e"));
    optionList.add(OptionBuilder.withLongOpt("key").hasArg()
            .withDescription("the routing key to use when sending messages [default: 'default.routing.key']")
            .create("k"));
    optionList.add(OptionBuilder.withLongOpt("type").hasArg()
            .withDescription("the type of exchange to create [default: 'topic']").create("t"));
    optionList.add(OptionBuilder.withLongOpt("durable")
            .withDescription("if set, a durable exchange will be declared [default: not set]").create("d"));
    optionList.add(OptionBuilder.withLongOpt("autodelete")
            .withDescription("if set, an auto-delete exchange will be declared [default: not set]")
            .create("a"));
    optionList.add(OptionBuilder.withLongOpt("single")
            .withDescription("if set, only a single message will be sent [default: not set]").create("s"));
    optionList.add(OptionBuilder.withLongOpt("start").hasArg()
            .withDescription("time to use to start sending messages from [default: 2010-01-01T00:00:00]")
            .create());/*from  w  ww.ja  va  2 s  . c om*/
    optionList.add(OptionBuilder.withLongOpt("stop").hasArg().withDescription(
            "time to use to send messages until (format: '2013-07-18T23:45:59') [default: current time]")
            .create());
    optionList.add(OptionBuilder.withLongOpt("interval").hasArg()
            .withDescription("the interval to add to the timestamp between messages in seconds [default: 10]")
            .create());
    optionList.add(OptionBuilder.withLongOpt("delay").hasArg()
            .withDescription("the delay between sending messages in milliseconds [default: 100]").create());

    // An extremely silly hack to maintain the above order in the help formatting.
    HelpFormatter formatter = new HelpFormatter();
    // Add a comparator to the HelpFormatter using the ArrayList above to sort by insertion order.
    //noinspection ComparatorCombinators -- don't replace with comparingInt() to preserve comments
    formatter.setOptionComparator((o1, o2) -> {
        // I know this isn't fast, but who cares! The list is short.
        //noinspection SuspiciousMethodCalls
        return Integer.compare(optionList.indexOf(o1), optionList.indexOf(o2));
    });

    // Now we can add all the options to an Options instance. This is dumb!
    Options options = new Options();
    for (Option option : optionList) {
        options.addOption(option);
    }

    CommandLine cmd = null;

    try {
        cmd = new BasicParser().parse(options, args);
    } catch (ParseException e) {
        formatter.printHelp("RabbitMQProducerMain", e.getMessage(), options, null);
        System.exit(1);
    }

    if (cmd.hasOption("h")) {
        formatter.printHelp("RabbitMQProducerMain", options);
        System.exit(2);
    }

    ConnectionFactory factory = new ConnectionFactory();

    if (cmd.hasOption("b")) {
        factory.setHost(cmd.getOptionValue("b"));
    }
    if (cmd.hasOption("u")) {
        factory.setUsername(cmd.getOptionValue("u"));
    }
    if (cmd.hasOption("p")) {
        factory.setPassword(cmd.getOptionValue("p"));
    }
    if (cmd.hasOption("v")) {
        factory.setVirtualHost(cmd.getOptionValue("v"));
    }
    if (cmd.hasOption("n")) {
        factory.setPort(Integer.parseInt(cmd.getOptionValue("n")));
    }

    String exchange = cmd.getOptionValue("e");
    String routingKey = "default.routing.key";
    if (cmd.hasOption("k")) {
        routingKey = cmd.getOptionValue("k");
    }

    boolean durable = cmd.hasOption("d");
    boolean autoDelete = cmd.hasOption("a");
    String type = cmd.getOptionValue("t", "topic");
    boolean single = cmd.hasOption("single");
    int interval = Integer.parseInt(cmd.getOptionValue("interval", "10"));
    int delay = Integer.parseInt(cmd.getOptionValue("delay", "100"));

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
    Date stop = sdf.parse(cmd.getOptionValue("stop", sdf.format(new Date())));

    Random r = ThreadLocalRandom.current();
    Calendar timer = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH);
    timer.setTime(sdf.parse(cmd.getOptionValue("start", "2010-01-01T00:00:00")));

    String msg_template = "{\"utcdt\": \"%s\", \"wp\": %d, \"gender\": \"%s\", \"age\": %d}";

    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(exchange, type, durable, autoDelete, null);

    do {
        int wp = (10 + r.nextInt(90)) * 100;
        String gender = r.nextBoolean() ? "male" : "female";
        int age = 20 + r.nextInt(70);

        String line = StringUtils.format(msg_template, sdf.format(timer.getTime()), wp, gender, age);

        channel.basicPublish(exchange, routingKey, null, StringUtils.toUtf8(line));

        System.out.println("Sent message: " + line);

        timer.add(Calendar.SECOND, interval);

        Thread.sleep(delay);
    } while ((!single && stop.after(timer.getTime())));

    connection.close();
}

From source file:org.apache.felix.ipojo.task.IPojoc.java

/**
 * Builds the option list// w w  w. j  a v  a2 s  .c  o  m
 * @return the options
 */
public static Options buildOptions() {
    Option input = OptionBuilder.withArgName("input file").withLongOpt("input").hasArg()
            .withDescription("the input jar file").isRequired(true).withType(File.class).create('i');

    Option output = OptionBuilder.withLongOpt("output").withArgName("output file").hasArg()
            .withDescription("the output jar file, if not set the manipulation replaces the input file")
            .isRequired(false).withType(File.class).create('o');

    Option metadata = OptionBuilder.withLongOpt("metadata").withArgName("metadata file").hasArg()
            .withDescription("the XML metadata file").isRequired(false).withType(File.class).create('m');

    Option verbose = new Option("X", "exception", false, "print exception stack trace in case of error");

    Option help = OptionBuilder.withLongOpt("help").withDescription("print this message").create('h');

    return new Options().addOption(input).addOption(output).addOption(metadata).addOption(verbose)
            .addOption(help);
}

From source file:org.apache.geronimo.cli.BaseCLParser.java

protected void addOptionWithParam(String longOption, String shortOption, String argName, String desc) {
    OptionBuilder optionBuilder = OptionBuilder.hasArg().withArgName(argName);
    optionBuilder = optionBuilder.withLongOpt(longOption);
    optionBuilder = optionBuilder.withDescription(desc);
    Option option = optionBuilder.create(shortOption);
    options.addOption(option);//from   w  w  w .j a v  a2  s  . com
}

From source file:org.apache.geronimo.cli.daemon.DaemonCLParser.java

protected void addOverride() {
    OptionBuilder optionBuilder = OptionBuilder.hasArgs().withArgName("moduleId ...");
    optionBuilder = optionBuilder.withLongOpt(ARGUMENT_MODULE_OVERRIDE);
    optionBuilder = optionBuilder.withDescription("USE WITH CAUTION!  Overrides the modules in "
            + "var/config/config.xml such that only the modules listed on "
            + "the command line will be started.  Note that many J2EE "
            + "features depend on certain modules being started, so you "
            + "should be very careful what you omit.  Any arguments after "
            + "this are assumed to be module names.");
    Option option = optionBuilder.create(ARGUMENT_MODULE_OVERRIDE_SHORTFORM);
    options.addOption(option);/*w  w  w  . jav  a2s. co m*/
}

From source file:org.apache.geronimo.cli.deployer.DistributeCommandArgsImpl.java

protected void addTargets() {
    OptionBuilder optionBuilder = OptionBuilder.hasArg().withArgName("targets");
    optionBuilder = optionBuilder.withLongOpt(ARGUMENT_TARGETS);
    optionBuilder = optionBuilder//from w w  w  .j a v  a2 s . com
            .withDescription("If no targets are provided, the module is distributed to all available "
                    + "targets. Geronimo only provides one target (ever), so this is primarily "
                    + "useful when using a different driver.\n");
    Option option = optionBuilder.create(ARGUMENT_TARGETS_SHORTFORM);
    options.addOption(option);
}

From source file:org.apache.geronimo.cli.deployer.InstallBundleCommandArgsImpl.java

protected void addStartLevel() {
    OptionBuilder optionBuilder = OptionBuilder.hasArg().withArgName("startLevel");
    optionBuilder = optionBuilder.withLongOpt(ARGUMENT_START_LEVEL);
    optionBuilder = optionBuilder.withDescription(
            "If no start level are provided, will use the framework's initial bundle start level");
    Option option = optionBuilder.create(ARGUMENT_START_LEVEL_SHORTFORM);
    options.addOption(option);/* ww w. ja v a2 s  .c o m*/
}

From source file:org.apache.geronimo.cli.deployer.InstallBundleCommandArgsImpl.java

protected void addGroupId() {
    OptionBuilder optionBuilder = OptionBuilder.hasArg().withArgName("groupId");
    optionBuilder = optionBuilder.withLongOpt(ARGUMENT_GROUP_ID);
    optionBuilder = optionBuilder/*ww  w .j  a  v a2  s  . c om*/
            .withDescription("If gourpId is not provided, will use \"default\" as its group id.");
    Option option = optionBuilder.create(ARGUMENT_GROUP_ID_SHORTFORM);
    options.addOption(option);
}

From source file:org.apache.geronimo.cli.deployer.InstallLibraryCommandArgsImpl.java

protected void addGroupId() {
    OptionBuilder optionBuilder = OptionBuilder.hasArg().withArgName(ARGUMENT_GROUP_ID);
    optionBuilder = optionBuilder.withLongOpt(ARGUMENT_GROUP_ID);
    optionBuilder = optionBuilder//from  w w w . j  a va2s .c  o m
            .withDescription("If a groupId is provided, the library file will be installed under that groupId. "
                    + "Otherwise, default will be used.");
    Option option = optionBuilder.create(ARGUMENT_GROUP_ID_SHORTFORM);
    options.addOption(option);
}

From source file:org.apache.hadoop.hdfs.tools.DiskBalancer.java

/**
 * Adds commands for plan command./* w w w  .jav a  2  s.  c o  m*/
 *
 * @return Options.
 */
private void addPlanCommands(Options opt) {

    Option plan = OptionBuilder.withLongOpt(PLAN)
            .withDescription("Hostname, IP address or UUID of datanode " + "for which a plan is created.")
            .hasArg().create();
    getPlanOptions().addOption(plan);
    opt.addOption(plan);

    Option outFile = OptionBuilder.withLongOpt(OUTFILE).hasArg()
            .withDescription(
                    "Local path of file to write output to, if not specified " + "defaults will be used.")
            .create();
    getPlanOptions().addOption(outFile);
    opt.addOption(outFile);

    Option bandwidth = OptionBuilder.withLongOpt(BANDWIDTH).hasArg().withDescription(
            "Maximum disk bandwidth (MB/s) in integer to be consumed by " + "diskBalancer. e.g. 10 MB/s.")
            .create();
    getPlanOptions().addOption(bandwidth);
    opt.addOption(bandwidth);

    Option threshold = OptionBuilder.withLongOpt(THRESHOLD).hasArg()
            .withDescription("Percentage of data skew that is tolerated before"
                    + " disk balancer starts working. For example, if"
                    + " total data on a 2 disk node is 100 GB then disk"
                    + " balancer calculates the expected value on each disk,"
                    + " which is 50 GB. If the tolerance is 10% then data"
                    + " on a single disk needs to be more than 60 GB"
                    + " (50 GB + 10% tolerance value) for Disk balancer to" + " balance the disks.")
            .create();
    getPlanOptions().addOption(threshold);
    opt.addOption(threshold);

    Option maxError = OptionBuilder.withLongOpt(MAXERROR).hasArg()
            .withDescription(
                    "Describes how many errors " + "can be tolerated while copying between a pair of disks.")
            .create();
    getPlanOptions().addOption(maxError);
    opt.addOption(maxError);

    Option verbose = OptionBuilder.withLongOpt(VERBOSE)
            .withDescription("Print out the summary of the plan on console").create();
    getPlanOptions().addOption(verbose);
    opt.addOption(verbose);
}

From source file:org.apache.hadoop.hdfs.tools.DiskBalancer.java

/**
 * Adds Help to the options.// w ww  .  jav a 2s  .  c o m
 */
private void addHelpCommands(Options opt) {
    Option help = OptionBuilder.withLongOpt(HELP).hasOptionalArg()
            .withDescription("valid commands are plan | execute | query | cancel" + " | report").create();
    getHelpOptions().addOption(help);
    opt.addOption(help);
}