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:com.spectralogic.ds3cli.Arguments.java

public static boolean matchesOption(final Option opt, final String token) {
    return token.equals('-' + opt.getOpt()) || token.equals("--" + opt.getLongOpt());
}

From source file:eu.project.ttc.tools.cli.TermSuiteCLIUtils.java

/**
 * Displays all command line options in log messages.
 * @param line/*from  ww  w  . jav  a 2s  .  c om*/
 */
public static void logCommandLineOptions(CommandLine line) {
    for (Option myOption : line.getOptions()) {
        String message;
        String opt = "";
        if (myOption.getOpt() != null) {
            opt += "-" + myOption.getOpt();
            if (myOption.getLongOpt() != null)
                opt += " (--" + myOption.getLongOpt() + ")";
        } else
            opt += "--" + myOption.getLongOpt() + "";

        if (myOption.hasArg())
            message = opt + " " + myOption.getValue();
        else
            message = opt;

        LOGGER.info("with option: " + message);
    }
}

From source file:lcmc.LCMC.java

/** Parse cluster options and create cluster button. */
private static void parseClusterOptions(final CommandLine cmd) throws ParseException {
    String clusterName = null;//w  ww  .j a  va  2 s  .  com
    List<HostOptions> hostsOptions = null;
    final Map<String, List<HostOptions>> clusters = new LinkedHashMap<String, List<HostOptions>>();
    for (final Option option : cmd.getOptions()) {
        final String op = option.getLongOpt();
        if (CLUSTER_OP.equals(op)) {
            clusterName = option.getValue();
            if (clusterName == null) {
                throw new ParseException("could not parse " + CLUSTER_OP + " option");

            }
            clusters.put(clusterName, new ArrayList<HostOptions>());
        } else if (HOST_OP.equals(op)) {
            final String[] hostNames = option.getValues();
            if (clusterName == null) {
                clusterName = "default";
                clusters.put(clusterName, new ArrayList<HostOptions>());
            }
            if (hostNames == null) {
                throw new ParseException("could not parse " + HOST_OP + " option");
            }
            hostsOptions = new ArrayList<HostOptions>();
            for (final String hostNameEntered : hostNames) {
                String hostName;
                String port = null;
                if (hostNameEntered.indexOf(':') > 0) {
                    final String[] he = hostNameEntered.split(":");
                    hostName = he[0];
                    port = he[1];
                    if ("".equals(port) || !Tools.isNumber(port)) {
                        throw new ParseException("could not parse " + HOST_OP + " option");
                    }
                } else {
                    hostName = hostNameEntered;
                }
                final HostOptions ho = new HostOptions(hostName);
                if (port != null) {
                    ho.setPort(port);
                }
                hostsOptions.add(ho);
                clusters.get(clusterName).add(ho);
            }
        } else if (SUDO_OP.equals(op)) {
            if (hostsOptions == null) {
                throw new ParseException(SUDO_OP + " must be defined after " + HOST_OP);
            }
            for (final HostOptions ho : hostsOptions) {
                ho.setSudo(true);
            }
        } else if (USER_OP.equals(op)) {
            if (hostsOptions == null) {
                throw new ParseException(USER_OP + " must be defined after " + HOST_OP);
            }
            final String userName = option.getValue();
            if (userName == null) {
                throw new ParseException("could not parse " + USER_OP + " option");
            }
            for (final HostOptions ho : hostsOptions) {
                ho.setUser(userName);
            }
        } else if (PORT_OP.equals(op)) {
            if (hostsOptions == null) {
                throw new ParseException(PORT_OP + " must be defined after " + HOST_OP);
            }
            final String port = option.getValue();
            if (port == null) {
                throw new ParseException("could not parse " + PORT_OP + " option");
            }
            for (final HostOptions ho : hostsOptions) {
                ho.setPort(port);
            }
        }
    }
    for (final String cn : clusters.keySet()) {
        for (final HostOptions hostOptions : clusters.get(cn)) {
            if (hostsOptions.size() < 1
                    || (hostsOptions.size() == 1 && !Tools.getConfigData().isOneHostCluster())) {
                throw new ParseException("not enough hosts for cluster: " + cn);
            }
        }
    }
    final String failedHost = Tools.setUserConfigFromOptions(clusters);
    if (failedHost != null) {
        Tools.appWarning("could not resolve host \"" + failedHost + "\" skipping");
    }
}

