commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles Sadowski <>
Subject Re: [CLI] Command-line arguments and option with mulitple arguments
Date Fri, 03 Jun 2011 08:47:27 GMT
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
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
is more flexible but, being is somewhat unusual, I hadn't expected this

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,

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message