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:com.emc.ecs.sync.config.ConfigUtilTest.java

private void assertOption(org.apache.commons.cli.Option option, String longOpt, boolean required, int args,
        String argName) {//from  w ww .  ja  va2 s . c  o m
    Assert.assertNull(option.getOpt());
    Assert.assertEquals(longOpt, option.getLongOpt());
    Assert.assertEquals(required, option.isRequired());
    Assert.assertEquals(args, option.getArgs());
    Assert.assertEquals(argName, option.getArgName());
}

From source file:com.zenoss.zenpacks.zenjmx.OptionsPrinter.java

/**
 * Returns a friendly string representation of the options
 *///from w  ww. ja  v a2  s  .c om
public String toString() {
    StringBuffer buffer = new StringBuffer();

    // get the required and optional arguments
    List required = _options.getRequiredOptions();
    Collection optional = new HashSet(_options.getOptions());

    buffer.append("Required Arguments:\n");
    for (Object nameObj : required) {
        String name = nameObj.toString();
        Option option = _options.getOption(name);

        buffer.append("  " + print(option) + "\n");
    }

    buffer.append("Options:\n");
    for (Object optionObj : optional) {
        Option option = (Option) optionObj;

        if (!required.contains(option.getOpt())) {
            buffer.append("  " + print(option) + "\n");
        }
    }

    return buffer.toString();
}

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

private Optional<String> getOptionValue(final Option option) {
    if (this.hasOption(option)) {
        final String val = this.cli.getOptionValue(option.getOpt());
        if (val == null || val.isEmpty()) {
            return Optional.empty();
        } else {/*from  w  w  w .j  a v  a  2 s .  c  om*/
            return Optional.of(val);
        }
    } else {
        return Optional.empty();
    }
}

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  . jav a 2s. c om*/
 *
 * @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:com.netcrest.pado.tools.pado.command.ls.java

@Override
public void run(CommandLine commandLine, String command) throws Exception {
    String path;//from  w  w w.  j a va2  s. com
    if (commandLine.getArgList().size() == 1) {
        path = padoShell.getCurrentPath();
    } else {
        path = (String) commandLine.getArgList().get(1);
    }
    boolean includeHidden = commandLine.hasOption("a");
    boolean longList = commandLine.hasOption("l");
    boolean recursive = commandLine.hasOption("R");
    Option options[] = commandLine.getOptions();
    for (Option option : options) {
        if (includeHidden == false) {
            includeHidden = option.getOpt().contains("a");
        }
        if (longList == false) {
            longList = option.getOpt().contains("l");
        }
        if (recursive == false) {
            recursive = option.getOpt().contains("R");
        }
    }
    listPaths(path, includeHidden, longList, recursive);
}

From source file:de.fosd.jdime.config.CommandLineConfigSource.java

@Override
protected Optional<String> getMapping(String key) {

    if (ARG_LIST.equals(key)) {
        List<String> argList = cmdLine.getArgList();

        if (argList.isEmpty()) {
            return Optional.empty();
        } else {// w ww . j av a 2  s. c  om
            return Optional.of(String.join(ARG_LIST_SEP, argList));
        }
    }

    if (!options.hasOption(key)) {
        return Optional.empty();
    }

    Option opt = options.getOption(key);
    String optName = opt.getOpt();

    if (opt.hasArg()) {
        return Optional.ofNullable(cmdLine.getOptionValue(optName));
    } else {
        return Optional.of(cmdLine.hasOption(optName) ? "true" : "false");
    }
}

From source file:jtabwb.launcher.CmdLineOptions.java

