You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Dirk Heinrichs (Jira)" <ji...@apache.org> on 2020/06/18 14:44:00 UTC

[jira] [Comment Edited] (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:comment-tabpanel&focusedCommentId=17139470#comment-17139470 ] 

Dirk Heinrichs edited comment on GROOVY-9519 at 6/18/20, 2:43 PM:
------------------------------------------------------------------

Hmm, checked again with Groovy 3.0.4. It seems that only the "Integer with defaultValue '0'" case was fixed, but not "String with defaultValue ''". The latter became even worse. It still ignores the defaultValue, but instead of creating a Boolean, it now creates a NullObject. Try this code: {code}@Grab('info.picocli:picocli-groovy:4.3.2')
@GrabConfig(systemClassLoader=true)

import groovy.cli.picocli.CliBuilder

def cli = new CliBuilder(name: 'cliTest.groovy', stopAtNonOption: false)
cli.h(type: Boolean, longOpt: 'help', usageHelp: true, required: false, 'Show usage information')
cli.a(type: String, longOpt: 'optA', required: false, args: 1, defaultValue: '', 'Option a (optional)')
def opts = cli.parse(args)
opts || System.exit(1)
if(opts.h) {
  cli.usage()
  System.exit(0)
}

println(opts.a.getClass())
if (opts.a) {
  println(opts.a)
}
{code}
with Groovy 3.0.2: {code}% ~/tmp/groovy-3.0.2/bin/groovy ./cliTest.groovy
class java.lang.Boolean{code} and with Groovy 3.0.4: {code}% ~/tmp/groovy-3.0.4/bin/groovy ./cliTest.groovy
class org.codehaus.groovy.runtime.NullObject{code}


was (Author: dheinric):
Hmm, checked again with Groovy 3.0.4. It seems that only the "Integer with defaultValue '0'" case was fixed, but not "String with defaultValue ''". The latter became even worse. It still ignores the defaultValue, but instead of creating a Boolean, it now creates a NullObject. Try this code: {code}@Grab('info.picocli:picocli-groovy:4.3.2')
@GrabConfig(systemClassLoader=true)

import groovy.cli.picocli.CliBuilder

def cli = new CliBuilder(name: 'cliTest.groovy', stopAtNonOption: false)
cli.h(type: Boolean, longOpt: 'help', usageHelp: true, required: false, 'Show usage information')
cli.a(type: String, longOpt: 'optA', required: false, args: 1, defaultValue: '', 'Option a (optional)')
def opts = cli.parse(args)
opts || System.exit(1)
if(opts.h) {
  cli.usage()
  System.exit(0)
}

println(opts.a.getClass())
if (opts.a) {
  println(opts.a)
}
{code}
with Groovy 3.0.2: {code}% ~/tmp/groovy-3.0.2/bin/groovy ./cliTest.groovy
class java.lang.Boolean{code} and with Groovy 3.0.4: {code}% ~/tmp/groovy-3.0.4/bin/groovy ./cliTest.groovy
class org.codehaus.groovy.runtime.NullObject{code}
Please reopen.

> 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, 2.5.12
>
>
> 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)