Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Commons CLI - ordering help options?

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

like image 733
Rory Avatar asked Jul 31 '12 13:07

Rory


5 Answers

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.

like image 151
Mark Kemel Avatar answered Nov 07 '22 19:11

Mark Kemel


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.

like image 35
Duncan Jones Avatar answered Nov 07 '22 21:11

Duncan Jones


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());
    }
}

 

like image 8
mirlord Avatar answered Nov 07 '22 19:11

mirlord


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.

like image 3
aviad Avatar answered Nov 07 '22 20:11

aviad


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.

like image 1
Miro Hudak Avatar answered Nov 07 '22 21:11

Miro Hudak