Friday 16 December 2016

Commons CLI: Print command line argument in the order you inserted

By passing null argument to 'setOptionComparator' method of HelpFormatter class, you can print the help of given command in the order they specified.
                                                           
public void setOptionComparator(Comparator<Option> 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.

OptionDemo.java
import java.io.OutputStream;
import java.io.PrintWriter;

import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class OptionDemo {

 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.setOptionComparator(null);
  helpFormatter.printHelp(writer, width, cmdLineSyntax, header, options, leftPad, descPad, footer, autoUsage);
  writer.flush();
 }

 public static void main(String args[]) {

  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);

  printHelp(options, 100, "ls", "", "", 5, 5, true, System.out);

 }
}

Output
usage: ls [-A] [-b <SIZE> <CAPACITY> <LINE>] [--escape] [-c]
     -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

Now comment the line 'helpFormatter.setOptionComparator(null);' and re run the application, you can able to see 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




Previous                                                 Next                                                 Home

No comments:

Post a Comment