{"id":1212,"date":"2013-06-26T01:17:16","date_gmt":"2013-06-26T01:17:16","guid":{"rendered":"http:\/\/garysieling.com\/blog\/?p=1212"},"modified":"2013-06-26T01:17:16","modified_gmt":"2013-06-26T01:17:16","slug":"processing-command-line-arguments-in-java","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/processing-command-line-arguments-in-java\/","title":{"rendered":"Processing Command Line Arguments in Java"},"content":{"rendered":"<p>Rather than parsing command line arguments yourself, Apache has a nice library to do it for you, called <a href=\"http:\/\/commons.apache.org\/proper\/commons-cli\/\">Apache Commons CLI<\/a>. It has a few different options, for various flavors of parsing, although this example demonstrates the two most common use cases (I think) &#8211; a flag, and setting a value.  <\/p>\n<p>The nice thing about this is it warns you if you specify incorrect arguments, so you can avoid worrying about that ever again.<\/p>\n<pre lang=\"Java\">\nimport org.apache.commons.cli.*;\n\npublic class ArtEtl {\n  public static void main(String[] args) throws Exception {\n\n  Options options = new Options();\n\n  options.addOption(\"d\", false, \"Delete records\"); \/\/ does not have a value\n  options.addOption(\"c\", true, \"CSV Repository\"); \/\/ has a value\n\n  CommandLineParser parser = new PosixParser();\n  CommandLine cmd = parser.parse( options, args);\n\n  if (cmd.hasOption(\"d\")) {\n    System.out.println(\"Clearing index\");\n    server.deleteByQuery(\"*:*\");\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rather than parsing command line arguments yourself, Apache has a nice library to do it for you, called Apache Commons CLI. It has a few different options, for various flavors of parsing, although this example demonstrates the two most common use cases (I think) &#8211; a flag, and setting a value. The nice thing about &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/processing-command-line-arguments-in-java\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Processing Command Line Arguments in Java&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[300],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1212"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=1212"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/1212\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}