commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ed Young <>
Subject commons.cli and repeating option values
Date Tue, 17 Feb 2009 20:55:21 GMT
What is the best way to support a command line option that occurs multiple
times in a single command line invocation?

(I did some web searching with no ideal solution to this, but I appologize
if this is a recurring issue on this list. I just subscribed...)

For example, if I want to set the debug level for several different classes
like this:
myProgram -d com.mycompany.etrace.filters.udp.UdpPacketDistributor=TRACE
-d com.mycompany.etrace.filters.udp.SomOtherClass=DEBUG

I have used the OptionBuilder, but it doesn't seem to work the way I expect:

CommandLineParser parser = new BasicParser();
        Options options = new Options();

Option debug = OptionBuilder.withArgName("className=level")
                        .withDescription("debug level")

Then when I parse the command line:

if (commandLine.hasOption('d'))
            String[] debugOptArray = commandLine.getOptionValues('d');
            for (String str: debugOptArray)
                LOGGER.debug("str: " + str);

I get
2009-02-17 13:28:33,985 DEBUG - str:
2009-02-17 13:28:34,782 DEBUG - str: TRACE
2009-02-17 13:28:36,095 DEBUG - str:
2009-02-17 13:28:37,454 DEBUG - str: DEBUG
2009-02-17 13:28:39,876 DEBUG - debug string:

It's not deterministic as far as where in the debugOptArray the className is
and the level are, respectively.

Is there a way to extract all the "className" elements and the associated
"level" elements. A sort of Map interface?


- Ed

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message