void defineCmdLineOptions() {
    LinkedList<Option> lo = new LinkedList<Option>();

    // HELP//from w  w  w  .  ja v  a2  s  .  c  o m
    lo.add(Option.builder(OptNames.HELP).desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.HELP).build());
    // INPUT
    lo.add(Option.builder(OptNames.INPUT).desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.INPUT_FORMULA)
            .build());
    // LATEX_CTREE
    lo.add(Option.builder().longOpt(OptNames.LATEX_CTREE)
            .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.LATEX_CTREE_FILE).build());
    // LATEX_PROOF
    lo.add(Option.builder().longOpt(OptNames.LATEX_PROOF)
            .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.LATEX_PROOF_FILE).build());
    if (configuration.availableProvers.size() > 1) {
        // LIST_PROVERS
        lo.add(Option.builder().longOpt(OptNames.LIST_PROVERS)
                .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.LIST_PROVERS).build());
        // PROVER
        if (configuration.availableProvers.size() > 1)
            lo.add(Option.builder(OptNames.PROVER).hasArg().argName("name")
                    .desc(String.format(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.PROVER,
                            configuration.availableProvers.getNames()))
                    .build());
    }
    if (configuration.availableReaders.size() > 1) {
        // READER
        lo.add(Option.builder(OptNames.READER).hasArg().argName("name")
                .desc(String.format(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.READER,
                        configuration.availableReaders.getNames()))
                .build());
        // LIST_READERS
        lo.add(Option.builder().longOpt(OptNames.LIST_READERS)
                .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.LIST_READER).build());
    }
    // LOG
    lo.add(Option.builder().longOpt(OptNames.LOG).desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.LOG_FILE)
            .build());
    // LOG_TIME
    lo.add(Option.builder().longOpt(OptNames.LOG_TIME).desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.LOG_TIME)
            .build());
    // SAVE_TRACE
    lo.add(Option.builder().longOpt(OptNames.SAVE_TRACE)
            .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.PTRACE_FILE).build());
    // VERBOSE
    lo.add(Option.builder(OptNames.VERBOSE).desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.VERBOSE).build());
    // TESTSET
    lo.add(Option.builder().longOpt(OptNames.TESTSET).hasArg(true).argName("test-name")
            .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.TESTSET).build());
    // LOGDIR
    lo.add(Option.builder().longOpt(OptNames.LOG_DIR).hasArg(true).argName("filename")
            .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.LOGDIR).build());
    // F3_TIME_STR
    lo.add(Option.builder().longOpt(OptNames.F3_TIME_STR).hasArg(false)
            .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.F3_TIME_STR).build());
    // JTABWB_TIME_STR 
    lo.add(Option.builder().longOpt(OptNames.JTABWB_TIME_STR).hasArg(false)
            .desc(MSG.CMD_LINE_OPTIONS.OPTIONS_DESCRIPTIONS.JTABWB_TIME_STR).build());

    for (Option opt : lo)
        if (configuration.cmdLineOptions.getOption(opt.getOpt()) != null
                || configuration.cmdLineOptions.getOption(opt.getLongOpt()) != null)
            throw new LauncherOptionDefinitionException(String.format(
                    MSG.CMD_LINE_OPTIONS.EXCEPTIONS.LAUNCHER_OPTION_NAME_REDEFINITION_ERROR, opt.getOpt()));
        else
            configuration.cmdLineOptions.addOption(opt);
}

From source file:com.revolsys.gis.tools.ProcessorPipelineTool.java

