Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9599

CliBuilder: Option with "type: String, defaultValue ''" (empty String) results in NullObject if default value is applied

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.5.11, 3.0.4
    • 3.0.5, 2.5.13
    • None

    Description

      This is a follow-up ticket for GROOVY-9519:

      After checking 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:

      @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)
      }
      

      with Groovy 3.0.2:

      % ~/tmp/groovy-3.0.2/bin/groovy ./cliTest.groovy
      class java.lang.Boolean
      

      and with Groovy 3.0.4:

      % ~/tmp/groovy-3.0.4/bin/groovy ./cliTest.groovy
      class org.codehaus.groovy.runtime.NullObject
      

      Attachments

        Issue Links

          Activity

            People

              rpopma Remko Popma
              rpopma Remko Popma
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: