
- Apache Commons CLI Tutorial
- Home
- Overview
- Environment Setup
- First Application
- Option Properties
- Boolean Option
- Argument Option
- Properties Option
- Posix Parser
- GNU Parser
- Usage Example
- Help Example
- Apache Commons CLI Resources
- Quick Guide
- Useful Resources
- Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Apache Commons CLI - Help Example
Apache Commons CLI provides HelpFormatter class to print the help related to command line arguments. See the example.
Example
CLITester.java
import java.io.PrintWriter; 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(); 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