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.
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.
[java]@ServiceProvider(service = OptionProcessor.class)
public class MyProcessor extends OptionProcessor
private Option option1 = Option.requiredArgument(
protected Set getOptions()
Set set = new HashSet();
protected void process(Env env, Map optionValues)
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:
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.
[text]Unrecognized option –test-userdir-lock-with-invalid-arg[/text]
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:
[java]public class NewProcessor implements ArgsProcessor
@Arg(longName = “argumentName”)
@Description(displayName = “–argumentName NAME”,
shortDescription = “just any old argument”)
public String theOption;
public void process(Env env) throws CommandException
if (theOption != null)
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!