Friday 16 December 2016

Commons CLI: Option: Specify single command line option

Option class is used to specify single command line option.

You can specify following properties to an Option instance.

a.   Short name of the option
b.   Long name of the option
c.    Description to this option
d.   Flag indicating if an argument is required for this option

Option class provide following constructors to define an instance.

public Option(String opt, String description)
public Option(String opt, boolean hasArg, String description)
public Option(String opt, String longOpt, boolean hasArg, String description)

Each argument is described below
opt: Short representation of the option
longOpt: long representation of the option
description: Describes the function of the option
hasArg: specifies whether the Option takes an argument or not

For example, I am going to build the help to the linux command 'ls'
First we need to define options for the command ls.

Option option1 = new Option("A", "almost-all", false, "do not list implied . and ..");
Option option2 = new Option("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");
Option option3 = new Option("b", "block-size", true, "use SIZE-byte blocks");

Add all the options of ls command to Options class.

Options options = new Options();
options.addOption(option1).addOption(option2).addOption(option3);

Commons-CLI provides ‘HelpFormatter’ class to format the options.

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


Following is the complete working application.

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

 public static void main(String args[]) {

  Option option1 = new Option("A", "almost-all", false, "do not list implied . and ..");
  Option option2 = new Option("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");
  Option option3 = new Option("b", "block-size", true, "use SIZE-byte blocks");

  Options options = new Options();
  options.addOption(option1).addOption(option2).addOption(option3);

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

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

 }
}

Output

usage: ls
     -A,--almost-all           do not list implied . and ..
     -b,--block-size <arg>     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




usage: ls [-A] [-b <arg>] [-c]
     -A,--almost-all           do not list implied . and ..
     -b,--block-size <arg>     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




Previous                                                 Next                                                 Home

No comments:

Post a Comment