Feb 282012
 

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

When using the NetBeans Platform 7.1, the way to access command line parameters is by extending the class OptionProcessor.

@ServiceProvider(service = OptionProcessor.class)
public class MyProcessor extends OptionProcessor
{
    private Option option1 = Option.requiredArgument(
            'a', "argumentName");

    @Override
    protected Set getOptions()
    {
        Set set = new HashSet();
        set.add(option1);
        return set;
    }

    @Override
    protected void process(Env env, Map optionValues)
            throws CommandException
    {
        if (optionValues.containsKey(option1))
        {
            System.out.println(optionValues.get(option1)[0]);
        }
    }
}

Note: Module dependencies are required on Command Line Parsing API and Lookup API.

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 javadoc.

Testing the Code

This can be tested when running the suite (or standalone module) from the IDE. Just add the new parameter to the project.properties file:

run.args.extra=--argumentName=theName

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.

Unrecognized option --test-userdir-lock-with-invalid-arg

NetBeans Platform 7.2 Code – Enters Annotations

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 @Arg annotation for specifying a command line parameter. Here is an example in the IDE source code: GroupOptionProcessor from the Project UI module.

So now it is possible to declare the same thing as follows:

public class NewProcessor implements ArgsProcessor
{
    @Arg(longName = "argumentName")
    @Description(displayName = "--argumentName NAME",
    shortDescription = "just any old argument")
    public String theOption;

    @Override
    public void process(Env env) throws CommandException
    {
        if (theOption != null)
        {
            System.out.println(theOption);
        }
    }
}

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 bug report and consider voting for it. :)

Update: 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!

  3 Responses to “Parsing Command Line Parameters”

  1. How does the Apache CLI compare to this method?

    • Hi Pete,

      I have not used Apache Commons CLI before, so I am not claiming to be anywhere near an expert on the topic. :) However, I did have a quick look at the usage docs from the Commons CLI website. The NetBeans Platform library and Commons CLI accomplish essentially the same function, but in quite different ways. The big difference is that the NetBeans Platform library allows you to extend the Platform by adding a class or two that gets loaded by the Platform at runtime. (You do not have your own main method in a Platform application.) With the Commons CLI out of the box, no extendable platform is involved at all. You just do the parsing in the main method of your application.

  2. You shouldn’t access System.out, System.in and System.err inside the process method. That’s why you get passed an Env object.

    So instead of

    System.out.println(….)

    do:

    env.getOutputStream().println(….)

    You might want to correct that.

    Besides that, I have to say that I constantly rediscover your excellent blog posts, this one included.

 Leave a Reply

(required)

(required)


9 + two =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>