Apache Commons CLI Tutorial
Selected Reading
- Discussion
- Useful Resources
- Quick Guide
- Help Example
- Usage Example
- GNU Parser
- Posix Parser
- Properties Option
- Argument Option
- Boolean Option
- Option Properties
- First Application
- Environment Setup
- Overview
- Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Usage Example
Apache Commons CLI - Usage Example
Apache Commons CLI provides HelpFormatter class to print the usage guide of command pne arguments. See the example given below −
Example
CLITester.java
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(); formatter.printHelp("CLITester", options); } }
Output
Run the file and see the result.
java CLITester usage: CLITester -g,--gui Show GUI Apppcation -n <arg> No. of copies to print -p,--print Send print request to printer.Advertisements