Apache Commons CLI - ordering help options?

I am using CLI Apache Commons. By default, he orders help options on the command line alphabetically using the key. So what appears:

-csv -ip -msisdn -xml 

But I want to order them as follows:

 -csv -xml -ip -msisdn 

I know that there is an OptionFormatter class that you can use and pass to HelpFormatter, but you cannot see any examples of 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, did anyone do something like this?

thanks

+6
source share
5 answers

It is currently not supported. But this is 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 determine the desired order.

+3
source

And the best KISS way to implement such a 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()); } } 

+8
source

As in Apache Commons CLI 1.2, you can install the comparator directly in the HelpFormatter class:

setOptionComparator link ]

public void setOptionComparator(Comparator comparator)

Set the comparator used to sort the parameters when they are displayed in the help text. Passing in the null parameter sets the order to default mode.

You must provide your own implementation of Comparator<Option> , which sorts your parameters in the desired order.

+7
source

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

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

Link to the current issue.

+6
source

If you know the exact order of the parameters, you can extend the Option class by specifying the order number and provider of the OrderedOption instances in the Options instance using the Options.add (Option opt) method.

Then create a comparator and compare the order numbers in your OrderedOptions ... I would recommend not to mix both types of Option instances in one Option instance, as this can make it harder to order as well as testing a valid OrderedOption instance in Comparator.

+1
source

Source: https://habr.com/ru/post/921770/


All Articles