Example usage for org.apache.commons.cli Option getLongOpt

List of usage examples for org.apache.commons.cli Option getLongOpt

Introduction

In this page you can find the example usage for org.apache.commons.cli Option getLongOpt.

Prototype

public String getLongOpt() 

Source Link

Document

Retrieve the long name of this Option.

Usage

From source file:org.commonvox.hbase_column_manager.UtilityRunner.java

private void logParmInfo(CommandLine commandLine) {
    StringBuilder parmInfo = new StringBuilder(this.getClass().getSimpleName()
            + " has been invoked with the following <option=argument> combinations:");
    for (Option option : commandLine.getOptions()) {
        parmInfo.append(" <").append(option.getLongOpt())
                .append(option.getValue() == null ? "" : ("=" + option.getValue())).append(">");
    }/*from w  w w.  j  a va 2 s  .c o m*/
    LOG.info(parmInfo);
}

From source file:org.eclim.annotation.CommandListingProcessor.java

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    Options options = new Options();
    Pattern pattern = null;/*from w w w . j  ava 2s  . c  om*/
    String filter = this.processingEnv.getOptions().get("filter");
    if (filter != null) {
        pattern = Pattern.compile(filter);
    }
    for (TypeElement element : annotations) {
        for (Element e : env.getElementsAnnotatedWith(element)) {
            Command command = e.getAnnotation(Command.class);
            if (pattern == null || pattern.matcher(command.name()).matches()) {
                Collection<Option> opts = options.parseOptions(command.options());
                System.out.print(command.name());
                for (Option opt : opts) {
                    String display = "-" + opt.getOpt();
                    if (opt.hasArg()) {
                        display += " " + opt.getLongOpt();
                    }
                    if (opt.isRequired()) {
                        System.out.print(" " + display);
                    } else {
                        System.out.print(" [" + display + "]");
                    }
                }
                System.out.println("\n\tclass: " + e);
            }
        }
    }
    return true;
}

From source file:org.eclim.command.Main.java

public static void usage(String cmd, PrintStream out) {
    ArrayList<org.eclim.annotation.Command> commands = new ArrayList<org.eclim.annotation.Command>();
    for (Class<? extends Command> command : Services.getCommandClasses()) {
        commands.add(command.getAnnotation(org.eclim.annotation.Command.class));
    }/*from   w ww  .  java2 s  .  c  om*/
    Collections.sort(commands, new Comparator<org.eclim.annotation.Command>() {
        public int compare(org.eclim.annotation.Command o1, org.eclim.annotation.Command o2) {
            return o1.name().compareTo(o2.name());
        }
    });

    boolean cmdFound = cmd == null;
    if (cmd == null) {
        String osOpts = StringUtils.EMPTY;
        if (SystemUtils.IS_OS_UNIX) {
            osOpts = " [-f eclimrc] [--nailgun-port port]";
        }
        out.println("Usage: eclim" + osOpts + " -command command [args]");
        out.println("  To view a full list of available commands:");
        out.println("    eclim -? commands");
        out.println("  To view info for a specific command:");
        out.println("    eclim -? <command_name>");
        out.println("  Ex.");
        out.println("    eclim -? project_create");
    } else if (cmd.equals("commands")) {
        out.println("Available Commands:");
    } else {
        out.println("Requested Command:");
    }

    for (org.eclim.annotation.Command command : commands) {
        if (cmd == null || (!cmd.equals(command.name()) && !cmd.equals("commands"))) {
            continue;
        }
        cmdFound = true;

        Collection<Option> options = new Options().parseOptions(command.options());
        StringBuffer opts = new StringBuffer();
        Iterator<Option> iterator = options.iterator();
        for (int ii = 0; iterator.hasNext(); ii++) {
            Option option = iterator.next();
            opts.append(option.isRequired() ? " " : " [");
            opts.append('-').append(option.getOpt());
            if (option.hasArg()) {
                opts.append(' ').append(option.getLongOpt());
            }
            if (!option.isRequired()) {
                opts.append(']');
            }
            // wrap every 4 options
            if ((ii + 1) % 4 == 0 && ii != options.size() - 1) {
                opts.append(StringUtils.rightPad("\n", command.name().length() + 5));
            }
        }
        StringBuffer info = new StringBuffer().append("    ").append(command.name()).append(opts);
        out.println(info);
        if (!command.description().equals(StringUtils.EMPTY)) {
            out.println("      " + command.description());
        }
    }

    if (!cmdFound) {
        out.println("    No Such Command: " + cmd);
    }
}

From source file:org.finra.datagenerator.samples.CmdLine.java

