Example usage for org.apache.commons.cli Options getOption

List of usage examples for org.apache.commons.cli Options getOption

Introduction

In this page you can find the example usage for org.apache.commons.cli Options getOption.

Prototype

public Option getOption(String opt) 

Source Link

Document

Retrieve the Option matching the long or short name specified.

Usage

From source file:com.emc.ecs.sync.config.ConfigUtilTest.java

@Test
public void testCliOptionGeneration() {
    Options options = ConfigUtil.wrapperFor(Foo.class).getOptions();

    assertOption(options.getOption("my-value"), "my-value", false, 1, "my-value");
    assertOption(options.getOption("my-num"), "my-num", false, 1, "my-num");
    assertOption(options.getOption("my-flag"), "my-flag", false, -1, null);
    assertOption(options.getOption("no-negative-flag"), "no-negative-flag", false, -1, null);
    assertOption(options.getOption("my-enum"), "my-enum", false, 1, "my-enum");
}

From source file:com.zimbra.cs.volume.VolumeCLI.java

private void printOpt(String optStr, int leftPad) {
    Options options = getOptions();
    Option opt = options.getOption(optStr);
    StringBuilder buf = new StringBuilder();
    buf.append(Strings.repeat(" ", leftPad));
    buf.append('-').append(opt.getOpt()).append(",--").append(opt.getLongOpt());
    if (opt.hasArg()) {
        buf.append(" <arg>");
    }//from  www .j av a  2  s. com
    buf.append(Strings.repeat(" ", 35 - buf.length()));
    buf.append(opt.getDescription());
    System.err.println(buf.toString());
}

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

@Test
public void testBuildPa11yOptions() throws Exception {
    Options options = OptionsBuilder.buildPa11yOptions();

    assertEquals(10, options.getOptions().size());

    assertPa11yOptions(options);//from  ww w .j a v  a  2 s.  c om

    OptionGroup optionGroup = options.getOptionGroup(options.getOption("c"));
    Collection<String> optionGroupNames = optionGroup.getNames();
    assertTrue(optionGroupNames.contains("c"));
    assertTrue(optionGroupNames.contains("l"));
    assertTrue(optionGroupNames.contains("r"));
    assertTrue(optionGroupNames.contains("g"));
    assertTrue(optionGroupNames.contains("d"));
}

From source file:com.google.cloud.runtimes.builder.config.AppYamlParserTest.java

@Test
public void testAddOverrideSettingsToOptions() {
    Options options = new Options();
    Application.addOverrideSettingsToOptions(options);

    assertFalse(options.getOption("enable_app_engine_apis").hasArg());
    assertEquals("Replaces the setting from app.yaml under beta_settings : enable_app_engine_apis",
            options.getOption("enable_app_engine_apis").getDescription());

    assertFalse(options.getOption("jetty_quickstart").hasArg());
    assertEquals("Replaces the setting from app.yaml under runtime_config : jetty_quickstart",
            options.getOption("jetty_quickstart").getDescription());

    assertTrue(options.getOption("build_script").hasArg());
    assertEquals("Replaces the setting from app.yaml under runtime_config : build_script",
            options.getOption("build_script").getDescription());

    assertTrue(options.getOption("jdk").hasArg());
    assertEquals("Replaces the setting from app.yaml under runtime_config : jdk",
            options.getOption("jdk").getDescription());

    assertTrue(options.getOption("server").hasArg());
    assertEquals("Replaces the setting from app.yaml under runtime_config : server",
            options.getOption("server").getDescription());

    assertTrue(options.getOption("artifact").hasArg());
    assertEquals("Replaces the setting from app.yaml under runtime_config : artifact",
            options.getOption("artifact").getDescription());
}

From source file:com.bc.fiduceo.post.PostProcessingToolTest.java

