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 19:23:44 GMT
Hi,

I only offer the code to illustrate how to deal with repeated options.  In
this particular case, the repeated option is -N with
values "1=localhost:8000", "2=localhost:8001" etc (these values relate to
the particular application I'm developing).

Simon

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

> Hi.
>
> > 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.
>
> It makes sense, but it's just a workaround that makes you use "dummy"
> properties (i.e. you don't need things like "1", "2", "3" in the code).
> Moreover, it is error-prone because one could easily and mistakenly write
>
>  -N1=localhost:8000 -N1=localhost:8001 -N3=localhost:8002
>
> which would probably make one of the arguments disappear silently.
>
> I think that the simpler syntax
>
>  -N localhost:8000 -N localhost:8001 -N localhost:8002
>
> should be allowed, and behave as expected, i.e. IMHO one should be able to
> retrieve the String[] array with
>
>  String[] nodes = cli.getOptionValues("N");
>
> If it is not possible, shouldn't it be considered a bug?
>
> Best,
> Gilles
>
> > 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
> > >
> > >
>
> ---------------------------------------------------------------------
> 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