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
Posix Parser
Apache Commons CLI - Posix Parser
A Posix parser is use to parse Posix pke arguments passed. It is now deprecated and is replaced by DefaultParser.
Example
CLITester.java
import org.apache.commons.cp.CommandLine; import org.apache.commons.cp.CommandLineParser; import org.apache.commons.cp.Options; import org.apache.commons.cp.ParseException; import org.apache.commons.cp.PosixParser; pubpc class CLITester { pubpc static void main(String[] args) throws ParseException { //Create posix pke options Options posixOptions = new Options(); posixOptions.addOption("D", false, "Display"); posixOptions.addOption("A", false, "Act"); CommandLineParser posixParser = new PosixParser(); CommandLine cmd = posixParser.parse(posixOptions, args); if( cmd.hasOption("D") ) { System.out.println("D option was used."); } if( cmd.hasOption("A") ) { System.out.println("A option was used."); } } }
Output
Run the file while passing -D -A as options and see the result.
java CLITester -D -A D option was used. A option was used.
Run the file while passing --D as option and see the result.
java CLITester --D D option was used.Advertisements