@Test
public void testOptions() {
    final Options options = PostProcessingTool.getOptions();
    assertEquals(6, options.getOptions().size());

    Option o;//from www.j a  v  a 2 s  .  co m

    o = options.getOption("c");
    assertNotNull(o);
    assertEquals("config", o.getLongOpt());
    assertEquals("Defines the configuration directory. Defaults to './config'.", o.getDescription());
    assertEquals(true, o.hasArg());
    assertEquals(false, o.isRequired());

    o = options.getOption("i");
    assertNotNull(o);
    assertEquals("input-dir", o.getLongOpt());
    assertEquals("Defines the path to the input mmd files directory.", o.getDescription());
    assertEquals(true, o.hasArg());
    assertEquals(true, o.isRequired());

    o = options.getOption("end");
    assertNotNull(o);
    assertEquals("end-date", o.getLongOpt());
    assertEquals("Defines the processing end-date, format 'yyyy-DDD'. DDD = Day of year.", o.getDescription());
    assertEquals(true, o.hasArg());
    assertEquals(true, o.isRequired());

    o = options.getOption("h");
    assertNotNull(o);
    assertEquals("help", o.getLongOpt());
    assertEquals("Prints the tool usage.", o.getDescription());
    assertEquals(false, o.hasArg());
    assertEquals(false, o.isRequired());

    o = options.getOption("j");
    assertNotNull(o);
    assertEquals("job-config", o.getLongOpt());
    assertEquals(
            "Defines the path to post processing job configuration file. Path is relative to the configuration directory.",
            o.getDescription());
    assertEquals(true, o.hasArg());
    assertEquals(true, o.isRequired());

    o = options.getOption("start");
    assertNotNull(o);
    assertEquals("start-date", o.getLongOpt());
    assertEquals("Defines the processing start-date, format 'yyyy-DDD'. DDD = Day of year.",
            o.getDescription());
    assertEquals(true, o.hasArg());
    assertEquals(true, o.isRequired());
}

From source file:com.conversantmedia.mapreduce.tool.ToolContext.java

private String[] handleHelp(String[] args, Options fullOptions) {
    Options options = new Options();
    options.addOption(fullOptions.getOption("help"));
    Parser p = new PosixParser();
    try {/*from ww w.  j  a v  a2 s .  c om*/
        CommandLine line = p.parse(options, args, true);
        if (line.hasOption('?')) {
            showHelpAndExit(fullOptions, 0);
            new HelpFormatter().printHelp(getCommandLineSyntax(), fullOptions);
            System.exit(0);
        }
        return line.getArgs();
    } catch (ParseException e) {
        // ignore
    }
    return args;
}

From source file:com.bc.fiduceo.matchup.MatchupToolTest.java

@Test
public void testGetOptions() {
    final Options options = MatchupTool.getOptions();
    assertNotNull(options);/*  w  ww.j  ava2s . c o  m*/

    final Option helpOption = options.getOption("h");
    assertNotNull(helpOption);
    assertEquals("h", helpOption.getOpt());
    assertEquals("help", helpOption.getLongOpt());
    assertEquals("Prints the tool usage.", helpOption.getDescription());
    assertFalse(helpOption.hasArg());

    final Option configOption = options.getOption("config");
    assertNotNull(configOption);
    assertEquals("c", configOption.getOpt());
    assertEquals("config", configOption.getLongOpt());
    assertEquals("Defines the configuration directory. Defaults to './config'.", configOption.getDescription());
    assertTrue(configOption.hasArg());

    final Option startOption = options.getOption("start");
    assertNotNull(startOption);
    assertEquals("start", startOption.getOpt());
    assertEquals("start-date", startOption.getLongOpt());
    assertEquals("Defines the processing start-date, format 'yyyy-DDD'", startOption.getDescription());
    assertTrue(startOption.hasArg());

    final Option endOption = options.getOption("end");
    assertNotNull(endOption);
    assertEquals("end", endOption.getOpt());
    assertEquals("end-date", endOption.getLongOpt());
    assertEquals("Defines the processing end-date, format 'yyyy-DDD'", endOption.getDescription());
    assertTrue(endOption.hasArg());

    final Option useCaseOption = options.getOption("usecase");
    assertNotNull(useCaseOption);
    assertEquals("u", useCaseOption.getOpt());
    assertEquals("usecase", useCaseOption.getLongOpt());
    assertEquals(
            "Defines the path to the use-case configuration file. Path is relative to the configuration directory.",
            useCaseOption.getDescription());
    assertTrue(useCaseOption.hasArg());
}

From source file:edu.umn.msi.gx.mztosqlite.MzToSQLite.java

