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

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

Introduction

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

Prototype

public boolean hasArgs() 

Source Link

Document

Query to see if this Option can take many values.

Usage

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

public static Configuration parseOptions(String[] args) throws ParseException {
    Options options = buildOptions();/*from   w  ww . j  ava  2 s. c  om*/

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);

    Configuration result = new MapConfiguration(new HashMap<String, Object>());

    if (cmd.hasOption('B')) {
        // check backends
        Set<String> existing = Sets
                .newHashSet(Iterators.transform(backends.iterator(), new BackendIdentifierFunction()));
        if (!existing.contains(cmd.getOptionValue('B'))) {
            throw new ParseException("the backend " + cmd.getOptionValue('B') + " does not exist");
        }

        result.setProperty(LoaderOptions.BACKEND, cmd.getOptionValue('B'));
    }

    if (cmd.hasOption('b')) {
        result.setProperty(LoaderOptions.BASE_URI, cmd.getOptionValue('b'));
    }

    if (cmd.hasOption('z')) {
        result.setProperty(LoaderOptions.COMPRESSION, CompressorStreamFactory.GZIP);
    }

    if (cmd.hasOption('j')) {
        result.setProperty(LoaderOptions.COMPRESSION, CompressorStreamFactory.BZIP2);
    }

    if (cmd.hasOption('c')) {
        result.setProperty(LoaderOptions.CONTEXT, cmd.getOptionValue('c'));
    }

    if (cmd.hasOption('t')) {
        RDFFormat fmt = getRDFFormat(cmd.getOptionValue('t'));
        if (fmt == null) {
            throw new ParseException("unrecognized MIME type: " + cmd.getOptionValue('t'));
        }

        result.setProperty(LoaderOptions.FORMAT, fmt.getDefaultMIMEType());
    }

    if (cmd.hasOption('f')) {
        result.setProperty(LoaderOptions.FILES, Arrays.asList(cmd.getOptionValues('f')));
    }

    if (cmd.hasOption('d')) {
        result.setProperty(LoaderOptions.DIRS, Arrays.asList(cmd.getOptionValues('d')));
    }

    if (cmd.hasOption('a')) {
        result.setProperty(LoaderOptions.ARCHIVES, Arrays.asList(cmd.getOptionValues('a')));
    }

    if (cmd.hasOption('s')) {
        result.setProperty(LoaderOptions.STATISTICS_ENABLED, true);
        result.setProperty(LoaderOptions.STATISTICS_GRAPH, cmd.getOptionValue('s'));
    }

    if (cmd.hasOption('D')) {
        for (Map.Entry e : cmd.getOptionProperties("D").entrySet()) {
            result.setProperty(e.getKey().toString(), e.getValue());
        }
    }

    for (LoaderBackend b : backends) {
        for (Option option : b.getOptions()) {
            if (cmd.hasOption(option.getOpt())) {
                String key = String.format("backend.%s.%s", b.getIdentifier(),
                        option.getLongOpt() != null ? option.getLongOpt() : option.getOpt());
                if (option.hasArg()) {
                    if (option.hasArgs()) {
                        result.setProperty(key, Arrays.asList(cmd.getOptionValues(option.getOpt())));
                    } else {
                        result.setProperty(key, cmd.getOptionValue(option.getOpt()));
                    }
                } else {
                    result.setProperty(key, true);
                }
            }
        }
    }

    return result;
}

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

/**
 * Constructs a new instance from the supplied command line.
 *
 * @param command The command./*  ww  w .  ja  va2  s.c om*/
 * @param commandLine The command line.
 * @param args The orginal command line args.
 */
public CommandLine(Command command, org.apache.commons.cli.CommandLine commandLine, String[] args) {
    this.command = command;
    this.args = args;
    Option[] options = commandLine.getOptions();
    for (Option option : options) {
        if (option.hasArgs()) {
            this.options.put(option.getOpt(), commandLine.getOptionValues(option.getOpt()));
        } else { //if(option.hasArg() || option.hasOptionalArg()){
            this.options.put(option.getOpt(), commandLine.getOptionValue(option.getOpt()));
        }
    }
    unrecognized = commandLine.getArgs();
}

From source file:org.g_node.srv.CliOptionServiceTest.java

/**
 * Main assertions of all option arguments.
 * @param opt The actual {@link Option}.
 * @param shortOpt Short commandline argument of the current option.
 * @param longOpt Long commandline argument of the current option.
 * @param desc Description text of the current option.
 * @param isRequired Whether the current option is a required commandline argument.
 * @param hasArguments Whether the current option requires an additional argument.
 *///from ww  w  . jav  a 2 s.  c  o  m
private void assertOption(final Option opt, final String shortOpt, final String longOpt, final String desc,
        final Boolean isRequired, final Boolean hasArgument, final Boolean hasArguments) {
    assertThat(opt.getOpt()).isEqualToIgnoringCase(shortOpt);
    assertThat(opt.getLongOpt()).isEqualToIgnoringCase(longOpt);
    assertThat(opt.getDescription()).contains(desc);
    assertThat(opt.isRequired()).isEqualTo(isRequired);
    assertThat(opt.hasArg()).isEqualTo(hasArgument);
    assertThat(opt.hasArgs()).isEqualTo(hasArguments);
}

From source file:org.ldp4j.tutorial.client.ContactsShell.java

protected void debug(String command, CommandLine commandLine) {
    this.console.message("- Command: ").metadata(command).message("%n");
    for (Option opt : options.getOptions()) {
        if (commandLine.hasOption(opt.getOpt())) {
            if (!opt.hasArg()) {
                this.console.metadata("  + %s%n", opt.getOpt());
            } else {
                if (!opt.hasArgs()) {
                    this.console.metadata("  + %s: ", opt.getOpt()).data("%s%n",
                            commandLine.getOptionValue(opt.getOpt()));
                } else {
                    this.console.metadata("  + %s: ", opt.getOpt()).data("%s%n",
                            Arrays.toString(commandLine.getOptionValues(opt.getOpt())));
                }/* w ww .j  av a 2s.  c om*/
            }
        }
    }
    List<String> argList = commandLine.getArgList();
    if (!argList.isEmpty()) {
        this.console.metadata("  + Arguments:%n");
        for (String arg : argList) {
            this.console.metadata("    * ").data("%s%n", arg);
        }
    }
}

From source file:org.stathissideris.ascii2image.core.CommandLineConverter.java

private static void printRunInfo(CommandLine cmdLine) {
    System.out.println("\n" + notice + "\n");

    System.out.println("Running with options:");
    Option[] opts = cmdLine.getOptions();
    for (Option option : opts) {
        if (option.hasArgs()) {
            for (String value : option.getValues()) {
                System.out.println(option.getLongOpt() + " = " + value);
            }//from w w w. ja v  a 2s  . co m
        } else if (option.hasArg()) {
            System.out.println(option.getLongOpt() + " = " + option.getValue());
        } else {
            System.out.println(option.getLongOpt());
        }
    }
}

From source file:ro.cs.products.Executor.java

private static void printCommandLine(CommandLine cmd) {
    Logger.getRootLogger().debug("Executing with the following arguments:");
    for (Option option : cmd.getOptions()) {
        if (option.hasArgs()) {
            Logger.getRootLogger().debug(option.getOpt() + "=" + String.join(" ", option.getValues()));
        } else if (option.hasArg()) {
            Logger.getRootLogger().debug(option.getOpt() + "=" + option.getValue());
        } else {//from  w w  w  .ja v  a2 s. co  m
            Logger.getRootLogger().debug(option.getOpt());
        }
    }
}