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:com.cloudera.csd.tools.MetricDescriptorGeneratorTool.java

private MapConfiguration generateAndValidateConfig(CommandLine cmdLine) throws ParseException {
    Preconditions.checkNotNull(cmdLine);
    MapConfiguration ret = new MapConfiguration(Maps.<String, Object>newHashMap());

    for (Option option : cmdLine.getOptions()) {
        ret.addProperty(option.getLongOpt(), option.getValue());
    }//from   w  ww. jav a2s  .  co m

    if (null == ret.getProperty(OPT_INPUT_MDL.getLongOpt())) {
        throw new ParseException("MetricGeneratorTool missing mdl file " + "location");
    } else {
        String fileName = ret.getString(OPT_INPUT_MDL.getLongOpt());
        File file = new File(fileName);
        if (!file.exists()) {
            throw new ParseException("MDL file '" + fileName + "' does not " + "exist");
        } else if (!file.isFile()) {
            throw new ParseException("MDL file '" + fileName + "' is not a " + "file");
        }
    }

    if (null == ret.getProperty(OPT_INPUT_FIXTURE.getLongOpt())) {
        throw new ParseException("MetricGeneratorTool missing fixture file " + "location");
    } else {
        String fileName = ret.getString(OPT_INPUT_FIXTURE.getLongOpt());
        File file = new File(fileName);
        if (!file.exists()) {
            throw new ParseException("Fixture file '" + fileName + "' does not " + "exist");
        } else if (!file.isFile()) {
            throw new ParseException("Fixture file '" + fileName + "' is not a " + "file");
        }
    }

    if (null != ret.getProperty(OPT_INPUT_CONVENTIONS.getLongOpt())) {
        String fileName = ret.getString(OPT_INPUT_CONVENTIONS.getLongOpt());
        File file = new File(fileName);
        if (!file.exists()) {
            throw new ParseException("Conventions file '" + fileName + "' does " + "not exist");
        } else if (!file.isFile()) {
            throw new ParseException("Conventions file '" + fileName + "' is " + "not a file");
        }
    }

    if (null == ret.getProperty(OPT_ADAPTER_CLASS.getLongOpt())) {
        throw new ParseException("MetricGeneratorTool missing adapter class");
    } else {
        String className = ret.getString(OPT_ADAPTER_CLASS.getLongOpt());
        try {
            Class<?> adapterClass = this.getClass().getClassLoader().loadClass(className);
            if (!MetricFixtureAdapter.class.isAssignableFrom(adapterClass)) {
                throw new ParseException("Adapter class " + className + "is of the " + "wrong type");
            }
            ret.addProperty(ADAPTER_CLASS_CONFIG, adapterClass);
        } catch (ClassNotFoundException e) {
            throw new ParseException("Unknown metric adapter " + className);
        }
    }
    return ret;
}

From source file:com.splunk.Command.java

