{"id":219,"date":"2012-02-28T23:49:11","date_gmt":"2012-02-28T21:49:11","guid":{"rendered":"http:\/\/www.pellissier.co.za\/hermien\/?p=219"},"modified":"2012-02-28T23:49:11","modified_gmt":"2012-02-28T21:49:11","slug":"parsing-command-line-parameters","status":"publish","type":"post","link":"https:\/\/www.pellissier.co.za\/hermien\/?p=219","title":{"rendered":"Parsing Command Line Parameters"},"content":{"rendered":"<p>Using the Command Line Parsing API, it is possible to process parameters passed to your NetBeans Platform application via the command line. This article explores how it works.<\/p>\n<p>Read this <a href=\"https:\/\/blogs.oracle.com\/geertjan\/entry\/invoke_an_action_from_the\">excellent blog post<\/a> by Geertjan which provided me with some of the information for this article. Also useful was the <a href=\"http:\/\/bits.netbeans.org\/dev\/javadoc\/org-netbeans-modules-sendopts\/org\/netbeans\/spi\/sendopts\/OptionProcessor.html\">javadoc<\/a>.<\/p>\n<p><strong>NetBeans Platform 7.1 Code<\/strong><\/p>\n<p>When using the NetBeans Platform 7.1, the way to access command line parameters is by extending the class OptionProcessor.<\/p>\n<p>[java]@ServiceProvider(service = OptionProcessor.class)<br \/>\npublic class MyProcessor extends OptionProcessor<br \/>\n{<br \/>\n    private Option option1 = Option.requiredArgument(<br \/>\n            &#8216;a&#8217;, &#8220;argumentName&#8221;);<\/p>\n<p>    @Override<br \/>\n    protected Set getOptions()<br \/>\n    {<br \/>\n        Set set = new HashSet();<br \/>\n        set.add(option1);<br \/>\n        return set;<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    protected void process(Env env, Map optionValues)<br \/>\n            throws CommandException<br \/>\n    {<br \/>\n        if (optionValues.containsKey(option1))<br \/>\n        {<br \/>\n            System.out.println(optionValues.get(option1)[0]);<br \/>\n        }<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>Note: Module dependencies are required on Command Line Parsing API and Lookup API.<\/p>\n<p>The interesting part is the static method Option.requiredArgument() that creates the actual Option instance. Have a look at the full list of methods in the <a href=\"http:\/\/bits.netbeans.org\/dev\/javadoc\/org-netbeans-modules-sendopts\/org\/netbeans\/spi\/sendopts\/Option.html\">javadoc<\/a>.<\/p>\n<p><strong>Testing the Code<\/strong><\/p>\n<p>This can be tested when running the suite (or standalone module) from the IDE. Just add the new parameter to the project.properties file:<\/p>\n<p>[text]run.args.extra=&#8211;argumentName=theName[\/text]<\/p>\n<p>If the lock file exists in the test user directory, you will see a message indicating that there was an unrecognised option. Just delete the lock file or perform a clean and build on the project and run it again.<\/p>\n<p>[text]Unrecognized option &#8211;test-userdir-lock-with-invalid-arg[\/text]<\/p>\n<p><strong>NetBeans Platform 7.2 Code &#8211; Enters Annotations<\/strong><\/p>\n<p>As I was searching for the javadocs, I came across some new classes and annotations that are introduced in the development builds of NetBeans 7.2. There is now a handy <a href=\"http:\/\/bits.netbeans.org\/dev\/javadoc\/org-netbeans-modules-sendopts\/org\/netbeans\/spi\/sendopts\/Arg.html\">@Arg annotation<\/a> for specifying a command line parameter. Here is an example in the IDE source code: <a href=\"http:\/\/hg.netbeans.org\/main\/file\/a4ea1f7d8d68\/projectui\/src\/org\/netbeans\/modules\/project\/ui\/groups\/GroupOptionProcessor.java\">GroupOptionProcessor<\/a> from the Project UI module.<\/p>\n<p>So now it is possible to declare the same thing as follows:<\/p>\n<p>[java]public class NewProcessor implements ArgsProcessor<br \/>\n{<br \/>\n    @Arg(longName = &#8220;argumentName&#8221;)<br \/>\n    @Description(displayName = &#8220;&#8211;argumentName NAME&#8221;,<br \/>\n    shortDescription = &#8220;just any old argument&#8221;)<br \/>\n    public String theOption;<\/p>\n<p>    @Override<br \/>\n    public void process(Env env) throws CommandException<br \/>\n    {<br \/>\n        if (theOption != null)<br \/>\n        {<br \/>\n            System.out.println(theOption);<br \/>\n        }<br \/>\n    }<br \/>\n}[\/java]<\/p>\n<p>As with many of the other annotations, a generated layer file is created at compile time. If you are experiencing trouble with this, have a look at this <a href=\"http:\/\/netbeans.org\/bugzilla\/show_bug.cgi?id=208977\">bug report<\/a> and consider voting for it. \ud83d\ude42<\/p>\n<p><strong>Update:<\/strong> Jaroslav Tulach replied to the the bug report with a very easy work around: add a compile time dependency on the FileSystem API. Thanks Jaroslav!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the Command Line Parsing API, it is possible to process parameters passed to your NetBeans Platform application via the command line. This article explores how it works. Read this excellent blog post by Geertjan which provided me with some of the information for this article. Also useful was the javadoc. NetBeans Platform 7.1 Code &#8230; <a title=\"Parsing Command Line Parameters\" class=\"read-more\" href=\"https:\/\/www.pellissier.co.za\/hermien\/?p=219\" aria-label=\"More on Parsing Command Line Parameters\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[3,5],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1v8WL-3x","_links":{"self":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/219"}],"collection":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=219"}],"version-history":[{"count":0,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pellissier.co.za\/hermien\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}