Example usage for org.apache.commons.cli OptionBuilder hasOptionalArgs

List of usage examples for org.apache.commons.cli OptionBuilder hasOptionalArgs

Introduction

In this page you can find the example usage for org.apache.commons.cli OptionBuilder hasOptionalArgs.

Prototype

public static OptionBuilder hasOptionalArgs() 

Source Link

Document

The next Option can have an unlimited number of optional arguments.

Usage

From source file:org.eclim.command.Options.java

/**
 * Parses the String representation of an Option to an Option instance.
 *
 * @param option The option String./*from   ww  w  . j  av a  2s . c o  m*/
 * @return The Option.
 */
public Option parseOption(String option) {
    String[] parts = StringUtils.split(option);

    // command can have any additional arguments.
    //if(parts.length == 1 && ANY.equals(parts[0])){
    //}

    if (REQUIRED.equals(parts[0])) {
        OptionBuilder.isRequired();
    }
    if (ARG.equals(parts[3])) {
        OptionBuilder.hasArg();
        //OptionBuilder.withArgName(parts[2]);
    } else if (ANY.equals(parts[3])) {
        OptionBuilder.hasOptionalArgs();
    }
    OptionBuilder.withLongOpt(parts[2]);
    return OptionBuilder.create(parts[1]);
}

From source file:org.unigram.docvalidator.Main.java

public static void main(String[] args) throws DocumentValidatorException {
    Options options = new Options();
    options.addOption("h", "help", false, "help");

    OptionBuilder.withLongOpt("format");
    OptionBuilder.withDescription("input data format");
    OptionBuilder.hasArg();/* w  w  w.j a v a  2  s  .  c  o  m*/
    OptionBuilder.withArgName("FORMAT");
    options.addOption(OptionBuilder.create("f"));

    OptionBuilder.withLongOpt("input");
    OptionBuilder.withDescription("input file");
    OptionBuilder.hasOptionalArgs();
    OptionBuilder.withArgName("INPUT FILE");
    options.addOption(OptionBuilder.create("i"));

    OptionBuilder.withLongOpt("conf");
    OptionBuilder.withDescription("configuration file");
    OptionBuilder.hasArg();
    options.addOption(OptionBuilder.create("c"));

    OptionBuilder.withLongOpt("result-format");
    OptionBuilder.withDescription("output result format");
    OptionBuilder.hasArg();
    OptionBuilder.withArgName("RESULT FORMAT");
    options.addOption(OptionBuilder.create("r"));

    options.addOption("v", "version", false, "print the version information and exit");

    CommandLineParser parser = new BasicParser();
    CommandLine commandLine = null;

    try {
        commandLine = parser.parse(options, args);
    } catch (ParseException e) {
        LOG.error("Error occurred in parsing command line options ");
        printHelp(options);
        System.exit(-1);
    }

    String inputFormat = "plain";
    String[] inputFileNames = null;
    String configFileName = "";
    String resultFormat = "plain";
    Parser.Type parserType;
    Formatter.Type outputFormat;

    if (commandLine.hasOption("h")) {
        printHelp(options);
        System.exit(0);
    }
    if (commandLine.hasOption("v")) {
        System.out.println("1.0");
        System.exit(0);
    }
    if (commandLine.hasOption("f")) {
        inputFormat = commandLine.getOptionValue("f");
    }
    if (commandLine.hasOption("i")) {
        inputFileNames = commandLine.getOptionValues("i");
    }
    if (commandLine.hasOption("c")) {
        configFileName = commandLine.getOptionValue("c");
    }
    if (commandLine.hasOption("r")) {
        resultFormat = commandLine.getOptionValue("r");
    }

    ConfigurationLoader configLoader = new ConfigurationLoader();
    Configuration conf = configLoader.loadConfiguration(configFileName);
    if (conf == null) {
        LOG.error("Failed to initialize the DocumentValidator configuration.");
        System.exit(-1);
    }

    parserType = Parser.Type.valueOf(inputFormat.toUpperCase());
    outputFormat = Formatter.Type.valueOf(resultFormat.toUpperCase());

    DocumentCollection documentCollection = DocumentGenerator.generate(inputFileNames, conf, parserType);

    if (documentCollection == null) {
        LOG.error("Failed to create a DocumentCollection object");
        System.exit(-1);
    }
    ResultDistributor distributor = ResultDistributorFactory.createDistributor(outputFormat, System.out);

    DocumentValidator validator = new DocumentValidator.Builder().setConfiguration(conf)
            .setResultDistributor(distributor).build();

    validator.check(documentCollection);

    System.exit(0);
}