You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Remko Popma (Jira)" <ji...@apache.org> on 2020/04/29 06:57:01 UTC

[jira] [Resolved] (GROOVY-9519) CLIBuilder: Option with "type: Integer, defaultValue '0'" results in Boolean if default value is applied

     [ https://issues.apache.org/jira/browse/GROOVY-9519?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Remko Popma resolved GROOVY-9519.
---------------------------------
    Fix Version/s: 3.0.4
       Resolution: Fixed

Fixed in master and the 3.0.x branch.

> CLIBuilder: Option with "type: Integer, defaultValue '0'" results in Boolean if default value is applied
> --------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-9519
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9519
>             Project: Groovy
>          Issue Type: Bug
>          Components: command line processing
>    Affects Versions: 3.0.2
>         Environment: WIndows/Linux
>            Reporter: Dirk Heinrichs
>            Assignee: Remko Popma
>            Priority: Major
>             Fix For: 3.0.4
>
>
> When using an option with {{type: Integer}} and {{defaultValue: '0'}} the resulting variable is of type Boolean if the option is not specified on the command line. Here's a little Groovy program to demonstrate the issue:
> {code:java}
> @Grab('info.picocli:picocli-groovy:4.2.0')
> @GrabConfig(systemClassLoader=true)
> import groovy.cli.picocli.CliBuilder
> def cli = new CliBuilder(name: 'cliTest.groovy')
> cli.h(type: Boolean, longOpt: 'help', usageHelp: true, required: false, 'Show usage information')
> cli.i(type: Integer, longOpt: 'intTest', required: false, args: 1, defaultValue: '0', 'Testing integer with default value 0')
> def opts = cli.parse(args)
> opts || System.exit(1)
> if(opts.h) {
>   cli.usage()
>   System.exit(0)
> }
> println(opts.i.getClass())
> println(opts.i)
> // Need to explicitely convert if default value applies
> def i = (opts.i instanceof Boolean ? 0 : opts.i)
> println(i.getClass())
> println(i){code}
> Executing this w/o providing {{-i}} prints:
> {code}% ~/tmp/groovy-3.0.2/bin/groovy ./cliTest.groovy 
> class java.lang.Boolean
> false
> class java.lang.Integer
> 0{code}
> But if {{-i}} IS provided (even with the same value as the default), it correctly prints:
> {code}% ~/tmp/groovy-3.0.2/bin/groovy ./cliTest.groovy -i 0
> class java.lang.Integer
> 0
> class java.lang.Integer
> 0{code}
> I would expect the type of {{opts.i}} to be Integer in both cases.
> NOTE: I've opened the same for PicoCLI, since I wasn't sure where it belongs.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)