You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by William Speirs <ws...@apache.org> on 2013/01/18 17:14:58 UTC
[CLI] OptionGroup Broken?
I'm trying to use OptionGroup to mutually exclude arguments, and it seems
to be broken. I've setup --ip-address & --hostname as options in a single
group, then provide both on the "command line" and would expect the parser
to throw an AlreadySelectedException, but it doesn't... it happily parses
both. Should I raise a JIRA issue for this, or am I totally missing
something?
Bill-
public static void main(String[] args) {
fakeMain(new String[] { "--ip-address", "192.168.1.1",
"--hostname", "www.example.com"});
}
public static void fakeMain(String[] args) {
final PosixParser parser = new PosixParser();
final Options options = configureOptions();
// parse with the basic parser
CommandLine cmdLine = null;
try {
cmdLine = parser.parse(options, args);
} catch(ParseException e) {
e.printStackTrace();
return;
}
// get the IP address or hostname
if(cmdLine.hasOption("ip-address")) {
System.out.println("IP: " +
cmdLine.getOptionValue("ip-address"));
}
if(cmdLine.hasOption("hostname")){
System.out.println("Hostname: " +
cmdLine.getOptionValue("hostname"));
}
}
@SuppressWarnings("static-access")
public static Options configureOptions() {
Options options = new Options();
// create the group to specify either an IP or hostname
OptionGroup group = new OptionGroup();
group.addOption(OptionBuilder.withLongOpt("ip-address")
.withArgName("ip")
.hasArg()
.withDescription("IP address to use")
.create());
group.addOption(OptionBuilder.withLongOpt("hostname")
.withArgName("host")
.hasArg()
.withDescription("Hostname address to
use")
.create());
options.addOptionGroup(group);
return options;
}
*** OUTPUT ***
IP: 192.168.1.1
Hostname: www.example.com
Re: [CLI] OptionGroup Broken?
Posted by Thomas Neidhart <th...@gmail.com>.
On 01/18/2013 05:14 PM, William Speirs wrote:
> I'm trying to use OptionGroup to mutually exclude arguments, and it seems
> to be broken. I've setup --ip-address & --hostname as options in a single
> group, then provide both on the "command line" and would expect the parser
> to throw an AlreadySelectedException, but it doesn't... it happily parses
> both. Should I raise a JIRA issue for this, or am I totally missing
> something?
Hi Bill,
just tested it with the latest trunk and I receive the following exception:
org.apache.commons.cli.AlreadySelectedException: The option 'hostname'
was specified but an option from this group has already been selected:
'ip-address'
So it seems to be already fixed, although I do not know when the next
release will be available.
Thomas
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org