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

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

Introduction

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

Prototype

public String getLongOpt() 

Source Link

Document

Retrieve the long name of this Option.

Usage

From source file:org.apache.maven.cli.CLIManagerDocumentationTest.java

public String getOptionsAsHtml() {
    StringBuilder sb = new StringBuilder(512);
    boolean a = true;
    sb.append(//from  w w w. j av a 2s . com
            "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>");
    for (Option option : new CLIManagerExtension().getOptions()) {
        a = !a;
        sb.append("<tr class='").append(a ? 'a' : 'b').append("'><td><code>-<a name='");
        sb.append(option.getOpt());
        sb.append("'>");
        sb.append(option.getOpt());
        sb.append("</a>,--<a name='");
        sb.append(option.getLongOpt());
        sb.append("'>");
        sb.append(option.getLongOpt());
        sb.append("</a>");
        if (option.hasArg()) {
            if (option.hasArgName()) {
                sb.append(" &lt;").append(option.getArgName()).append("&gt;");
            } else {
                sb.append(' ');
            }
        }
        sb.append("</code></td><td>");
        sb.append(option.getDescription());
        sb.append("</td></tr>");
        sb.append(LS);
    }
    sb.append("</table>");
    return sb.toString();
}

From source file:org.apache.maven.cli.CLIManagerTest.java

public String getOptionsAsHtml() {
    StringBuilder sb = new StringBuilder();
    boolean a = true;
    sb.append(// w w w. ja  v a  2 s  .  c o  m
            "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>");
    for (Option option : new CLIManagerExtension().getOptions()) {
        a = !a;
        sb.append("<tr class='").append(a ? 'a' : 'b').append("'><td><code>-<a name='");
        sb.append(option.getOpt());
        sb.append("'>");
        sb.append(option.getOpt());
        sb.append("</a>,--<a name='");
        sb.append(option.getLongOpt());
        sb.append("'>");
        sb.append(option.getLongOpt());
        sb.append("</a>");
        if (option.hasArg()) {
            if (option.hasArgName()) {
                sb.append(" &lt;").append(option.getArgName()).append("&gt;");
            } else {
                sb.append(' ');
            }
        }
        sb.append("</code></td><td>");
        sb.append(option.getDescription());
        sb.append("</td></tr>");
        sb.append(LS);
    }
    sb.append("</table>");
    return sb.toString();
}

From source file:org.apache.maven.index.cli.NexusIndexerCliTest.java

public String getOptionsAsHtml() {
    @SuppressWarnings("unchecked")
    List<Option> optList = new ArrayList<Option>(new NexusIndexerCli().buildDefaultCliOptions().getOptions());
    Collections.sort(optList, new OptionComparator());

    StringBuilder sb = new StringBuilder();
    boolean a = true;
    sb.append(/*  w  ww.  j  ava 2 s .co m*/
            "<table border='1' class='zebra-striped'><tr class='a'><th><b>Options</b></th><th><b>Description</b></th></tr>");
    for (Option option : optList) {
        a = !a;
        sb.append("<tr class='").append(a ? 'a' : 'b').append("'><td><code>-<a name='");
        sb.append(option.getOpt());
        sb.append("'>");
        sb.append(option.getOpt());
        sb.append("</a>,--<a name='");
        sb.append(option.getLongOpt());
        sb.append("'>");
        sb.append(option.getLongOpt());
        sb.append("</a>");
        if (option.hasArg()) {
            if (option.hasArgName()) {
                sb.append(" &lt;").append(option.getArgName()).append("&gt;");
            } else {
                sb.append(' ');
            }
        }
        sb.append("</code></td><td>");
        sb.append(option.getDescription());
        sb.append("</td></tr>");
        sb.append(LS);
    }
    sb.append("</table>");
    return sb.toString();
}

From source file:org.apache.nifi.toolkit.cli.impl.command.AbstractPropertyCommand.java