From source file:com.aliyun.odps.ship.common.OptionsBuilder.java

private static void processOptions(CommandLine line) {
    // set context value from options
    Option[] ops = line.getOptions();
    for (Option op : ops) {
        String v = removeQuote(op.getValue());
        if (Constants.FIELD_DELIMITER.equals(op.getLongOpt())
                || Constants.RECORD_DELIMITER.equals(op.getLongOpt())) {
            setContextValue(op.getLongOpt(), processDelimiter(v));
        } else {//from ww w  .  ja  v  a 2  s. c om
            setContextValue(op.getLongOpt(), v);
        }
    }
}

From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCli.java

private static String parse(CommandLine cmd, Option option) {
    String value = cmd.getOptionValue(option.getLongOpt());
    if (value != null && value.isEmpty()) {
        value = null;/*from  w  ww .  j a v a 2  s.  co  m*/
    }
    if (value == null && option.isRequired()) {
        throw new IllegalArgumentException(
                MessageFormat.format("required argument was not set: --{0}", option.getLongOpt()));
    }
    LOG.debug("--{}: {}", option.getLongOpt(), value); //$NON-NLS-1$
    return value;
}

From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCli.java

private static Map<String, String> parseProperties(CommandLine cmd, Option option) {
    Properties properties = cmd.getOptionProperties(option.getLongOpt());
    Map<String, String> results = new TreeMap<>();
    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
        results.put((String) entry.getKey(), (String) entry.getValue());
    }//from  ww w .  ja  v a2s  .co m
    return results;
}

From source file:com.basistech.lucene.tools.LuceneQueryTool.java

private static void validateOptions(Options options, String[] args)
        throws org.apache.commons.cli.ParseException {
    Set<String> optionNames = Sets.newHashSet();

    // non-generic forced by commons.cli api
    for (Object o : options.getOptions()) {
        Option option = (Option) o;
        optionNames.add(option.getLongOpt());
        String shortOpt = option.getOpt();
        if (shortOpt != null) {
            optionNames.add(shortOpt);/*from   w w w.  ja  v  a  2s.  c om*/
        }
    }
    for (String arg : args) {
        if (arg.startsWith("-")) {
            String argName = arg.replaceFirst("-+", "");
            if (!optionNames.contains(argName)) {
                throw new org.apache.commons.cli.ParseException("Unrecognized option: " + arg);
            }
        }
    }
}

From source file:com.databasepreservation.cli.CLI.java

private static boolean isImportModuleOption(Option option) {
    final String type = "i";
    if (StringUtils.isNotBlank(option.getOpt())) {
        return option.getOpt().startsWith(type);
    } else if (StringUtils.isNotBlank(option.getLongOpt())) {
        return option.getLongOpt().startsWith(type);
    }/*  w  w  w  .  j  av a 2  s. c  o m*/
    return false;
}

From source file:com.databasepreservation.cli.CLI.java

private static boolean isExportModuleOption(Option option) {
    final String type = "e";
    if (StringUtils.isNotBlank(option.getOpt())) {
        return option.getOpt().startsWith(type);
    } else if (StringUtils.isNotBlank(option.getLongOpt())) {
        return option.getLongOpt().startsWith(type);
    }// w ww .  jav a 2s  .co  m
    return false;
}

From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCli.java

private static File resolveFile(Option output, String value, boolean check) {
    File result = new File(value);
    if (check && result.exists() == false) {
        throw new IllegalArgumentException(
                MessageFormat.format("missing file: {1} (--{0})", output.getLongOpt(), result));
    }//from  w w  w  . ja v a  2 s  . c  o  m
    return result;
}