Apache Commons CLI - ordering help options?

岁酱吖の 提交于 2019-12-23 07:39:18

问题


I'm using the Apache Commons CLI. By default it orders help options on the command line alphabetically by key. So, what appears is:

-csv
-ip
-msisdn
-xml

But I want to order them as follows:

-csv
-xml
-ip
-msisdn

I know that there is a OptionFormatter class you can use and pass to the HelpFormatter, but can't see any examples on how to use it for my purposes above (http://www.marko.homeunix.org/programming/java/commons-cli/api/org/apache/commons/cli/HelpFormatter.OptionComparator.html).

Just wondering has anyone done anything similar?

Thanks


回答1:


Currently it is not supported. But it is an open source so, you know what to do...

From the source code:

    private static class OptionComparator
    implements Comparator {

    /**
     * <p>Compares its two arguments for order. Returns a negative 
     * integer, zero, or a positive integer as the first argument 
     * is less than, equal to, or greater than the second.</p>
     *
     * @param o1 The first Option to be compared.
     * @param o2 The second Option to be compared.
     *
     * @return a negative integer, zero, or a positive integer as 
     * the first argument is less than, equal to, or greater than the 
     * second.
     */
    public int compare(Object o1, Object o2)
    {
        Option opt1 = (Option)o1;
        Option opt2 = (Option)o2;

        return opt1.getKey().compareToIgnoreCase(opt2.getKey());
    }
}

You can override the default comparator and define the order you want.




回答2:


Since v1.3 you can call setOptionComparator(null), this way the formatter will skip sorting, and the arguments will be printed in the same order they were added.

HelpFormatter formatter = new HelpFormatter();
formatter.setOptionComparator(null);

Link to the actual issue.




回答3:


And the best KISS-way to implement such comparator is:

class OptionComparator<T extends Option> implements Comparator<T> {

    private static final String OPTS_ORDER = "abcdef"; // short option names

    public int compare(T o1, T o2) {
        return OPTS_ORDER.indexOf(o1.getOpt()) - OPTS_ORDER.indexOf(o2.getOpt());
    }
}

 




回答4:


As of Apache Commons CLI 1.2, you can set the comparator directly on the HelpFormatter class:

setOptionComparator [link]

public void setOptionComparator(Comparator comparator)

Set the comparator used to sort the options when they output in help text. Passing in a null parameter will set the ordering to the default mode.

You should supply your own implementation of Comparator<Option> that sorts your options in the order you desire.




回答5:


If you know exact order of the options, you can extend Option class to include your ordering number and provider your OrderedOption instances to the Options instance using Options.add(Option opt) method.

Then, create a comparator and compare order numbers in your OrderedOptions... I would recommend not to mix both types of Option instances in one Options instance as it can complicate ordering and also testing for the valid OrderedOption instance in Comparator.



来源:https://stackoverflow.com/questions/11741625/apache-commons-cli-ordering-help-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!