@SuppressWarnings("unchecked")
public boolean processArguments(final String[] args) {
    try {/*from   ww w .j a  v a2 s.  c  om*/
        final CommandLineParser parser = new PosixParser();
        this.commandLine = parser.parse(this.options, args);
        final List<String> arguments = this.commandLine.getArgList();
        final Option[] options = this.commandLine.getOptions();
        for (final Option option : options) {
            final String shortOpt = option.getOpt();
            if (shortOpt != null && shortOpt.equals("D")) {
                final String argument = arguments.remove(0);
                final String[] values = argument.split("=");
                System.setProperty(values[0], values[1]);
            }

        }
        if (this.commandLine.hasOption(SOURCE_DIRECTORY_OPTION)) {
            this.sourceDirectory = new File(this.commandLine.getOptionValue(SOURCE_DIRECTORY_OPTION));
            if (!this.sourceDirectory.isDirectory()) {
                System.err.println("Source directory '" + this.sourceDirectory.getAbsolutePath()
                        + "' does not exist or is not a directory");
                return false;
            }
        }
        if (this.commandLine.hasOption(SOURCE_FILE_EXTENSION_OPTION)) {
            this.sourceFileExtension = this.commandLine.getOptionValue(SOURCE_FILE_EXTENSION_OPTION);
        }
        if (this.commandLine.hasOption(OUTPUT_DIRECTORY_OPTION)) {
            this.targetDirectory = new File(this.commandLine.getOptionValue(OUTPUT_DIRECTORY_OPTION));
            if (!this.targetDirectory.isDirectory()) {
                System.err.println("Target directory '" + this.targetDirectory.getAbsolutePath()
                        + "' does not exist or is not a directory");
                return false;
            }
        }
        if (this.commandLine.hasOption(LOG_DIRECTORY_OPTION)) {
            this.logDirectory = new File(this.commandLine.getOptionValue(LOG_DIRECTORY_OPTION));
            if (!this.logDirectory.isDirectory()) {
                System.err.println("Log directory '" + this.logDirectory.getAbsolutePath()
                        + "' does not exist or is not a directory");
                return false;
            }
        }
        this.scriptFile = new File(this.commandLine.getOptionValue(SCRIPT_OPTION));
        if (!this.scriptFile.exists()) {
            System.err.println("The script '" + this.scriptFile + "' does not exist");
            return false;
        }
        this.excludePattern = this.commandLine.getOptionValue(EXCLUDE_PATTERN_OPTION);
        if (this.sourceDirectory != null) {
            if (this.targetDirectory == null) {
                System.err.println("A " + OUTPUT_DIRECTORY + " must be specified if " + SOURCE_DIRECTORY
                        + " is specified");
                return false;
            }
            if (this.sourceFileExtension == null) {
                System.err.println("A " + SOURCE_FLE_EXTENSION + " must be specified if " + SOURCE_DIRECTORY
                        + " is specified");
                return false;
            }
        } else {
            this.sourceFile = new File(arguments.get(0));
            if (!this.sourceFile.exists()) {
                System.err.println("The file '" + this.sourceFile + "' does not exist");
                return false;
            }
            this.targetFile = new File(arguments.get(1));
            // if (targetFile.isDirectory()) {
            // targetFile = new File(targetFile, sourceFile.getName());
            // }
        }
        return true;
    } catch (final MissingOptionException e) {
        System.err.println("Missing " + e.getMessage() + " argument");
        return false;
    } catch (final ParseException e) {
        System.err.println("Unable to process command line arguments: " + e.getMessage());
        return false;
    }
}

From source file:com.github.lindenb.jvarkit.util.command.CommandFactory.java

protected Status visit(final Option opt) {
    if (opt.getOpt().equals("version")) {
        stdout().println(getVersion());/*from   ww  w . jav a2 s.  c o m*/
        return Status.EXIT_SUCCESS;
    } else if (opt.getOpt().equals("h")) {
        usage(stdout());
        return Status.EXIT_SUCCESS;
    } else if (opt.getOpt().equals("proxy")) {
        String proxy = opt.getValue();
        int colon = proxy.lastIndexOf(':');
        if (colon == -1) {
            LOG.error("bad proxy \"" + proxy + "\"");
            return Status.EXIT_FAILURE;
        }
        LOG.debug("setting proxy " + proxy);
        System.setProperty("http.proxyHost", proxy.substring(0, colon));
        System.setProperty("http.proxyPort", proxy.substring(colon + 1));
        System.setProperty("https.proxyHost", proxy.substring(0, colon));
        System.setProperty("https.proxyPort", proxy.substring(colon + 1));
        return Status.OK;
    }
    LOG.fatal("Unknown option " + opt.getOpt());
    return Status.EXIT_FAILURE;
}

From source file:de.yaio.commons.config.Configuration.java

public void putCliOptions(org.apache.commons.cli.Option[] cliOptions) {
    for (final org.apache.commons.cli.Option prop : Arrays.asList(cliOptions)) {
        if (!StringUtils.isEmpty(prop.getLongOpt())) {
            this.putCliOption(new ConfigurationOption(prop.getLongOpt(), prop.getValue()));
        }//from  w  ww .  j  av a 2s.c o  m
        if (!StringUtils.isEmpty(prop.getOpt())) {
            this.putCliOption(new ConfigurationOption(prop.getOpt(), prop.getValue()));
        }
    }
}