You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Ed Young <ej...@summitbid.com> on 2009/02/17 21:55:21 UTC

commons.cli and repeating option values

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")
                        .hasArgs()
                        .withValueSeparator()
                        .withDescription("debug level")
                        .create('d');
        options.addOption(debug);

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 EtraceCommand.java - str:
com.comcast.etrace.filters.udp.UdpPacketDistributor
2009-02-17 13:28:34,782 DEBUG EtraceCommand.java - str: TRACE
2009-02-17 13:28:36,095 DEBUG EtraceCommand.java - str:
com.comcast.etrace.filters.udp.SomOtherClass
2009-02-17 13:28:37,454 DEBUG EtraceCommand.java - str: DEBUG
2009-02-17 13:28:39,876 DEBUG EtraceCommand.java - debug string:
com.comcast.etrace.filters.udp.UdpPacketDistributor

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?

Thanks.

-- 
- Ed

Re: commons.cli and repeating option values

Posted by Russel Winder <ru...@concertant.com>.
Ed,

Can you say which version of Commons CLI you are using.

The problem here is I suspect that you are trying to work in the area
where Commons CLI 1.0 behaves one way, Commons CLI 1.1 behaves a
different way with irritating bugs and Commons CLI 1.2 is the one you
need (but isn't released yet).   

On Tue, 2009-02-17 at 13:55 -0700, Ed Young wrote:
> 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")
>                         .hasArgs()
>                         .withValueSeparator()
>                         .withDescription("debug level")
>                         .create('d');
>         options.addOption(debug);
> 
> 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 EtraceCommand.java - str:
> com.comcast.etrace.filters.udp.UdpPacketDistributor
> 2009-02-17 13:28:34,782 DEBUG EtraceCommand.java - str: TRACE
> 2009-02-17 13:28:36,095 DEBUG EtraceCommand.java - str:
> com.comcast.etrace.filters.udp.SomOtherClass
> 2009-02-17 13:28:37,454 DEBUG EtraceCommand.java - str: DEBUG
> 2009-02-17 13:28:39,876 DEBUG EtraceCommand.java - debug string:
> com.comcast.etrace.filters.udp.UdpPacketDistributor
> 
> 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?
> 
> Thanks.
> 
-- 
Russel.
====================================================
Dr Russel Winder                 Partner

Concertant LLP                   t: +44 20 7585 2200, +44 20 7193 9203
41 Buckmaster Road,              f: +44 8700 516 084
London SW11 1EN, UK.             m: +44 7770 465 077