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

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

Introduction

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

Prototype

public int getArgs() 

Source Link

Document

Returns the number of argument values this Option can take.

Usage

From source file:org.codeseed.common.config.ext.CommandLineOptionsBuilder.java

/**
 * Potentially adds an option to the supplied collection. Used by the
 * {@link #build()} method to populate an options collection.
 *
 * @param options/*from  www .  j a  va  2 s. com*/
 *            the current collection of options
 * @param groups
 *            mappings of argument group identifiers to groups
 * @param method
 *            the configuration method to look up
 */
protected void addOption(Options options, Map<String, OptionGroup> groups, Method method) {
    final CommandLine commandLine = method.getAnnotation(CommandLine.class);

    // Iterate over the triggers; take the first values
    String opt = null;
    String longOpt = null;
    for (String trigger : commandLine.value()) {
        if (!options.hasOption(trigger)) {
            if (opt == null && trigger.length() == 1) {
                opt = trigger;
            } else if (longOpt == null) {
                longOpt = trigger;
            }
        }
    }

    // Either we can use the method name or there is no option being added
    if (opt == null && longOpt == null) {
        String methodOpt = LOWER_CAMEL.to(LOWER_HYPHEN, method.getName());
        if (!options.hasOption(methodOpt)) {
            longOpt = methodOpt;
        } else {
            // TODO Warn?
            return;
        }
    }

    // Create a new option
    Option option = new Option(opt, null);
    option.setLongOpt(longOpt);

    // Set the number of arguments based on the return type
    final Class<?> returnType = Primitives.wrap(method.getReturnType());
    if (returnType.equals(Boolean.class)) {
        option.setArgs(0);
    } else if (Iterable.class.isAssignableFrom(returnType)) {
        option.setArgs(commandLine.maximum());
    } else if (Map.class.isAssignableFrom(returnType)) {
        option.setArgs(2);
        option.setValueSeparator('=');
    } else {
        option.setArgs(1);
    }

    // Add some descriptive text
    if (bundle != null) {
        try {
            // TODO Does this make sense?
            String key = option.hasLongOpt() ? option.getLongOpt() : method.getName();
            option.setDescription(bundle.getString(key + ".description"));
        } catch (MissingResourceException e) {
            option.setDescription(null);
        }
    }

    // Set argument names
    if (bundle != null && option.getArgs() > 0) {
        try {
            option.setArgName(bundle.getString(method.getName() + ".argName"));
        } catch (MissingResourceException e) {
            option.setArgName(null);
        }
    }

    // Add to either the collection or to the option groups
    String groupKey = commandLine.groupId();
    if (groupKey.isEmpty()) {
        options.addOption(option);
    } else {
        OptionGroup group = groups.get(groupKey);
        if (group == null) {
            group = new OptionGroup();
            groups.put(groupKey, group);
        }
        group.addOption(option);
    }
}

From source file:org.jcryptool.commands.core.HelpCommand.java

/**
 * Recovers the command line form of which a CommandLine object could've originated from. Assumes, that all options
 * have been stated in short form.//from w  w  w .j a va 2 s  .  c  om
 *
 * @param commandName the name of the command
 * @param cmdLine the CommandLine object to parse
 * @return a command line, which would parse to a CommandLine object equal to the given one.
 */
public static String reverseCommandline(String commandName, CommandLine cmdLine) {
    StringBuilder builder = new StringBuilder();
    builder.append(commandName);
    for (String arg : cmdLine.getArgs()) {
        builder.append(" " + arg); //$NON-NLS-1$
    }

    for (Option option : cmdLine.getOptions()) {
        builder.append(" -" + option.getOpt()); //$NON-NLS-1$
        for (int i = 0; i < option.getArgs(); i++) {
            builder.append(" " + (option.getValue(i).contains(" ") ? "\"" + option.getValue(i) + "\"" //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
                    : option.getValue(i)));
        }
    }

    return builder.toString();
}