Hi Simon,
As Gilles pointed out, using several options N1, N2... is not very
optimal. You can achieve the same result with only one option. Here is
an example:
Options options = new Options();
options.addOption(OptionBuilder.withValueSeparator(':')
.hasArgs(2)
.create("N"));
CommandLine cmd = new DefaultParser().parse(options, new String[]
{ "-N", "localhost:8000",
"-N", "localhost:8001",
"-N", "localhost:8002"});
String[] values = cmd.getOptionValues("N");
for (int i = 0; i < values.length; i = i + 2) {
String hostname = values[i];
int port = Integer.parseInt(values[i+1]);
...
}
Emmanuel Bourg
Le 24/05/2011 15:08, Simon Courtenage a écrit :
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<
[email protected]> 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: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]