Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

args4j: in "usage" how to sort Options manually?

Tags:

java

In args4j I define options like that:

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

However when help is displayed args4j always uses alphabetically order so it prints

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

This is not convient because I want to display mandatory options first. Also I want to set order of options myself because some options (like host and port) should go together.

How can I control order of options in args4j?

I've found the same question asked 3 years ago but not answered http://markmail.org/message/xce6vitw6miywtos

like image 770
Oleg Vazhnev Avatar asked Nov 10 '11 20:11

Oleg Vazhnev


People also ask

Can V $sort_usage show more than just disk sort?

According to Brian Peasland, v$sort_usage will show more than just disk sorts. The sort segment in v$sort_usage can be temporary tables, open cursors or some temporary LOBs. This script will use v$sort_usage and v$session to show the TEMP tablespace usage for a specific session:

How can I see what session is storing temporary sort segments?

For sorting in the TEMP tablespace, the v$sort_usage view can be queried to see what session is storing temporary segments in the TEMP tablespace: v.* The v$sort_usage will show more than just disk sorts. The sort segment in v$sort_usage can be temporary tables, open cursors or some temporary LOBs.

How do I sort a temporary table in the temp tablespace?

For sorting in the TEMP tablespace, the v$sort_usage view can be queried to see what session is storing temporary segments in the TEMP tablespace: v$sort_usage v; The v$sort_usage will show more than just disk sorts. The sort segment in v$sort_usage can be temporary tables, open cursors or some temporary LOBs.


2 Answers

You can set the sorting via the ParserProperties and then use that in the CmdLineParser constructor. If you set the OptionSorter to null, the Option order will be preserved:

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

So in the question's example you will get:

-host - host to connect
-port - port to connect
-idle - idle
like image 79
Shoon Avatar answered Oct 15 '22 03:10

Shoon


You can't with the current Args4j (at least to my knowledge) - but since it's open source I would encourage you to implement it yourself and try to get the patch in the source for newer releases.

From the 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());
        } 
    });
like image 26
Peter Svensson Avatar answered Oct 15 '22 01:10

Peter Svensson