NetBeans Platform Cookbook Chapter 10 01

Parsing Command Line Args

An application can receive run parameters from the command line. NetBeans Platform has its own module for it - Command Line Parsing API. We will show basic usage of it.


The Command Line Parsing API infrastructure parses command parameters, then notifies each registered OptionProcessor instance giving the results. Each module can register the OptionProcessor subclass as a service provider. Registering and using of service provider is described in the chapter 3 Lookup. The OptionProcessor implementation has two methods: getOptions() returns Set<Option> instance. The infrastructure uses this information to map parameter and values and check the consistency. process(Env env, Map<Option,String[]> optionValues) is called by parsing API infrastructure to allow module process parameters values. If processing fails throw the CommandException. Options are represented by the Option class. It describes short name (one letter char), long name of the parameter, display name, description and if it has to have any value. The option can have none, one optional, one required or more arguments (values). The Option class provides factory methods to create instances. Options can be grouped into compound option with relationship. The OptionGroups class provides factory methods to create group option: someOf(), anyOf(), oneOf() and allOf().

How to

public class CommandArguments extends OptionProcessor {
    /** if -p option without any argument is present */
    protected static final String DEFAULT_PROPFILE_NAME 
             = "";

    /** -i inpfile.txt or --input inpfile.txt , requires argument */ 
    private Option input    = Option.requiredArgument('i', "input"); 
    private Option output   = Option.requiredArgument('o', "output");
    private Option log      = Option.requiredArgument('l', "log");
    /** -m <mode> or --mode <mode> where mode is 
     * one of AppContext.getModes().S
     * Values: basic, medium, advanced, expert.
    private Option mode     = Option.requiredArgument('m', "mode");
    /** -p or -p <file> or --propfile or --propfile <file>   */
    private Option propfile = Option.optionalArgument('p', "propfile");
    private Option optui = Option.withoutArgument('u', "ui");
    private Option oo = Option.defaultArguments();
    protected Set<Option> getOptions() {
        Set<Option> options = new HashSet<Option>(20);
        Option.shortDescription(mode, BUNDLE, "option.mode.description") ;
        Option.shortDescription(propfile, BUNDLE,
        // must be all together
        Option iolm = OptionGroups.allOf( input, output, log, mode     );
        // must be (i, o, l, m) or (p)
        options.add( OptionGroups.oneOf( iolm, propfile ) );
        options.add( optui );
        return options;

    protected void process(Env env, Map<Option, String[]> optionValues) 
                                     throws CommandException {
        AppContext context = AppContext.getInstance();
        env.getOutputStream().println("print to env.out ");
       "Env.curdir " + env.getCurrentDirectory() );
        CommandLine.getDefault().usage( pr );   // print usage 
        if (optionValues.containsKey( propfile )) {
            context.setPropertyFile( optionValues.get(propfile)[0] );
        if (optionValues.containsKey( input )) {
            context.setInputFile( optionValues.get(input)[0] );
        if (optionValues.containsKey( output )) {
            context.setOutputFile( optionValues.get(output)[0] );
        if (optionValues.containsKey( log )) {
            context.setLogFile( optionValues.get(log)[0] );
        if (optionValues.containsKey( mode     )) {
            try {
            } catch (IllegalArgumentException ie) {
                // if you want
                throw new CommandException(0, ie.getMessage() );

Define your class as a subclass of the OptionProcessor. The annotation line registers this class as provider of the OptionProcessor service. First define options, group them if you need it and add them into Set. The process() method checks for each option if it is present and handle it.

Notes/Tips - documentation

Documentation links:

Option class

OptionProcessor class

Documentation of the Command Line Parsing API is on

Notes/Tips - examples

You find sources for view and copy in 10_best / command_line directory.

Text and sources were created under NB 6.8 and will be upgraded.


Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo