
- 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 - Posix Parser
A Posix parser is use to parse Posix like arguments passed. It is now deprecated and is replaced by DefaultParser.
Example
CLITester.java
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; public class CLITester { public static void main(String[] args) throws ParseException { //Create posix like 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