Test code….

 

println "groovy version = ${GroovySystem.getVersion()}"

def builder = new CliBuilder()

def opt1 = [opt:'x']

builder."${opt1.opt ?: '_'}"(opt1, 'desc opt1')  // problem is here

def opt2 = [opt:'y',longOpt:'why']

builder."${opt2.opt ?: '_'}"(opt2, 'desc opt1')  // problem is here

def opt3 = [opt:'z',longOpt:'zed',argName:'arg',args:1]

builder."${opt3.opt ?: '_'}"(opt3, 'desc opt3')  // problem is here

def opt4 = [longOpt:'quiet']

builder."${opt4.opt ?: '_'}"(opt4, 'desc opt:')  // problem is here

println builder.options

builder.usage()

 

 

Test1

Shebang = #!/usr/bin/env /opt/apps/tools/groovy-2.4.15/bin/groovy

 

$ .test_cli

groovy version = 2.4.15

[ Options: [ short {x=[ option: x  :: desc opt1 ], y=[ option: y why  :: desc opt1 ], z=[ option: z zed  [ARG] :: desc opt3 ], quiet=[ option: null quiet  :: desc opt: ]} ] [ long {zed=[ option: z zed  [ARG] :: desc opt3 ], why=[ option: y why  :: desc opt1 ], quiet=[ option: null quiet  :: desc opt: ]} ]

usage: groovy

    --quiet       desc opt:

-x               desc opt1

-y,--why         desc opt1

-z,--zed <arg>   desc opt3

 

Test2

Shebang = #!/usr/bin/env /opt/apps/tools/groovy-2.5.0-rc-3/bin/groovy

 

$ test_cli

groovy version = 2.5.0-rc-3

Caught: groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: opt for class: org.apache.commons.cli.Option

groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: opt for class: org.apache.commons.cli.Option

                at test_cli.run(test_cli:6)

 

 

Erick Nelson

Senior Developer – IT

HD Supply Facilities Maintenance

 

 

From: Paul King <paulk@asert.com.au>
Reply-To: "users@groovy.apache.org" <users@groovy.apache.org>, "paulk@asert.com.au" <paulk@asert.com.au>
Date: Friday, May 25, 2018 at 5:02 PM
To: "users@groovy.apache.org" <users@groovy.apache.org>
Subject: Re: does 2.5 break CliBuilder?

 

Do you have a standalone example which triggers the error, i.e. with map and config already set? That will save us time reproducing.

 

Cheers, Paul.

 

On Sat, May 26, 2018 at 2:59 AM, Nelson, Erick <Erick.Nelson@hdsupply.com> wrote:

Caught: groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: opt for class: org.apache.commons.cli.Option

groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: opt for class: org.apache.commons.cli.Option

       at script.Cli.createOption(Cli.groovy:158)

       at script.Cli.createDefaultOptions(Cli.groovy:88)

       at script.Cli.<init>(Cli.groovy:29)

       at test_cli.run(test_cli.groovy:6)

 

I see that it upgrades from commons-cli 1.2 to 1.4

 

Code snippet…

I’m dynamically trying to build default command line options for my scripting framework.

This works in 2.4

 

        // validate the option

        Integer slen = map.opt.length() // short opt length

        Integer llen = map.longOpt.length() // long opt length

        if (slen > 1 || llen == 1 || (slen == 0 && llen == 0)) {

            log.warn "invalid cli opt definition [$config]"

            return

        }

        if (slen) {

            if (builder.options.getOption(map.opt)) {

                log.warn "opt already used [$config]"

                return

            }

        }

        if (llen) {

            if (builder.options.getOption(map.longOpt)) {

                log.warn "longOpt already used [$config]"

                return

            }

        }

        // add the option

        Map builderOption = [:]

        if (llen > 0) {

            builderOption.longOpt = map.longOpt

        }

        if (slen > 0) {

            builderOption.opt = map.opt

        }

        if (map.argName) {

            builderOption.args = 1

            builderOption.argName = map.argName

        }

        builder."${map.opt ?: '_'}"(builderOption, map.desc)  // problem is here