在args4j中我定义了这样的选项:
@Option(name="-host",usage="host to connect")
@Option(name="-port",usage="port of the host")
@Option(name="-idle",usage="idle")
Run Code Online (Sandbox Code Playgroud)
但是,当显示帮助时,args4j始终按字母顺序使用,以便打印
-host - host to connect
-idle - idle
-port - port to connect
Run Code Online (Sandbox Code Playgroud)
这不方便,因为我想首先显示强制选项.另外我想自己设置选项的顺序,因为一些选项(如主机和端口)应该一起使用.
如何控制args4j中的选项顺序?
我在3年前发现了同样的问题,但没有回答http://markmail.org/message/xce6vitw6miywtos
小智 6
您可以通过ParserProperties设置排序,然后在CmdLineParser构造函数中使用它.如果将OptionSorter设置为null,则将保留选项顺序:
ParserProperties properties = ParserProperties.defaults();
properties.withOptionSorter(null);
CmdLineParser parser = new CmdLineParser(YOUR_OPTIONS_CLASS, properties);
Run Code Online (Sandbox Code Playgroud)
所以在问题的例子中你会得到:
-host - host to connect
-port - port to connect
-idle - idle
Run Code Online (Sandbox Code Playgroud)
您不能使用当前的 Args4j(至少据我所知) - 但由于它是开源的,我鼓励您自己实现它并尝试在新版本的源代码中获取补丁。
从来源org.kohsuke.args4j.CmdLineParser::
// for display purposes, we like the arguments in argument order, but the options in alphabetical order
Collections.sort(options, new Comparator<OptionHandler>() {
public int compare(OptionHandler o1, OptionHandler o2) {
return o1.option.toString().compareTo(o2.option.toString());
}
});
Run Code Online (Sandbox Code Playgroud)