X

Geertjan's Blog

  • July 22, 2007

Parsing the Command Line to Enable a Menu Item

Geertjan Wielenga
Product Manager
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!

Join the discussion

Comments ( 9 )
  • Sven Reimers Monday, July 23, 2007
    Option.NO_SHORT_NAME is used for gnu style options (-n --name) if you do not want a short name, ok I think you got it.
  • Tom Wheeler Monday, July 23, 2007
    Are you going to be covering more about testing platform applications in your blog?
  • Geertjan Monday, July 23, 2007
    Thanks Sven. Tom, yes, this week and in the coming weeks, as I learn more about it, definitely.
  • Hermann Matthes Thursday, December 6, 2007

    Hello Geertjan,

    what do I have to do in a NB5.5.1 project to parse command line options?

    Currently I cannot move to 6.0

    Any hint is welcome

    Thanks

    Hermann


  • Geertjan Thursday, December 6, 2007

    Then you can't parse command line options, because this API is part of 6.0.


  • Laurentiu Trica Wednesday, February 27, 2008

    I tried your example with another purpose, disabling certain functionality for some user type at login.

    I work with Netbeans 5.5.1 but couldn't get this done. Is there any version limitation for this?

    The problem is that I override the method but it's never called.

    I am puzzled regarding the Conditionally Enabled. Which should I choose from Project, Open Cookie, Edit Cookie, Editor Cookie, Data Object - Project doesn't work as it needs the Project class and the wizzard is adding the Project API dependency but the Project class seems not to be in there.

    Please help me get this done.

    Thanks in advance,

    Laurentiu Trica


  • Geertjan Wednesday, February 27, 2008

    This is a new API in 6.0. You cannot do this in 5.5.1. Please don't even try.


  • Kevin Koch Wednesday, March 14, 2012

    Have NetBeans updates made these instructions erroneous?

    With NB 7.1, running the Action wizard produces a class implementing ActionListener, not CookieAction. So the instructions veer off into the woods at step 1 -- enable(Node[]) is not a method defined in the interface.

    What is the right thing to do in NB 7.1?

    Thanks!


  • Geertjan Wednesday, March 14, 2012

    Rewrite the ActionListener to implement CookieAction and remove the annotations, then register the CookieAction into the layer.xml manually.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.