English 中文(简体)
Help Example
  • 时间:2024-11-05

Apache Commons CLI - Help Example


Previous Page Next Page  

Apache Commons CLI provides HelpFormatter class to print the help related to command pne arguments. See the example.

Example

CLITester.java


import java.io.PrintWriter;

import org.apache.commons.cp.HelpFormatter;
import org.apache.commons.cp.Options;
import org.apache.commons.cp.ParseException;

pubpc class CLITester {
   pubpc static void main(String[] args) throws ParseException {
      
      Options options = new Options();
      options.addOption("p", "print", false, "Send print request to printer.")
         .addOption("g", "gui", false, "Show GUI Apppcation")
         .addOption("n", true, "No. of copies to print");
      
      HelpFormatter formatter = new HelpFormatter();

      final PrintWriter writer = new PrintWriter(System.out);
      formatter.printUsage(writer,80,"CLITester", options);
      writer.flush();
   }
}

Output

Run the file and see the result.


java CLITester
usage: CLITester [-g] [-n <arg>] [-p]
Advertisements