public Command parse(String[] argv) {
    CommandLineParser parser = new PosixParser();

    CommandLine cmdline = null;/* w  w  w. j a  v a2s . c  o  m*/
    try {
        cmdline = parser.parse(this.rules, argv);
    } catch (ParseException e) {
        error(e.getMessage());
    }

    // Unpack the cmdline into a simple Map of options and optionally
    // assign values to any corresponding fields found in the Command class.
    for (Option option : cmdline.getOptions()) {
        String name = option.getLongOpt();
        Object value = option.getValue();

        // Figure out the type of the option and convert the value.
        if (!option.hasArg()) {
            // If it has no arg, then its implicitly boolean and presence
            // of the argument indicates truth.
            value = true;
        } else {
            Class type = (Class) option.getType();
            if (type == null) {
                // Null implies String, no conversion necessary
            } else if (type == Integer.class) {
                value = Integer.parseInt((String) value);
            } else {
                assert false; // Unsupported type
            }
        }

        this.opts.put(name, value);

        // Look for a field of the Command class (or subclass) that
        // matches the long name of the option and, if found, assign the
        // corresponding option value in order to provide simplified
        // access to command options.
        try {
            java.lang.reflect.Field field = this.getClass().getField(name);
            field.set(this, value);
        } catch (NoSuchFieldException e) {
            continue;
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    String[] orig = this.args;
    String[] more = cmdline.getArgs();
    this.args = new String[orig.length + more.length];
    System.arraycopy(orig, 0, this.args, 0, orig.length);
    System.arraycopy(more, 0, this.args, orig.length, more.length);

    if (this.help) {
        printHelp();
        System.exit(0);
    }

    return this;
}

From source file:com.somerledsolutions.pa11y.client.cli.OptionsBuilderTest.java

private void assertHelpOption(Options options) {
    Option helpOption = options.getOption("h");
    assertEquals("h", helpOption.getOpt());
    assertEquals("Help", helpOption.getArgName());
    assertEquals("help", helpOption.getLongOpt());
    assertEquals("Shows this help", helpOption.getDescription());
}

From source file:com.somerledsolutions.pa11y.client.cli.OptionsBuilderTest.java

private void assertUrlOption(Options options) {
    Option createOption = options.getOption("u");
    assertEquals("u", createOption.getOpt());
    assertEquals("URL", createOption.getArgName());
    assertEquals("url", createOption.getLongOpt());
    assertEquals("The base URL of the pa11y webservice", createOption.getDescription());
}

From source file:com.somerledsolutions.pa11y.client.cli.OptionsBuilderTest.java

private void assertRunOption(Options options) {
    Option taskIdOption = options.getOption("r");
    assertEquals("r", taskIdOption.getOpt());
    assertEquals("Run", taskIdOption.getArgName());
    assertEquals("run", taskIdOption.getLongOpt());
    assertEquals("Run a task by ID, generating new results", taskIdOption.getDescription());
}

From source file:com.somerledsolutions.pa11y.client.cli.OptionsBuilderTest.java

private void assertRetrieveTaskOption(Options options) {
    Option taskIdOption = options.getOption("g");
    assertEquals("g", taskIdOption.getOpt());
    assertEquals("Get", taskIdOption.getArgName());
    assertEquals("get", taskIdOption.getLongOpt());
    assertEquals("Get a single task by ID", taskIdOption.getDescription());
}

From source file:com.somerledsolutions.pa11y.client.cli.OptionsBuilderTest.java

private void assertNameOption(Options options) {
    Option createOption = options.getOption("n");
    assertEquals("n", createOption.getOpt());
    assertEquals("Name", createOption.getArgName());
    assertEquals("name", createOption.getLongOpt());
    assertEquals("The name of the task", createOption.getDescription());
}

From source file:kieker.tools.util.CLIHelpFormatter.java

@SuppressWarnings("unchecked")
@Override/*from  ww  w.j a va  2 s .  co  m*/
protected StringBuffer renderOptions(final StringBuffer sb, final int width, final Options options,
        final int leftPad, final int descPad) {
    final String lpad = this.createPadding(leftPad);
    final String dpad = this.createPadding(8); // we use a fixed value instead of descPad

    StringBuilder optBuf;

    final List<Option> optList = new ArrayList<Option>(options.getOptions());
    Collections.sort(optList, this.getOptionComparator());

    for (final Iterator<Option> i = optList.iterator(); i.hasNext();) {
        final Option option = i.next();

        optBuf = new StringBuilder(8);

        if (option.getOpt() == null) {
            optBuf.append(lpad).append("   ").append(this.getLongOptPrefix()).append(option.getLongOpt());
        } else {
            optBuf.append(lpad).append(this.getOptPrefix()).append(option.getOpt());

            if (option.hasLongOpt()) {
                optBuf.append(',').append(this.getLongOptPrefix()).append(option.getLongOpt());
            }
        }

        if (option.hasArg()) {
            if (option.hasArgName()) {
                optBuf.append(" <").append(option.getArgName()).append('>');
            } else {
                optBuf.append(' ');
            }
        }

        sb.append(optBuf.toString()).append(this.getNewLine());

        optBuf = new StringBuilder();
        optBuf.append(dpad);

        if (option.getDescription() != null) {
            optBuf.append(option.getDescription());
        }

        this.renderWrappedText(sb, width, dpad.length(), optBuf.toString());

        if (i.hasNext()) {
            sb.append(this.getNewLine());
            sb.append(this.getNewLine());
        }
    }

    return sb;
}

From source file:dioscuri.TestCommandLineInterface.java

/**
 * Test all valid parameters:/*from   w ww .  ja v a  2s.  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:com.zuehlke.sbdfx.utils.CampApplBase.java

protected List<String> getOptionValues(final Option option) {
    final String[] optionValues = commandLine.getOptionValues(option.getLongOpt());
    if (optionValues == null) {
        return Collections.emptyList();
    }/* w ww. java  2 s .com*/
    final List<String> result = Lists.newArrayList();
    for (final String optionValue : optionValues) {
        if (StringUtils.isNotBlank(optionValue)) {
            result.add(optionValue);
        }
    }
    return result;
}