/**
 * Prints the help on the command line//from  ww w  .j av  a  2  s .co m
 *
 * @param options Options object from commons-cli
 */
public static void printHelp(final Options options) {
    Collection<Option> c = options.getOptions();
    System.out.println("Command line options are:");
    int longestLongOption = 0;
    for (Option op : c) {
        if (op.getLongOpt().length() > longestLongOption) {
            longestLongOption = op.getLongOpt().length();
        }
    }

    longestLongOption += 2;
    String spaces = StringUtils.repeat(" ", longestLongOption);

    for (Option op : c) {
        System.out.print("\t-" + op.getOpt() + " --" + op.getLongOpt());
        if (op.getLongOpt().length() < spaces.length()) {
            System.out.print(spaces.substring(op.getLongOpt().length()));
        } else {
            System.out.print(" ");
        }
        System.out.println(op.getDescription());
    }
}

From source file:org.finra.dm.core.ArgumentParser.java

/**
 * Retrieves the argument as an Integer object, if any, of an option and validates it against minimum and maximum allowed values.
 *
 * @param option the option that we want argument value to be returned for
 * @param defaultValue is the default value to be returned if the option is not specified
 * @param minValue the minimum allowed Integer value for the option
 * @param maxValue the maximum allowed Integer value for the option
 *
 * @return Value of the argument if option is set, and has an argument, otherwise defaultValue.
 * @throws IllegalArgumentException if there are problems with the option value
 *//*w  w  w.j  ava2s .c o  m*/
public Integer getIntegerValue(Option option, Integer defaultValue, Integer minValue, Integer maxValue)
        throws IllegalArgumentException {
    Integer answer = getIntegerValue(option, defaultValue);

    if (answer != null) {
        Assert.isTrue(answer.compareTo(minValue) >= 0,
                String.format("The %s option value %d is less than the minimum allowed value of %d.",
                        option.getLongOpt(), answer, minValue));
        Assert.isTrue(answer.compareTo(maxValue) <= 0,
                String.format("The %s option value %d is bigger than maximum allowed value of %d.",
                        option.getLongOpt(), answer, maxValue));
    }

    return answer;
}

From source file:org.finra.dm.core.ArgumentParserTest.java

@Test
public void testAddArgument() {
    Option opt1 = new Option("a", "some_flag", false, "Some flag parameter");
    Option opt2 = new Option("b", "some_parameter", true, "Some parameter with an argument");

    List<String> optionsIn = Arrays.asList(optionToString(opt1), optionToString(opt2));

    ArgumentParser argParser = new ArgumentParser("");

    argParser.addArgument(opt1, true);//  ww w. j  a v  a2  s. c o m
    argParser.addArgument(opt2.getOpt(), opt2.getLongOpt(), opt2.hasArg(), opt2.getDescription(), false);

    Collection resultOptions = argParser.getConfiguredOptions();

    List<String> optionsOut = new ArrayList<>();

    for (Object obj : resultOptions) {
        assertTrue(obj instanceof Option);
        optionsOut.add(optionToString((Option) obj));
    }

    optionsOut.containsAll(optionsIn);
    optionsIn.containsAll(optionsOut);
}

From source file:org.finra.dm.core.ArgumentParserTest.java

@Test
public void testGetUsageInformation() {
    List<Option> optionsIn = new ArrayList<>();
    optionsIn.add(new Option("a", "some_flag", false, "Some flag parameter"));
    optionsIn.add(new Option("b", "some_parameter", true, "Some parameter with an argument"));

    ArgumentParser argParser = new ArgumentParser("TestApp");
    argParser.addArgument(optionsIn.get(0), false);
    argParser.addArgument(optionsIn.get(1), true);

    String usage = argParser.getUsageInformation();

    assertNotNull(usage);//  w  w w .  ja  v a 2s . c o  m
    assertTrue(usage.contains(String.format("usage: %s", argParser.getApplicationName())));

    for (Option option : optionsIn) {
        assertTrue(usage.contains(String.format("-%s,", option.getOpt())));
        assertTrue(usage.contains(String.format("--%s", option.getLongOpt())));
        assertTrue(usage.contains(option.getDescription()));
        assertTrue(!option.hasArg() || usage.contains(String.format("<%s>", option.getArgName())));
    }
}

From source file:org.finra.dm.core.ArgumentParserTest.java

