commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Courtenage <courten...@gmail.com>
Subject Re: [CLI] Command-line arguments and option with mulitple arguments
Date Tue, 24 May 2011 13:08:54 GMT
Hi Gilles,

I had a similar problem, where I wanted a series of command-line arguments
to be like
-N1=localhost:8000 -N2=localhost:8001 -N3=localhost:8002 etc.

The code below allowed me to get all the -N args values as an enumeration,
and is based on some code I found in the documentation.
Hope it makes sense and is useful.

Regards

-- Simon

Option node =
OptionBuilder.withArgName("property=value").hasArgs(2).withValueSeparator()
                .withDescription("brokerid=address").create("N");
        options.addOption(node);
        CommandLineParser parser = new GnuParser();
        CommandLine cli = parser.parse(options,args);
        Properties props = cli.getOptionProperties("N");
        for (Enumeration keys = props.keys();keys.hasMoreElements();) {
            String key = (String) keys.nextElement();
            String[] address = props.getProperty(key).split(":");
            NodeInfo n = new
NodeInfo(Integer.parseInt(key),address[0],Integer.parseInt(address[1]));
            cfg.addNode(n);
        }

On Tue, May 24, 2011 at 2:13 PM, Gilles Sadowski <
gilles@harfang.homelinux.org> wrote:

> Hello.
>
> [With official release 1.2]
>
> I'd like to call a commmand "cmd" as follows:
>  $ cmd --foo a --foo b --foo c cmdArg1 cmdArg2
>
> There can be any number of arguments to the option "--foo". When I try,
> the parser ("GnuParser") considers the "cmdArg1" and "cmdArg2" arguments as
> arguments to the "--foo" option.
> This is so even with the "stopAtNonOption" flag set to true.
>
> When I try
>  $ cmd --foo 'a b c' cmdArg1 cmdArg2
> I don't get 3 separate option arguments "a", "b", "c", but a single string
> "a b c".
>
>
> Best regards,
> Gilles
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

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