3

I am using Common-CLI api to display help to the command. Following function displays help of the command.

public static void printHelp(final Options options, final int width, final String cmdLineSyntax,
        final String header, final String footer, final int leftPad, final int descPad, final boolean autoUsage,
        final OutputStream out) {
    PrintWriter writer = new PrintWriter(out);
    final HelpFormatter helpFormatter = new HelpFormatter();
    helpFormatter.printHelp(writer, width, cmdLineSyntax, header, options, leftPad, descPad, footer, autoUsage);
    writer.flush();
}

I added options in the following order.

Option option1 = Option.builder("A").longOpt("almost-all").desc("do not list implied . and ..").hasArg(false)
                .build();

        Option option2 = Option.builder("b").longOpt("block-size").argName("SIZE> <CAPACITY> <LINE").numberOfArgs(3)
                .desc("use SIZE-byte blocks").hasArg(true).build();

        Option option3 = Option.builder("c")
                .desc("with -lt: sort by, and show, ctime (time of last modification of file status information) with -l:show ctime and sort by name otherwise: sort by ctime")
                .hasArg(false).build();

        Options options = new Options();
        options.addOption(option1);
        options.addOption(option2);
        options.addOption(Option.builder().longOpt("escape").desc("print octal escapes for nongraphic characters")
                .hasArg(false).build());

        options.addOption(option3);

When I call the printHelp function on options object, I am getting following output.

usage: ls [-A] [-b <SIZE> <CAPACITY> <LINE>] [-c] [--escape]
     -A,--almost-all                              do not list implied . and ..
     -b,--block-size <SIZE> <CAPACITY> <LINE>     use SIZE-byte blocks
     -c                                           with -lt: sort by, and show, ctime (time of last
                                                  modification of file status information) with
                                                  -l:show ctime and sort by name otherwise: sort by
                                                  ctime
        --escape                                  print octal escapes for nongraphic characters

But I am expecting in following way.

usage: ls [-A] [-b <SIZE> <CAPACITY> <LINE>] [-c] [--escape]
     -A,--almost-all                              do not list implied . and ..
     -b,--block-size <SIZE> <CAPACITY> <LINE>     use SIZE-byte blocks
        --escape                                  print octal escapes for nongraphic characters
     -c                                           with -lt: sort by, and show, ctime (time of last
                                                  modification of file status information) with
                                                  -l:show ctime and sort by name otherwise: sort by
                                                  ctime

Can any one tell me, how can I get the expected output?

4
  • I am not familiar with "apache-commons-cli" but it seems the order of creating the options is relevant for the print order. So you have to create the last option one step above. Commented Dec 15, 2016 at 13:24
  • I am adding the options in the order to options object. Commented Dec 15, 2016 at 13:28
  • Yes, but you are creating them in another order than you are putting it into the options object. The answer explains this obviously. Commented Dec 15, 2016 at 13:56
  • Thanks for the response. The order of creation of option objects doesn't matter here. Commented Dec 15, 2016 at 14:10

1 Answer 1

3

I believe this is what you are looking for.

public void setOptionComparator(Comparator comparator) Set the comparator used to sort the options when they output in help text. Passing in a null comparator will keep the options in the order they were declared. Parameters: comparator - the Comparator to use for sorting the options

Ref: https://commons.apache.org/proper/commons-cli/javadocs/api-release/org/apache/commons/cli/HelpFormatter.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.