Friday 16 December 2016

Commons CLI: OptionGroup: group mutually Exclusive options

By using OptionGroup class, we can group mutually exclusive options.

How to define OptionGroup?
OptionGroup primaryGroup = new OptionGroup();

How to add options to OptionGroup?
By calling the addOption method you can add options to given OptionGroup.
primaryGroup.addOption(input);
primaryGroup.addOption(output);

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.OptionGroup;
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");

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

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

  OptionGroup primaryGroup = new OptionGroup();
  primaryGroup.addOption(input);
  primaryGroup.addOption(output);
  primaryGroup.setRequired(true);

  OptionGroup verboseGroup = new OptionGroup();
  verboseGroup.addOption(quiet);
  verboseGroup.addOption(verbose);
  verboseGroup.addOption(debug);

  OptionGroup helpGroup = new OptionGroup();
  helpGroup.addOption(help);
  helpGroup.addOption(version);

  options.addOptionGroup(verboseGroup);
  options.addOptionGroup(helpGroup);
  options.addOptionGroup(primaryGroup);

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




Previous                                                 Next                                                 Home

No comments:

Post a Comment