commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Veit Guna" <Veit.G...@gmx.de>
Subject [cli] optional args not working as expected
Date Tue, 06 Sep 2016 08:09:17 GMT
Hi.

I'm using 1.3.1 for parsing cmdline parameters. I stumbled across an odd things when working
with optional parameters like in the following
example:

	public static void main(String[] args) throws ParseException {

		Options options = new Options();

		options.addOption(
				Option.builder("ls").longOpt("logSsl").desc("Activates SSL logging (very noisy!)").build());
		options.addOption(
				Option.builder("lr").longOpt("logRequest")
						.desc("Activates request/response logging with a maximum of n bytes per payload (default:
10000)")
						.argName("byte-count").optionalArg(true).numberOfArgs(1).build());

		CommandLineParser parser = new DefaultParser();
		CommandLine line = parser.parse(options, args);
		System.out.println(line.getOptionValue("lr"));
	}

Executing it with "-lr -ls" returns "-ls". If I invoke it with "-lr 5000 -ls" it returns 5000.
So it seems, although I marked it as an optional argument, it just takes the next parameter
"-ls"
as an argument for "-lr" - which I wouldn't expect. It could simply identify whether it is
an argument
by simply checking against the list of valid parameters and just return null or something.

Is this expected behavior? If so, how can I check if an optional argument has been provided
or not?

Thanks.
Veit

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


Mime
View raw message