commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Emmanuel Bourg <ebo...@apache.org>
Subject Re: [CLI] Command-line arguments and option with mulitple arguments
Date Thu, 02 Jun 2011 23:07:21 GMT
Hi Gilles,

> 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.

This is possible by declaring the foo option with exactly one argument. 
Here is an example:

     Options options = new Options();
     options.addOption(OptionBuilder.hasArg().create("foo"));

     CommandLine cmd = new GnuParser().parse(options, new String[] { 
"--foo", "a", "--foo", "b", "--foo", "c", "cmdArg1", "cmdArg2"});

     String[] values = cmd.getOptionValues("foo");


> 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".

By using quotes the value 'a b c' is kept as is, the parser will never 
try to split it. If the argument was a path containing a space you 
certainly wouldn't want to split the value.

Emmanuel Bourg



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message