You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Emmanuel Bourg <eb...@apache.org> on 2011/06/03 01:07:21 UTC

Re: [CLI] Command-line arguments and option with mulitple arguments

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


Re: [CLI] Command-line arguments and option with mulitple arguments

Posted by Gilles Sadowski <gi...@harfang.homelinux.org>.
Hi Emmanuel.

Thanks for your reply.

> 
> >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");

I see, now; however, I think that I hadn't tried that variant because it
seems counter-intuitive that you could get multiple values from an Option
declaring a single argument. [This is in fact the same reasoning that you
give in your other mail concerning my proposal to "auto-split" a single
argument.]
Isn't the most common behaviour for command-line parsing tools to forget all
arguments but the last in such cases? I agree that having both
  getOptionValue
  getOptionValues
is more flexible but, being is somewhat unusual, I hadn't expected this
behaviour...

The additional behaviour I'm proposing makes it easier to the users of the
application in case there are many arguments, as he could write:
  java MypApp --foo 'a b c d e f g h i j k l m n o p q r s t u v w x y z'
instead of a much longer line with '--foo' repeated for each argument.

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

You are right. But it is just another level of flexibility which the user
is not forced to use, especially in this case. In my case, the 'a', 'b', 'c'
etc. are a series of flags.


Best regards,
Gilles

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