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

CLIBuilder: Option with "type: Integer, defaultValue '0'" results in Boolean if default value is applied

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.0.2
    • Fix Version/s: 3.0.4, 2.5.12
    • Labels:
      None
    • Environment:
      WIndows/Linux

      Description

      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:

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

      Executing this w/o providing -i prints:

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

      But if -i IS provided (even with the same value as the default), it correctly prints:

      % ~/tmp/groovy-3.0.2/bin/groovy ./cliTest.groovy -i 0
      class java.lang.Integer
      0
      class java.lang.Integer
      0

      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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                rpopma Remko Popma
                Reporter:
                dheinric Dirk Heinrichs
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: