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

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

Introduction

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

Prototype

public String getOpt() 

Source Link

Document

Retrieve the name of this Option.

Usage

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

private Constructor<? extends T> inferConstructorOptions(Options otherOptions) {
    Constructor<? extends T> selectedConstructor = null;
    for (Constructor<?> constructor : this.klazz.getConstructors()) {
        if (canUseConstructor(constructor)) {
            if (selectedConstructor == null) {
                selectedConstructor = (Constructor<? extends T>) constructor;
            } else {
                throw new RuntimeException("Multiple usable constructors for " + this.klazz.getName());
            }/* www  .  j  av a2s .c om*/
        }
    }
    if (selectedConstructor == null) {
        throw new RuntimeException("There is no usable constructor for " + this.klazz.getName());
    }

    int constructorIdx = 0;
    for (String argument : selectedConstructor.getAnnotation(CliObjectSupport.class).argumentNames()) {
        Option option = Option.builder(argument).required().hasArg().build();
        otherOptions.addOption(option);
        constructoArgumentsMap.put(option.getOpt(), constructorIdx);
        constructorIdx++;
    }

    return selectedConstructor;
}

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

private String optList(final Option opt) {
    final String lOptName = opt.getLongOpt();
    final StringBuilder strBuilder = new StringBuilder();

    strBuilder.append(decorateOptionName(opt.getOpt()));

    if (lOptName != null) {
        strBuilder.append(", ").append(decorateOptionName(opt.getLongOpt()));
    }//from w w  w  .ja v a2 s  .c  o  m

    return strBuilder.toString();
}

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

private Constructor<? extends EmbeddedGobblin> inferConstructorOptions(Options otherOptions) {
    Constructor<? extends EmbeddedGobblin> selectedConstructor = null;
    for (Constructor<?> constructor : this.klazz.getConstructors()) {
        if (canUseConstructor(constructor)) {
            if (selectedConstructor == null) {
                selectedConstructor = (Constructor<? extends EmbeddedGobblin>) constructor;
            } else {
                throw new RuntimeException("Multiple usable constructors for " + this.klazz.getName());
            }/*from   w ww .  j a  va  2  s. co  m*/
        }
    }
    if (selectedConstructor == null) {
        throw new RuntimeException("There is no usable constructor for " + this.klazz.getName());
    }

    int constructorIdx = 0;
    for (String argument : selectedConstructor.getAnnotation(EmbeddedGobblinCliSupport.class).argumentNames()) {
        Option option = Option.builder(argument).required().hasArg().build();
        otherOptions.addOption(option);
        constructoArgumentsMap.put(option.getOpt(), constructorIdx);
        constructorIdx++;
    }

    return selectedConstructor;
}

From source file:com.github.triceo.robozonky.app.CommandLineInterface.java

private boolean hasOption(final Option option) {
    return this.cli.hasOption(option.getOpt());
}

From source file:dioscuri.TestCommandLineInterface.java

/**
 * Test all valid parameters:/* www .  ja va2s  . c o  m*/
 * <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:gobblin.runtime.cli.PublicMethodsCliObjectFactory.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 object with the correct
 * arguments./*from  ww w . j a va 2s  .c om*/
 */
public void applyCommandLineOptions(CommandLine cli, T 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);
    }
}

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

@Test
public void testCreateCharArg() {
    Option option = this.builder.create('c');
    assertNotNull("The option should not be null.", option);
    assertEquals("The option is incorrect.", "c", option.getOpt());
}

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

@Test
public void testCreateStringArg() {
    Option option = this.builder.create("test");
    assertNotNull("The option should not be null.", option);
    assertEquals("The option is incorrect.", "test", option.getOpt());
}

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

private Options inferOptionsFromMethods() {
    Options options = new Options();

    for (Method method : klazz.getMethods()) {
        if (canUseMethod(method)) {
            CliObjectOption annotation = method.isAnnotationPresent(CliObjectOption.class)
                    ? method.getAnnotation(CliObjectOption.class)
                    : null;/*from   ww  w  .  j  a  v a  2s  .  c om*/
            String optionName = annotation == null || Strings.isNullOrEmpty(annotation.name())
                    ? method.getName()
                    : annotation.name();
            String description = annotation == null ? "" : annotation.description();
            Option.Builder builder = Option.builder(optionName).desc(description);
            boolean hasArg = method.getParameterTypes().length > 0;
            if (hasArg) {
                builder.hasArg();
            }
            Option option = builder.build();
            options.addOption(option);
            this.methodsMap.put(option.getOpt(), method);
        }
    }

    return options;
}

From source file:net.sourceforge.jencrypt.CommandLineHelper.java

/**
 * Formatter for printing the help text describing command line options. The
 * options are ordered as they appear in indexOfOptions.
 * //from  w  w  w .  jav  a2 s.  co  m
 * @param indexOfOptions
 * @return
 */
private HelpFormatter getHelpFormatter(final String indexOfOptions) {
    HelpFormatter helpFormatter = new HelpFormatter();
    helpFormatter.setOptionComparator(new Comparator<Option>() {
        public int compare(Option o1, Option o2) {
            return indexOfOptions.indexOf(o1.getOpt()) - indexOfOptions.indexOf(o2.getOpt());
        }
    });
    return helpFormatter;
}