Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 26

Download

HowToParseTheCommandLine

A new features since NetBeans Platform 6 is the ability to parse the command line.

So, let’s try to parse the command line to automatically open a document when the NetBeans Platform starts up. The example below uses the Sketsa SVG Editor as an example:

  • Create an object that inherits from OptionProcessor:

          public class SketsaOptionProcessor extends OptionProcessor {
    
              private Option openOption = Option.defaultArguments();
              private Option openOption2 = Option.additionalArguments(
                                                          'o', "open");
    
              @Override
              public Set getOptions() {
                  HashSet set = new HashSet();
                  set.add(openOption);
                  set.add(openOption2);
                  return set;
              }
    
              @Override
              public void process(Env env, Map values)
                                  throws CommandException {
                  List<string> filenameList = new ArrayList<string>();
                  Object obj = values.get(openOption);
                  if (obj != null) {
                      filenameList.addAll(Arrays.asList((String[]) obj));
                  }
                  obj = values.get(openOption2);
                  if (obj != null) {
                      filenameList.addAll(Arrays.asList((String[]) obj));
                  }
    
                  for (int i = 0; i &lt; filenameList.size(); i++) {
                      File file = new File(filenameList.get(i));
                      if (!file.isAbsolute()) {
                          file = new File(env.getCurrentDirectory(),
                               filenameList.get(i));
                      }
    
                      //System.out.println(file.toString());
    
                      try {
                          DataObject data = DataObject.find(
                              FileUtil.toFileObject(file));
                          OpenCookie cookie = data.getCookie(OpenCookie.class);
                          cookie.open();
                      } catch (OutOfMemoryError ex) {
                          String msg = Application.getMessage(
                                 "MSG_OutOfMemoryError.Text");
                          NotifyDescriptor nd = new NotifyDescriptor.Message(
                                  msg,
                                  NotifyDescriptor.ERROR_MESSAGE);
                          DialogDisplayer.getDefault().notify(nd);
                      } catch (Exception ex) {
                          NotifyDescriptor nd = new NotifyDescriptor.Message(
                                  ex.getMessage(),
                                  NotifyDescriptor.ERROR_MESSAGE);
                          DialogDisplayer.getDefault().notify(nd);
                      }
                  }
              }

    The important parts here are the two overidden methods "getOptions()" and "process(Env,Map)". The getOptions indicate which command line you want to capture, in this case we want to capture defaultArgument and -open some_file or -o some_file. So the following will work:

      sketsa artwork.svg
      sketsa --open artwork.svg
      sketsa -o artwork.svg

+ * Since the OptionProcessor is a service or lookup, so we need to register it somewhere. We register it in META-INF services by adding following annotation to the processor class implementation:

+

@ServiceProvider(service=OptionProcessor.class)
public class SketsaOptionProcessor extends OptionProcessor {
// trimmed
}

+ This indicates generates appropriate META-INF/services/org.netbeans.spi.sendopts.OptionProcessor file during compilation.

+ * Compile and build the project. Now the NetBeans Platform application is able to parse command line arguments.