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

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

Introduction

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

Prototype

public boolean hasArg() 

Source Link

Document

Query to see if this Option requires an argument

Usage

From source file:de.hsos.ecs.richwps.wpsmonitor.boundary.cli.command.CommandFormatter.java

private String decorateArg(final Option opt) {
    final StringBuilder strBuilder = new StringBuilder();

    if (opt.hasArg()) {
        if (opt.hasOptionalArg()) {
            strBuilder.append("[");
        }/*  w w  w  .  j a  va2 s . c  o  m*/

        strBuilder.append("=").append("<").append(opt.getArgName()).append(">");

        if (opt.hasOptionalArg()) {
            strBuilder.append("]");
        }
    }

    return strBuilder.toString();
}

From source file:dioscuri.TestCommandLineInterface.java

/**
 * Test all valid parameters:// w  w  w  .  java2s. com
 * <pre>
 *   -?,--help                         print this message
 *   -a,--architecture <'16'|'32'>     the cpu's architecture
 *   -b,--boot <'floppy'|'harddisk'>   the boot drive
 *   -c,--config <file>                a custom config xml file
 *   -d,--harddisk <file>              a custom hard disk image
 *   -e,--exit                         used for testing purposes, will cause
 *                                     Dioscuri to exit immediately
 *   -f,--floppy <file>                a custom floppy image
 *   -h,--hide                         hide the GUI
 *   -r,--autorun                      emulator will directly start emulatio
 *                                     process
 *   -s,--autoshutdown                 emulator will shutdown automatically
 *                                     when emulation process is finished
 * </pre>
 *
 * @throws Exception -
 */
@Test
public void testAllValid() throws Exception {
    Options options = parseCommandLineInterface().commandLineOptions;

    // no parameters is valid, of course
    testValid("");

    // test all single options
    for (Object o : options.getOptions()) {
        Option op = (Option) o;
        if (!op.hasArg()) {
            testValid("-" + op.getOpt());
            testValid("--" + op.getLongOpt());
        }
    }
    // test some multiple params
    testValid("-he");
    testValid("-h", "-e", "-s");

    // test the options that need a valid input as 2nd parameter
    File temp = createTempFile();
    if (temp != null) {
        // couldn't create a temp file, skip : -cfd
        testValid("-c", temp.getAbsolutePath());
        testValid("-f", temp.getAbsolutePath());
        testValid("-d", temp.getAbsolutePath());
    }

    testValid("-b", "floppy");
    testValid("-b", "HARDdisk"); // case insensitive

    testValid("-a", "16");
    testValid("-a", "32");
}

From source file:edu.vt.middleware.ldap.cli.AbstractCli.java

/**
 * Reads the options from the supplied command line and returns a properties
 * containing those options.// w  w  w. j  a  v a  2s .c o m
 *
 * @param  domain  to place property names in
 * @param  line  command line
 *
 * @return  properties for each option and value
 */
protected Properties getPropertiesFromOptions(final String domain, final CommandLine line) {
    final Properties props = new Properties();
    for (Option o : line.getOptions()) {
        if (o.hasArg()) {
            // if provider property, split the value
            if (o.getOpt().equals(OPT_PROVIDER_PROPERTIES)) {
                final String[] s = o.getValue().split("=");
                props.setProperty(s[0], s[1]);
                // add the domain to vt-ldap properties
            } else {
                props.setProperty(domain + o.getOpt(), o.getValue());
            }
        }
    }
    return props;
}

From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.spi.TestCliOptionsBuilder.java

@Test
public void testHasArg() {
    Option option = this.builder.hasArg().create("test");
    assertNotNull("The option should not be null.", option);
    assertTrue("hasArg should be true.", option.hasArg());
}

From source file:de.hsos.ecs.richwps.wpsmonitor.boundary.cli.command.CommandFormatter.java

/**
 * Representates the command object as text with all options. The returned
 * string is formatted for console output.
 *
 * @return Command representation./*  ww w . j  ava2  s  . c om*/
 */
