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

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

Introduction

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

Prototype

public String getOpt() 

Source Link

Document

Retrieve the name of this Option.

Usage

From source file:wqm.CmdLine.java

public static String get(CommandLine cLine, Option option) {
    return cLine.getOptionValue(option.getOpt());
}

From source file:wqm.CmdLine.java

public static int getInt(CommandLine cLine, Option option) {
    return Integer.parseInt(cLine.getOptionValue(option.getOpt()));
}

From source file:wvw.utils.cmd.CmdUtils.java

public CmdUtils(String execName, CmdOptionListener listener, CmdOption... optionsArray) {

    this.execName = execName;
    this.listener = listener;

    for (CmdOption cmdOption : optionsArray)
        this.cmdOptions.add(cmdOption);

    formatter.setOptionComparator(new Comparator<Option>() {

        public int compare(Option o1, Option o2) {
            int idx = cmdOptions.indexOf(find(o1.getOpt()));
            int idx2 = cmdOptions.indexOf(find(o2.getOpt()));

            if (idx > idx2)
                return 1;
            else if (idx < idx2)
                return -1;
            else/*  w  ww  .  jav a 2s  .c om*/
                return 0;
        }
    });

    options = new Options();

    for (CmdOption cmdOption : optionsArray)
        options.addOption(cmdOption.getName(), cmdOption.hasArg(), cmdOption.getDescription());
}

From source file:wvw.utils.cmd.CmdUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean process(String[] args) {
    if (ArrayUtils.contains(args, "-help")) {
        help();/*www.j a  v  a2 s . c  om*/

        return false;
    }

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = null;

    try {
        cmd = parser.parse(options, args);

    } catch (ParseException e) {
        error(e);

        return false;
    }

    Option[] options = cmd.getOptions();
    for (Option option : options) {

        final String name = option.getOpt();
        CmdOption cmdOption = find(name);

        Object value = option.getValue();
        if (cmdOption.hasArg()) {

            if (value == null) {
                error("require value for option \"" + name + "\"");

                return false;
            }

            if (cmdOption.hasArgClz()) {

                if (cmdOption.getArgClz().equals(Integer.class)) {
                    try {
                        value = Integer.parseInt((String) value);

                    } catch (NumberFormatException e) {
                        error(e);

                        return false;
                    }

                } else if (cmdOption.getArgClz().equals(Double.class)) {
                    try {
                        value = Double.parseDouble((String) value);

                    } catch (NumberFormatException e) {
                        error(e);

                        return false;
                    }

                } else if (cmdOption.getArgClz().equals(String.class)) {

                } else {
                    error("unsupported argument class: " + cmdOption.getArgClzName());

                    return false;
                }
            }

            if (cmdOption.getOptions() != null) {

                if (!ArrayUtils.contains(cmdOption.getOptions(), value)) {
                    error("unsupported argument \"" + value + "\"");

                    return false;
                }
            }
        }

        if (!listener.cmdOptionValue(name, value))
            return false;
    }

    this.processedOptions = new ArrayList(Arrays.asList(options));

    if (!doPostCheck(options))
        return false;

    return true;
}

From source file:wvw.utils.cmd.CmdUtils.java

private boolean inArray(Option[] options, String name, String value) {
    for (Option option : options)
        if (option.getOpt().equals(name)
                && (value == null || (option.getValue() != null && option.getValue().equals(value))))

            return true;

    return false;
}

From source file:wvw.utils.cmd.CmdUtils.java

public String toString() {
    StringBuffer ret = new StringBuffer();
    ret.append("- parameters:\n");

    int cnt = 0;//from  www  .ja v a2  s.c  om
    for (Object obj : processedOptions) {

        if (cnt++ > 0)
            ret.append("\n");

        if (obj instanceof Option) {
            Option option = (Option) obj;

            if (option.hasArg())
                ret.append(option.getOpt() + " : " + option.getValue());
            else
                ret.append(option.getOpt());

        } else if (obj instanceof CmdOptionVal) {
            CmdOptionVal option = (CmdOptionVal) obj;

            if (option.getOption().hasArg())
                ret.append(option.getName() + " : " + option.getValue());
            else
                ret.append(option.getName());
        }
    }

    ret.append("\n");

    return ret.toString();
}