If I don’t try and get fancy with this line in the original test (example) ….
builder."${opt4.opt ?: '_'}"(opt4, 'desc opt:')
and I change my code to directly access the apache objects, I don’t seem to
have a problem.
Test code….
import org.apache.commons.cli.Option
println "groovy version = ${GroovySystem.getVersion()}"
println "java version = ${System.properties['java.version']}"
def builder = new CliBuilder()
def options = builder.options
def option
def addOption = { map ->
option = null
if (map.opt) {
option = new Option(map.opt,map.desc)
}
if (map.longOpt) {
if (!option) {
option = new Option(null,map.desc)
}
option.longOpt = map.longOpt
}
if (map.argName) {
option.argName = map.argName
option.args = 1
}
options.addOption(option)
}
addOption([opt:'x',desc:'opt1'])
addOption([opt:'y',longOpt:'why',desc:'opt2'])
addOption([opt:'z',longOpt:'zed',argName:'arg',args:1,desc:'opt3'])
addOption([longOpt:'quiet',desc:'opt4'])
println builder.options
builder.usage()
$ test_cli
groovy version = 2.4.15
java version = 1.8.0_91
[ Options: [ short {x=[ option: x :: opt1 ], y=[ option: y why :: opt2 ], z=[
option: z zed [ARG] :: opt3 ], quiet=[ option: null quiet :: opt4 ]} ] [ long
{zed=[ option: z zed [ARG] :: opt3 ], why=[ option: y why :: opt2 ], quiet=[
option: null quiet :: opt4 ]} ]
usage: groovy
--quiet opt4
-x opt1
-y,--why opt2
-z,--zed <arg> opt3
$ test_cli
groovy version = 2.5.0-rc-3
java version = 1.8.0_91
[ Options: [ short {x=[ option: x :: opt1 :: class java.lang.String ], y=[
option: y why :: opt2 :: class java.lang.String ], z=[ option: z zed [ARG] ::
opt3 :: class java.lang.String ], quiet=[ option: null quiet :: opt4 :: class
java.lang.String ]} ] [ long {why=[ option: y why :: opt2 :: class
java.lang.String ], zed=[ option: z zed [ARG] :: opt3 :: class
java.lang.String ], quiet=[ option: null quiet :: opt4 :: class
java.lang.String ]} ]
usage: groovy
--quiet opt4
-x opt1
-y,--why opt2
-z,--zed <arg> opt3
Erick Nelson
Senior Developer – IT
HD Supply Facilities Maintenance
(858) 740-6523
From: "Nelson, Erick" <[email protected]>
Date: Friday, May 25, 2018 at 5:39 PM
To: "[email protected]" <[email protected]>, "[email protected]"
<[email protected]>
Subject: Re: does 2.5 break CliBuilder?
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 <[email protected]>
Reply-To: "[email protected]" <[email protected]>,
"[email protected]" <[email protected]>
Date: Friday, May 25, 2018 at 5:02 PM
To: "[email protected]" <[email protected]>
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
<[email protected]<mailto:[email protected]>> 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