Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How can I read apache.commons.cli.CommandLine code options into an array

802252Jul 2 2009 — edited Jul 4 2009
I have a code with hundreds of options using CommandLine, Options, OptionsBuilder and GnuParser. I am trying to use the code with a swing based application. So instead of the commandLine the user will be entering the inputs at the GUI level. The code below explains. If I try to create my own array which will store the user's input, instead of using the array in main which is
public static void main (String[]args)
, I get a NullPointerException and points me to the following line of code:
CommandLine cl = new GnuParser().parse(opts, args);
. Now my question is, if you have code is CommandLine options, how can you store the options into an array? The code below explains:
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class Main {

    /**
     * @param args the command line arguments
     */

    private static CommandLine parse(String[] args) {
        Options opts = new Options();
        OptionBuilder.withArgName("aet[@host][:port]");
        OptionBuilder.hasArg();
        OptionBuilder.withDescription(
                "set AET, local address and listening port of local Application Entity");
        opts.addOption(OptionBuilder.create("L"));

        
        OptionGroup qrlevel = new OptionGroup();

        OptionBuilder.withDescription("perform patient level query, multiple "
                + "exclusive with -S and -I, perform study level query\n"
                + "                            by default.");
        OptionBuilder.withLongOpt("patient");
        opts.addOption(OptionBuilder.create("P"));

        OptionBuilder.withDescription("perform series level query, multiple "
                + "exclusive with -P and -I, perform study level query\n"
                + "                            by default.");
        
        CommandLine cl = null;
        try {
            cl = new GnuParser().parse(opts, args);
        } catch (ParseException e) {
            e.getMessage();
            throw new RuntimeException("unreachable");
        }

        return cl;
    }


    public static void main(String[] args) {

        String[]argv = new String[20];

        argv[0] = "DCM4CHEE@localhost:11112";

        //CommandLine cl = parse(args); //This works, but input can only be read from commandLine
        CommandLine cl = parse(argv);  // This gives a NullPointerException
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 1 2009
Added on Jul 2 2009
2 comments
1,042 views