Apache Commons CLI-订购帮助选项? [英] Apache Commons CLI - ordering help options?
问题描述
我正在使用Apache Commons CLI.默认情况下,它按字母顺序在命令行上按顺序排列帮助选项.因此,出现的是:
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
我知道您可以使用一个OptionFormatter类并将其传递给HelpFormatter,但是上面看不到任何有关如何使用它的示例(http://www.marko.homeunix.org/programming/java/commons-cli/api/org/apache/commons/cli/HelpFormatter.OptionComparator.html).
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?
谢谢
推荐答案
当前不支持. 但这是一个开放源代码,因此,您知道该怎么做...
Currently it is not supported. But it is an open source so, you know what to do...
从源代码开始:
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.
这篇关于Apache Commons CLI-订购帮助选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!