You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Veit Guna <Ve...@gmx.de> on 2016/09/06 08:09:17 UTC

[cli] optional args not working as expected

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


Re: [cli] optional args not working as expected

Posted by Benedikt Ritter <br...@apache.org>.
Thank you! If you want to speed up things, how about provided in patch for
this issue?

Regards,
Benedikt

Veit Guna <ve...@gmx.de> schrieb am Di., 6. Sep. 2016 um 22:35 Uhr:

> Hi Benedikt.
>
> Thanks for your feedback! Looks there's already one:
>
> https://issues.apache.org/jira/browse/CLI-265
>
> joined it and linked this thread.
>
> Thanks
> Veit
>
> Am 06.09.2016 um 19:56 schrieb Benedikt Ritter:
> > Hello Veit,
> >
> > it looks to me, like this is a bug in 1.3.1 Could you please file a bug
> in
> > our Issue tracker [1] and provide your code sample there?
> >
> > Thank you!
> > Benedikt
> >
> > [1] https://issues.apache.org/jira/browse/CLI
> >
> > Veit Guna <Ve...@gmx.de> schrieb am Di., 6. Sep. 2016 um 10:09 Uhr:
> >
> >> 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
> >>
> >>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [cli] optional args not working as expected

Posted by Veit Guna <ve...@gmx.de>.
Hi Benedikt.

Thanks for your feedback! Looks there's already one:

https://issues.apache.org/jira/browse/CLI-265

joined it and linked this thread.

Thanks
Veit

Am 06.09.2016 um 19:56 schrieb Benedikt Ritter:
> Hello Veit,
>
> it looks to me, like this is a bug in 1.3.1 Could you please file a bug in
> our Issue tracker [1] and provide your code sample there?
>
> Thank you!
> Benedikt
>
> [1] https://issues.apache.org/jira/browse/CLI
>
> Veit Guna <Ve...@gmx.de> schrieb am Di., 6. Sep. 2016 um 10:09 Uhr:
>
>> 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
>>
>>


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


Re: [cli] optional args not working as expected

Posted by Benedikt Ritter <br...@apache.org>.
Hello Veit,

it looks to me, like this is a bug in 1.3.1 Could you please file a bug in
our Issue tracker [1] and provide your code sample there?

Thank you!
Benedikt

[1] https://issues.apache.org/jira/browse/CLI

Veit Guna <Ve...@gmx.de> schrieb am Di., 6. Sep. 2016 um 10:09 Uhr:

> 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
>
>