{"id":1334,"date":"2016-06-04T08:34:53","date_gmt":"2016-06-04T08:34:53","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1334"},"modified":"2021-09-20T13:50:04","modified_gmt":"2021-09-20T13:50:04","slug":"what-is-commons-cli-library","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/what-is-commons-cli-library\/","title":{"rendered":"What is Commons CLI library?"},"content":{"rendered":"<h3>Overview<\/h3>\n<p>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.<\/p>\n<p>More information about the library, please visit : <a href=\"https:\/\/commons.apache.org\/proper\/commons-cli\/index.html\" target=\"_blank\" rel=\"noopener\">https:\/\/commons.apache.org\/proper\/commons-cli\/index.html<\/a><\/p>\n<h3><strong>Maven dependency:<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;!\u2013 https:\/\/mvnrepository.com\/artifact\/commons-cli\/commons-cli \u2013&gt;\r\n\r\n&lt;dependency&gt;\r\n&lt;groupId&gt;commons-cli&lt;\/groupId&gt;\r\n&lt;artifactId&gt;commons-cli&lt;\/artifactId&gt;\r\n&lt;version&gt;1.4&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>For other build tools :<\/strong><\/p>\n<p><a href=\"https:\/\/commons.apache.org\/proper\/commons-cli\/dependency-info.html\" target=\"_blank\" rel=\"noopener\">https:\/\/commons.apache.org\/proper\/commons-cli\/dependency-info.html<\/a><\/p>\n<p>Let\u2019s 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.<\/p>\n<h4><strong>Sample Program Code<\/strong><\/h4>\n<p><strong>CommandLine<\/strong> class represents list of arguments parsed against Options descriptor.<\/p>\n<p><strong>Options<\/strong> class represents a collection of objects, which describe the possible options for a command-line.<\/p>\n<p><strong>CommandLineParser<\/strong> interface: A class that implements this parser interface can parse a String array according to the Options specified and return a command line object.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class CommandLineExample {\r\n\r\nprivate static Options options = null;\r\n\r\nprivate static final String envOption = \"e\";\r\n private static final String testOption = \"t\";\r\n private static final String helpOption = \"h\";\r\n private CommandLine cmd = null; \r\n public static String envType ;\r\n public static String testType ;\r\n\r\nstatic{\r\n options = new Options();\r\n options.addOption(envOption, true, \r\n \"QA or STAGE or PROD\");\r\n options.addOption(testOption, true, \"BVT or Regression\");\r\n options.addOption(helpOption, false,\"help option\");\r\n\r\n}\r\n\r\n\r\n\r\npublic static void main(String[] args) {\r\n CommandLineExample cliProg = new CommandLineExample();\r\n cliProg.loadArgs(args);\r\n }\r\n\r\n\r\n\r\nprivate void loadArgs(String[] args){\r\n \/\/CommandLineParser parser = new PosixParser();\r\n CommandLineParser parser = new DefaultParser();\r\n try {\r\n cmd = parser.parse(options, args);\r\n } catch (ParseException e) {\r\n System.err.println(\"Error parsing arguments\");\r\n e.printStackTrace();\r\n System.exit(1);\r\n }\r\n\r\n\/\/ Check for mandatory arguments\r\n\r\nif (!cmd.hasOption(envOption)){\r\n HelpFormatter formatter = new HelpFormatter();\r\n formatter.printHelp(\"java -jar SampleAutomationFramework.jar\", options);\r\n System.exit(1);\r\n }\r\n if (!cmd.hasOption(testOption)){\r\n HelpFormatter formatter = new HelpFormatter();\r\n formatter.printHelp(\"java -jar SampleAutomationFramework.jar\", options);\r\n System.exit(1);\r\n }\r\n if ( cmd.hasOption(helpOption)){\r\n HelpFormatter formatter = new HelpFormatter();\r\n formatter.printHelp(\"java -jar SampleAutomationFramework.jar\", options);\r\n System.exit(1);\r\n }\r\n\r\n\r\n\r\n\/\/ Look for optional arguments.\r\n if (cmd.hasOption(envOption)){\r\n envType = cmd.getOptionValue(envOption);\r\n System.out.println(envType);}\r\n\r\nif (cmd.hasOption(testOption)){\r\n testType = cmd.getOptionValue(testOption);\r\n System.out.println(testType);}\r\n }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong>Running the program:<\/strong><\/h4>\n<p>\/&gt;java -jar SampleAutomationFramework.jar -h<br \/>\nusage: java -jar SampleAutomationFramework.jar<br \/>\n-e &lt;arg&gt; QA or STAGE or PROD<br \/>\n-h help option<br \/>\n-t &lt;arg&gt; BVT or Regression<\/p>\n<p>\/&gt;java -jar SampleAutomationFramework.jar -e QA<br \/>\nusage: java -jar SampleAutomationFramework.jar<br \/>\n-e &lt;arg&gt; QA or STAGE or PROD<br \/>\n-h help option<br \/>\n-t &lt;arg&gt; BVT or Regression<\/p>\n<p>\/&gt;<br \/>\nC:\\EclipseWorkspace&gt;java -jar SampleAutomationFramework.jar -e QA -t BVT<br \/>\nQA<br \/>\nBVT<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-620\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/CommandLine-Arguments.jpeg\" alt=\"Commons CLI library\" width=\"1364\" height=\"704\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<p><strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\n<div id=\"themify_builder_content-619\" class=\"themify_builder_content themify_builder_content-619 themify_builder\" data-postid=\"619\"><\/div>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1334\" data-postid=\"1334\" class=\"themify_builder_content themify_builder_content-1334 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>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: &lt;!\u2013 https:\/\/mvnrepository.com\/artifact\/commons-cli\/commons-cli \u2013&gt; &lt;dependency&gt; &lt;groupId&gt;commons-cli&lt;\/groupId&gt; &lt;artifactId&gt;commons-cli&lt;\/artifactId&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1334","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1334"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1334\/revisions"}],"predecessor-version":[{"id":21038,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1334\/revisions\/21038"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}