Overview
The Apache Commons CLI library provides an API for parsing command line options passed to Java programs. It also provides support for options available for a command line tool in different formats like POSIX , GNU etc.
More information about the library, please visit : https://commons.apache.org/proper/commons-cli/index.html
Maven dependency:
<!– https://mvnrepository.com/artifact/commons-cli/commons-cli –> <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.4</version> </dependency>
For other build tools :
https://commons.apache.org/proper/commons-cli/dependency-info.html
Let’s us assume we are developing a sample automation framework which can be run from the command line and accepts command line options for Test Environment and Test type. Test environment could be QA , STAGE or PROD etc. Test Types could be BVT, P1 , Regression etc.
Sample Program Code
CommandLine class represents list of arguments parsed against Options descriptor.
Options class represents a collection of objects, which describe the possible options for a command-line.
CommandLineParser interface: A class that implements this parser interface can parse a String array according to the Options specified and return a command line object.
public class CommandLineExample { private static Options options = null; private static final String envOption = "e"; private static final String testOption = "t"; private static final String helpOption = "h"; private CommandLine cmd = null; public static String envType ; public static String testType ; static{ options = new Options(); options.addOption(envOption, true, "QA or STAGE or PROD"); options.addOption(testOption, true, "BVT or Regression"); options.addOption(helpOption, false,"help option"); } public static void main(String[] args) { CommandLineExample cliProg = new CommandLineExample(); cliProg.loadArgs(args); } private void loadArgs(String[] args){ //CommandLineParser parser = new PosixParser(); CommandLineParser parser = new DefaultParser(); try { cmd = parser.parse(options, args); } catch (ParseException e) { System.err.println("Error parsing arguments"); e.printStackTrace(); System.exit(1); } // Check for mandatory arguments if (!cmd.hasOption(envOption)){ HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar SampleAutomationFramework.jar", options); System.exit(1); } if (!cmd.hasOption(testOption)){ HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar SampleAutomationFramework.jar", options); System.exit(1); } if ( cmd.hasOption(helpOption)){ HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar SampleAutomationFramework.jar", options); System.exit(1); } // Look for optional arguments. if (cmd.hasOption(envOption)){ envType = cmd.getOptionValue(envOption); System.out.println(envType);} if (cmd.hasOption(testOption)){ testType = cmd.getOptionValue(testOption); System.out.println(testType);} } }
Running the program:
/>java -jar SampleAutomationFramework.jar -h
usage: java -jar SampleAutomationFramework.jar
-e <arg> QA or STAGE or PROD
-h help option
-t <arg> BVT or Regression
/>java -jar SampleAutomationFramework.jar -e QA
usage: java -jar SampleAutomationFramework.jar
-e <arg> QA or STAGE or PROD
-h help option
-t <arg> BVT or Regression
/>
C:\EclipseWorkspace>java -jar SampleAutomationFramework.jar -e QA -t BVT
QA
BVT
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :
https://www.oracle.com/in/java/