Args4j: in "use" how to sort the parameters manually?

In args4j, I define these parameters:

@Option(name="-host",usage="host to connect") @Option(name="-port",usage="port of the host") @Option(name="-idle",usage="idle") 

However, when help appears, args4j always uses alphabetical order to print

 -host - host to connect -idle - idle -port - port to connect 

This is not convincing because I want to display the required parameters first. I also want to set the order of the parameters myself, because some parameters (for example, the host and port) must go together.

How can I control the order of options in args4j?

I found the same question asked 3 years ago, but did not answer http://markmail.org/message/xce6vitw6miywtos

+6
source share
3 answers

You cannot with the current Args4j (at least as far as I know), but since it is open source, I would recommend that you implement it yourself and try to get a fix in the source for newer versions.

From source: 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()); } }); 
+2
source

You can customize the sort with ParserProperties, and then use it in the CmdLineParser constructor. If you set OptionSorter to null, the order of the options will be preserved:

 ParserProperties properties = ParserProperties.defaults(); properties.withOptionSorter(null); CmdLineParser parser = new CmdLineParser(YOUR_OPTIONS_CLASS, properties); 

So, in the example question you will get:

 -host - host to connect -port - port to connect -idle - idle 
+5
source

This problem is still not resolved after 3 years. But there is a workaround.

Since CmdLineParser uses a list to store options, and the list is displayed by getOptions() , you can get the list and sort it yourself. Since the list is sorted only in the constructor. You can sort it anytime after creating CmdLineParser . Then the output of printUsage() ordered.

This workaround is dependent on the implementation of CmdLineParser , so this is not a good example to follow. But as a workaround, it works, at least in the current version (2.0.28).

0
source

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


All Articles