public final void parseOptions(String[] args) {
    Integer MAX_INPUTS = 100;//  w ww  .  ja v a 2 s . c  o m
    Parser parser = new BasicParser();
    String dbOpt = "sqlite";
    String inputFileOpt = "input";
    String inputNameOpt = "name";
    String inputIdOpt = "encoded_id";
    String verboseOpt = "verbose";
    String helpOpt = "help";
    Options options = new Options();
    options.addOption("s", dbOpt, true, "SQLite output file");
    options.addOption("v", verboseOpt, false, "verbose");
    options.addOption("h", helpOpt, false, "help");
    options.addOption("i", inputFileOpt, verbose, "input file");
    options.addOption("n", inputNameOpt, verbose, "name for input file");
    options.addOption("e", inputIdOpt, verbose, "encoded id for input file");
    options.addOption("f", inputIdOpt, verbose, "FASTA Search Database files");
    options.getOption(inputFileOpt).setArgs(MAX_INPUTS);
    options.getOption(inputNameOpt).setArgs(MAX_INPUTS);
    options.getOption(inputIdOpt).setArgs(MAX_INPUTS);
    // create the parser
    try {
        // parse the command line arguments
        CommandLine cli = parser.parse(options, args);
        if (cli.hasOption(helpOpt)) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar MzToSQLite.jar [options] [proteomics_data_file ...]", options);
            exit(0);
        }
        if (cli.hasOption(verboseOpt)) {
            verbose = true;
        }
        if (cli.hasOption(dbOpt)) {
            dbPath = cli.getOptionValue(dbOpt);
            mzSQLiteDB = new MzSQLiteDB(dbPath);
            try {
                mzSQLiteDB.createTables();
            } catch (SqlJetException ex) {
                Logger.getLogger(MzToSQLite.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        List<String> argList = cli.getArgList();
        if (argList != null) {
            for (String filePath : argList) {
                File inputFile = new File(filePath);
                if (inputFile.canRead()) {
                    try {
                        ProteomicsFormat format = ProteomicsFormat.getFormat(inputFile);
                        switch (format) {
                        case MZID:
                            identFiles.put(filePath, format);
                            break;
                        case MZML:
                        case MGF:
                        case DTA:
                        case MS2:
                        case PKL:
                        case MZXML:
                        case XML_FILE:
                        case MZDATA:
                        case PRIDEXML:
                            scanFiles.put(filePath, format);
                            break;
                        case FASTA:
                            seqDbFiles.put(filePath, format);
                            break;
                        case PEPXML:
                        case UNSUPPORTED:
                        default:
                            Logger.getLogger(MzToSQLite.class.getName()).log(Level.WARNING,
                                    "Unknown or unsupported format: {0}", filePath);
                            break;
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(MzToSQLite.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    Logger.getLogger(MzToSQLite.class.getName()).log(Level.WARNING, "Unable to read {0}",
                            filePath);
                }
            }
        }
    } catch (ParseException exp) {
        Logger.getLogger(MzToSQLite.class.getName()).log(Level.SEVERE, null, exp);
    }

}

From source file:de.vandermeer.skb.commons.utils.Test_CLIApache.java

@Test
public void testDeclareOptions() {
    CLIApache aca = new CLIApache();
    aca.declareOptions(this.prepareComProperties());

    assertEquals(6, aca.optionList.size());
    for (String s : this.rows()) {
        assertTrue(aca.optionList.containsKey(Table.defaulSeparator + s));
    }//  ww w  .  j  a  v a  2 s.co  m

    Options op = aca.options;

    assertEquals(6, op.getOptions().size());

    assertTrue(op.hasOption("-b"));
    assertTrue(op.hasOption("-d"));
    assertTrue(op.hasOption("-i"));
    assertTrue(op.hasOption("-l"));
    assertTrue(op.hasOption("-s"));

    assertTrue(op.hasOption("--test-boolean"));
    assertTrue(op.hasOption("--test-double"));
    assertTrue(op.hasOption("--test-integer"));
    assertTrue(op.hasOption("--test-long"));
    assertTrue(op.hasOption("--test-string"));

    assertTrue(op.hasOption("--test-string-w-arg"));
    assertTrue(op.getOption("--test-string-w-arg").hasArg());
    assertTrue(op.getOption("--test-string-w-arg").hasArgName());
}

From source file:com.axelor.shell.core.Target.java

public Object[] findArguments(String[] args) {
    final List<Object> arguments = new ArrayList<>(method.getParameterTypes().length);
    final Options options = getOptions();

    final CommandLineParser lineParser = new BasicParser();
    final CommandLine cmdLine;
    try {/*ww w. ja v a2  s. c  o  m*/
        cmdLine = lineParser.parse(options, args);
    } catch (ParseException e) {
        System.out.println();
        System.out.println(e.getMessage());
        System.out.println();
        return null;
    }

    for (CliOption cliOption : cliOptions) {
        if (cliOption == null) {
            arguments.add(cmdLine.getArgs());
            continue;
        }

        String key = "" + cliOption.shortName();
        if (isBlank(key)) {
            key = cliOption.name();
        }

        Option opt = options.getOption(key);

        Object value = false;
        if (opt.hasArgs()) {
            value = cmdLine.getOptionValues(key);
        } else if (opt.hasArg()) {
            value = cmdLine.getOptionValue(key);
        } else {
            value = cmdLine.hasOption(key);
        }

        arguments.add(value);
    }

    return arguments.toArray();
}