@Override
public String toString() {
    final String desc_sep = "\t\t";
    final String desc_sep_break = "\n" + desc_sep;
    final String desc_opt_sep_break = desc_sep_break + "\t\t\t";

    final Integer maxStrL = 100;

    final String cmdDescription = trimByWhitespace(cmd.getDescription(), desc_sep_break, maxStrL);

    StringBuilder strBuilder = new StringBuilder(cmd.getCommandName()).append(desc_sep).append(cmdDescription);

    if (cmd.getOptions().getOptions().size() > 0) {
        strBuilder.append("\n\n").append(desc_sep).append("Options:").append(desc_sep);
    }

    for (final Object o : cmd.getOptions().getOptions()) {
        if (o instanceof Option) {
            final Option opt = (Option) o;
            final String optName = opt.getOpt();

            strBuilder.append("\n").append(desc_sep).append(optList(opt));

            if (opt.hasArg()) {
                strBuilder.append(decorateArg(opt));
            }

            final String optDescription = trimByWhitespace(opt.getDescription(), desc_opt_sep_break,
                    maxStrL - optName.length() - 5);

            strBuilder.append(" : ").append(optDescription);
        }
    }

    return strBuilder.append("\n").toString();
}

From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.spi.TestCliOptionsBuilder.java

@Test
public void testHasArgBooleanArg() {
    Option option = this.builder.hasArg(false).create("test");
    assertNotNull("The option should not be null.", option);
    assertFalse("hasArg should be false.", option.hasArg());
}

From source file:name.livitski.databag.cli.Syntax.java

protected String formatArguments(Option option) {
    if (!option.hasArg())
        return "";
    else {/*from  www . ja  va 2  s .c om*/
        String arg = "#unknown#";
        try {
            String id = getArgumentId(option);
            arg = getResources().getString(USAGE_BUNDLE, getClass(), id);
        } catch (MissingResourceException missing) {
        }
        return ' ' + arg;
    }
}

From source file:com.github.horrorho.inflatabledonkey.args.ArgsManager.java

String parse(Option option, UnaryOperator<String> parser) {
    if (option.hasArgs()) {
        return option.getValuesList().stream().map(u -> parse(option.getLongOpt(), u, parser))
                .collect(Collectors.joining(" "));
    }//from   w w  w .ja v  a  2  s  .  c o m
    if (option.hasArg()) {
        return parse(option.getLongOpt(), option.getValue(), parser);
    }
    return Boolean.TRUE.toString();
}

From source file:com.github.horrorho.inflatabledonkey.args.PropertyLoader.java

String parse(Option option) {
    testIntegers(option);/* w  ww  . j a va  2  s  . c  om*/

    if (option.hasArgs()) {
        // Array
        return option.getValuesList().stream().collect(Collectors.joining(" "));
    }
    if (option.hasArg()) {
        // Value
        return option.getValue();
    }
    // Boolean
    return Boolean.TRUE.toString();
}

From source file:gobblin.runtime.cli.PublicMethodsGobblinCliFactory.java

/**
 * For each method for which the helper created an {@link Option} and for which the input {@link CommandLine} contains
 * that option, this method will automatically call the method on the input {@link EmbeddedGobblin} with the correct
 * arguments.//from  w ww.j a v  a  2  s .c o m
 */
public void applyCommandLineOptions(CommandLine cli, EmbeddedGobblin embeddedGobblin) {
    try {
        for (Option option : cli.getOptions()) {
            if (!this.methodsMap.containsKey(option.getOpt())) {
                // Option added by cli driver itself.
                continue;
            }
            if (option.hasArg()) {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin, option.getValue());
            } else {
                this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin);
            }
        }
    } catch (IllegalAccessException | InvocationTargetException exc) {
        throw new RuntimeException("Could not apply options to " + embeddedGobblin.getClass().getName(), exc);
    }
}