@Override
public final R execute(final CommandLine commandLine) throws CommandException {
    try {/* w  ww .j a  va 2s.co m*/
        final Properties properties = new Properties();

        // start by loading the properties file if it was specified
        if (commandLine.hasOption(CommandOption.PROPERTIES.getLongName())) {
            final String propertiesFile = commandLine.getOptionValue(CommandOption.PROPERTIES.getLongName());
            if (!StringUtils.isBlank(propertiesFile)) {
                try (final InputStream in = new FileInputStream(propertiesFile)) {
                    properties.load(in);
                }
            }
        } else {
            // no properties file was specified so see if there is anything in the session
            final SessionVariable sessionVariable = getPropertiesSessionVariable();
            if (sessionVariable != null) {
                final Session session = getContext().getSession();
                final String sessionPropsFiles = session.get(sessionVariable.getVariableName());
                if (!StringUtils.isBlank(sessionPropsFiles)) {
                    try (final InputStream in = new FileInputStream(sessionPropsFiles)) {
                        properties.load(in);
                    }
                }
            }
        }

        // add in anything specified on command line, and override anything that was already there
        for (final Option option : commandLine.getOptions()) {
            final String optValue = option.getValue() == null ? "" : option.getValue();
            properties.setProperty(option.getLongOpt(), optValue);
        }

        // delegate to sub-classes
        return doExecute(properties);

    } catch (CommandException ce) {
        throw ce;
    } catch (Exception e) {
        throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
    }
}

From source file:org.apache.phoenix.mapreduce.index.IndexScrutinyTool.java

private void requireOption(CommandLine cmdLine, Option option) {
    if (!cmdLine.hasOption(option.getOpt())) {
        throw new IllegalStateException(option.getLongOpt() + " is a mandatory parameter");
    }/*  ww w. ja  v  a2  s . co m*/
}

From source file:org.apache.shiro.tools.hasher.Hasher.java

private static String createMutexMessage(Option... options) {
    StringBuilder sb = new StringBuilder();
    sb.append("The ");

    for (int i = 0; i < options.length; i++) {
        if (i > 0) {
            sb.append(", ");
        }/*from w  ww  .  ja  v a  2 s .co m*/
        Option o = options[0];
        sb.append("-").append(o.getOpt()).append("/--").append(o.getLongOpt());
    }
    sb.append(" and generated salt options are mutually exclusive.  Only one of them may be used at a time");
    return sb.toString();
}

From source file:org.apache.shiro.tools.hasher.Hasher.java

private static int getRequiredPositiveInt(CommandLine line, Option option) {
    String iterVal = line.getOptionValue(option.getOpt());
    try {//from   w w w .ja va2s.c  o  m
        return Integer.parseInt(iterVal);
    } catch (NumberFormatException e) {
        String msg = "'" + option.getLongOpt() + "' value must be a positive integer.";
        throw new IllegalArgumentException(msg, e);
    }
}

From source file:org.apache.stratos.adc.mgt.cli.completer.CommandCompleter.java

public CommandCompleter(Map<String, Command<StratosCommandContext>> commands) {
    if (logger.isDebugEnabled()) {
        logger.debug("Creating auto complete for {} commands", commands.size());
    }//ww w  .j  ava  2 s  .co  m
    argumentMap = new HashMap<String, Collection<String>>();
    defaultCommandCompleter = new StringsCompleter(commands.keySet());
    helpCommandCompleter = new ArgumentCompleter(new StringsCompleter(CliConstants.HELP_ACTION),
            defaultCommandCompleter);
    for (String action : commands.keySet()) {
        Command<StratosCommandContext> command = commands.get(action);
        Options commandOptions = command.getOptions();
        if (commandOptions != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Creating argument completer for command: {}", action);
            }
            List<String> arguments = new ArrayList<String>();
            Collection<?> allOptions = commandOptions.getOptions();
            for (Object o : allOptions) {
                Option option = (Option) o;
                String longOpt = option.getLongOpt();
                String opt = option.getOpt();
                if (StringUtils.isNotBlank(longOpt)) {
                    arguments.add("--" + longOpt);
                } else if (StringUtils.isNotBlank(opt)) {
                    arguments.add("-" + opt);
                }
            }

            argumentMap.put(action, arguments);
        }
    }
}

From source file:org.apache.tomcat.vault.VaultTool.java

public VaultTool(String[] args) {
    initOptions();// w w w  .j a  v  a  2  s .c  o m
    parser = new PosixParser();
    try {
        cmdLine = parser.parse(options, args, true);
    } catch (MissingArgumentException e) {
        Option opt = e.getOption();
        for (String s : args) {
            String optionSpecified = s.replaceAll("^-+", "");
            if (optionSpecified.equals(opt.getLongOpt()) || optionSpecified.equals(opt.getOpt())) {
                System.err.println("Missing argument for option: " + optionSpecified);
                System.exit(2);
            }
        }
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        System.exit(2);
    }
}

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 v  a 2  s. c o  m
 *            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);
    }
}