Netbeans Platform parsing command line arguments
August 1st, 2007 | by Tonny Kohar |New features in Netbeans Platform 6 is the ability to parsing command line, so let’s try to parse command line to automatically open document on startup. This example below are using Sketsa SVG Editor as an example
- First create an object that inherit from OptionProcessor
public class SketsaOptionProcessor extends OptionProcessor { private Option openOption = Option.defaultArguments(); private Option openOption2 = Option.additionalArguments( 'o', "open"); public Set getOptions() { HashSet set = new HashSet(); set.add(openOption); set.add(openOption2); return set; } 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 < 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 here is the 2 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 works
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
- create META-INF/services folder in the source folder. - put the following file with a name org.netbeans.spi.sendopts.OptionProcessor - inside that file put the text kiyut.sketsa.SketsaOptionProcessor on the first line.
This indicate that org.netbeans.spi.sendopts.OptionProcessor service is implemented in kiyut.sketsa.SketsaOptionProcessor
- compile and build the project. Now the NB Platform based application able to parse command line argument
Note: this command line argument processing need Netbeans 6
Tags: Branding, Java, Netbeans Platform

You must be logged in to post a comment.