@Test
public void testGetBooleanValue() throws ParseException {
    ArgumentParser argParser = new ArgumentParser("");
    Option boolOpt = argParser.addArgument("b", "bool", false, "Some optional configuration flag", false);
    Boolean resultValue;//from  w  w w .  ja  va2s .  c  om

    final String shortBoolOpt = String.format("-%s", boolOpt.getOpt());
    final String longBoolOpt = String.format("--%s", boolOpt.getLongOpt());

    argParser.parseArguments(new String[] {});
    resultValue = argParser.getBooleanValue(boolOpt);
    assertNotNull(resultValue);
    assertFalse(resultValue);

    argParser.parseArguments(new String[] { shortBoolOpt });
    resultValue = argParser.getBooleanValue(boolOpt);
    assertNotNull(resultValue);
    assertTrue(resultValue);

    argParser.parseArguments(new String[] { longBoolOpt });
    resultValue = argParser.getBooleanValue(boolOpt);
    assertNotNull(resultValue);
    assertTrue(resultValue);
}

From source file:org.finra.dm.core.ArgumentParserTest.java

@Test
public void testGetStringValueAsBoolean() throws ParseException {
    ArgumentParser argParser = new ArgumentParser("");
    Option strOpt = argParser.addArgument("s", "str", true,
            "Some string input parameter to have a boolean value", false);

    final String shortStrOpt = String.format("-%s", strOpt.getOpt());
    final String longStrOpt = String.format("--%s", strOpt.getLongOpt());

    // Validate the default value - no option is specified.
    argParser.parseArguments(new String[] {});
    assertFalse(argParser.getStringValueAsBoolean(strOpt, false));
    assertTrue(argParser.getStringValueAsBoolean(strOpt, true));

    // Validate all "true" boolean values using both short an long options.
    for (String inputValue : Arrays.asList(CustomBooleanEditor.VALUE_TRUE, CustomBooleanEditor.VALUE_YES,
            CustomBooleanEditor.VALUE_ON, CustomBooleanEditor.VALUE_1)) {
        argParser.parseArguments(new String[] { shortStrOpt, inputValue });
        assertTrue(argParser.getStringValueAsBoolean(strOpt, false));
        argParser.parseArguments(new String[] { longStrOpt, inputValue });
        assertTrue(argParser.getStringValueAsBoolean(strOpt, false));
    }/*from  w w  w  . j a  v  a2s .c om*/

    // Validate all "false" boolean values.
    for (String inputValue : Arrays.asList(CustomBooleanEditor.VALUE_FALSE, CustomBooleanEditor.VALUE_NO,
            CustomBooleanEditor.VALUE_OFF, CustomBooleanEditor.VALUE_0)) {
        argParser.parseArguments(new String[] { shortStrOpt, inputValue });
        assertFalse(argParser.getStringValueAsBoolean(strOpt, true));
    }

    // Try to parse an invalid boolean value.
    argParser.parseArguments(new String[] { shortStrOpt, INVALID_BOOLEAN_VALUE });
    try {
        argParser.getStringValueAsBoolean(strOpt, false);
        fail("Suppose to throw a ParseException when option has an invalid boolean value.");
    } catch (ParseException e) {
        assertEquals(String.format("Invalid boolean value [%s]", INVALID_BOOLEAN_VALUE), e.getMessage());
    }
}

From source file:org.finra.dm.core.ArgumentParserTest.java

@Test
public void testGetStringValue() throws ParseException {
    final String testDefaultValue = "default_str_value";

    ArgumentParser argParser = new ArgumentParser("");
    Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter", false);
    String inputValue;/*from  w w  w  .  j  a va2  s  .c o  m*/
    String resultValue;

    final String shortStrOpt = String.format("-%s", strOpt.getOpt());
    final String longStrOpt = String.format("--%s", strOpt.getLongOpt());

    argParser.parseArguments(new String[] {});
    assertNull(argParser.getStringValue(strOpt));
    assertEquals(testDefaultValue, argParser.getStringValue(strOpt, testDefaultValue));

    inputValue = "my_string_value_1";
    argParser.parseArguments(new String[] { shortStrOpt, inputValue });
    resultValue = argParser.getStringValue(strOpt);
    assertNotNull(resultValue);
    assertEquals(inputValue, resultValue);

    inputValue = "my_string_value_2";
    argParser.parseArguments(new String[] { shortStrOpt, inputValue });
    resultValue = argParser.getStringValue(strOpt, testDefaultValue);
    assertNotNull(resultValue);
    assertEquals(inputValue, resultValue);

    inputValue = "my_string_value_3";
    argParser.parseArguments(new String[] { longStrOpt, inputValue });
    resultValue = argParser.getStringValue(strOpt);
    assertNotNull(resultValue);
    assertEquals(inputValue, resultValue);
}