Contact at mumbai.academics@gmail.com or 8097636691
Responsive Ads Here

Sunday, 1 July 2018

Apache Commons CLI - Usage Example

Apache Commons CLI - Usage Example


Apache Commons CLI provides HelpFormatter class to print the usage guide of command line arguments. See the example.

Example

CLITester.java
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class CLITester {
   public 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 Application")
         .addOption("n", true, "No. of copies to print");

      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("CLITester", options);
   } 
}

Output

Run the file and see the result.
java CLITester
usage: CLITester
 -g,--gui     Show GUI Application
 -n <arg>     No. of copies to print
 -p,--print   Send print request to printer.

No comments:

Post a Comment