Download NetBeans!

20070722 Sunday July 22, 2007

Parsing the Command Line to Enable a Menu Item

Let's set things up so that we have an Admin menu item that is always disabled, unless the following bit in bold is present within the command line:

/bin/sh "/home/geertjan/netbeans-6.0m10/bin/netbeans" --name admin

So, if the "name" option is not set to "admin", we will see a greyed out Admin menu item in the Tools menu:

"What?! Does that mean that my application on the NetBeans Platform can somehow parse the command line?!" Yes, that's exactly what that means. "But... wow... isn't that amazing?!" Well, yes, if you think so. :-)

To see how everything fits together, do the following:

  1. Create a 'conditionally enabled' action via the New Action wizard. Put it in the Tools menu (or anywhere else). Finish the wizard and you will have created an implementation of the CookieAction class. Now override the very cool enable method, as follows:

    @Override
    protected boolean enable(Node[] arg0) {
        super.enable(arg0);
        if (getUserName().equals("admin")) {
            return true;
        }
        return false;
    }

    So, only if a method called getUserName returns "admin", will the menu item be enabled. In all other cases, it will be disabled, i.e., greyed out.

  2. Within the CookieAction implementation, add the following:

    private static String name;
    
    static void setName(String string) {
        name = string;
    }
    
    private String getUserName(){
        return name;
    }

  3. Now let's feed the above setName method with the value of the 'name' option, retrieved from the command line. Add a dependency on the (brand! new!) Command Line Parsing API. (Well, it's been around for several months already, something like half a year, but it is for 6.0 only, already available in all milestones, so forget about 5.5 and 5.5.1 already and welcome to the future, also known as "6.0".)

    Create a class called MyOptions and let it extend OptionProcessor. You will find that you now need to implement two methods, called getOptions and process. The names of these methods are very clear. The getOptions method is for... getting the options from the command line. The process method is for... doing something useful with the options that the getOptions method got for you.

    But let's first define a new option. "Oh my god! I can define new command line options? You've got to be kidding me! That is so cool!" Relax, okay. In the greater scheme of things all this is arbitrary. But, nevertheless, here's our new option, neatly declared at the top of the class:

    private static Option name = Option.requiredArgument(Option.NO_SHORT_NAME, "name");

    Here, the option is required. However, it could also be an optional option instead. A variety of alternative methods could be set on the Option object. So, here we have an option called 'name'. And there's an 'Option.NO_SHORT_NAME' constant which, when I consult the javadoc, is a "constant that represents no short name indicator". Hmm. Not much wiser about that one then.

    Next, we implement the two required methods:

    public Set getOptions() {
        return Collections.singleton(name);
    }
    
    public void process(Env env, Map values) throws CommandException {
        String[] args = (String[]) values.get(name );
        if (args.length > 0) {
            StatusDisplayer.getDefault().setStatusText("Hello " + args[0]);
            SomeAction.setName(args[0]);
       }
    }

    So, if getOptions returns an argument, we take the first option and we greet our user in the status bar, plus we pass the name to our CookieAction class. And that's all there is to it, in terms of coding.

  4. Finally, expand the META-INF services node, within the Important Files node. There are two nodes there. One is called "all services". Expand that. Scroll a looong way down, until you get to a node called 'org.netbeans.spi.sendopts.OptionProcessor'. Right-click that node. Choose 'Add New Service'. A dialog box pops up. Now browse to your MyOptions class, i.e., the class that extends OptionProcessor.

  5. Install the module.

  6. Restart the application, after making sure you type the name option on the command line. Try 'admin' as a value and notice that the menu item is enabled. Then type anything else as a value and notice that the menu item is greyed out. Hurray, you've parsed the command line and done something useful with the result.

Further reading:

And now... happy command line parsing!

Jul 22 2007, 03:49:28 PM PDT Permalink