Friday, 16 December 2016

Commons CLI: Boolean options

Boolean options don’t take any arguments.

For example, open command prompt and type ‘javac’, you can able to see following options.
C:\>javac
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath <path>          Specify where to find user class files and annotation processors
  -cp <path>                 Specify where to find user class files and annotation processors
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
  -processorpath <path>      Specify where to find annotation processors
  -parameters                Generate metadata for reflection on method parameters
  -d <directory>             Specify where to place generated class files
  -s <directory>             Specify where to place generated source files
  -h <directory>             Specify where to place generated native header files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version
  -profile <profile>         Check that API used is available in the specified profile
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system
  -Werror                    Terminate compilation if warnings occur
  @<filename>                Read options and filenames from file

In the above example,
g, nowarn, verbose, deprecation, parameters, version, help, X, Werror are the boolean options.

For example, you can define boolean options like below.

Option help = new Option("help", "print this message");
Option version = new Option("version", "print the version information and exit");
Option quiet = new Option("quiet", "be extra quiet");
Option verbose = new Option("verbose", "be extra verbose");
Option debug = new Option("debug", "print debugging information");

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

 public static void main(String args[]) {

  Options options = new Options();

  Option input = new Option("i", "input", true, "input file to read data from");
  input.setRequired(true);
  input.setArgName("FILE PATH");
  options.addOption(input);

  Option output = new Option("o", "output", true, "output file to write the final result");
  output.setRequired(true);
  output.setArgName("FILE PATH");
  options.addOption(output);

  Option help = new Option("help", "print this message");
  Option version = new Option("version", "print the version information and exit");
  Option quiet = new Option("quiet", "be extra quiet");
  Option verbose = new Option("verbose", "be extra verbose");
  Option debug = new Option("debug", "print debugging information");

  options.addOption(help);
  options.addOption(version);
  options.addOption(quiet);
  options.addOption(verbose);
  options.addOption(debug);

  Option language = new Option("lang", "language", true, "Encoding Langauge");
  language.setArgName("LANGUAGE");
  options.addOption(language);

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

 }
}

Output
usage: FileCopy -i <FILE PATH> -o <FILE PATH> [-help] [-version] [-quiet] [-verbose] [-debug] [-lang
       <LANGUAGE>]
     -i,--input <FILE PATH>          input file to read data from
     -o,--output <FILE PATH>         output file to write the final result
     -help                           print this message
     -version                        print the version information and exit
     -quiet                          be extra quiet
     -verbose                        be extra verbose
     -debug                          print debugging information
     -lang,--language <LANGUAGE>     Encoding Langauge




Previous                                                 Next                                                 Home

No comments:

Post a Comment