Using Groovy @SourceURI with Picocli @PicocliScript2 and Subcommands??? #2490
-
|
I'm writing a Groovy script with Picocli and the @Grab('info.picocli:picocli-groovy:4.7.7')
@GrabConfig(systemClassLoader = true)
@picocli.CommandLine.Command(name = "example", mixinStandardHelpOptions = true, description = "Example script")
@picocli.groovy.PicocliScript2
import groovy.transform.Field
import groovy.transform.SourceURI
import picocli.CommandLine.Command
import picocli.CommandLine.ExitCode
import picocli.CommandLine.Help.Ansi
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Spec
@SourceURI
URI scriptSourceUri
@Field
@Spec
CommandSpec spec
println(scriptSourceUri) // This prints the URI
println(Ansi.AUTO.text('@|bold,red ERROR|@ Missing command'))
spec.commandLine().usage(System.err)
System.exit(ExitCode.USAGE)
@SuppressWarnings('unused')
@Command(name = "start", description = "Start")
int startCluster() {
println(scriptSourceUri) // groovy.lang.MissingPropertyException: No such property: scriptSourceUri for class: example
return ExitCode.OK
}
@SuppressWarnings('unused')
@Command(name = "delete", description = "Delete")
int deleteCluster() {
println(scriptSourceUri) // No such property: scriptSourceUri for class: example
return ExitCode.OK
}Here's the exception that I get: I'm aware that both |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi that's a good question. I suspect you may be right about the AST transformations... |
Beta Was this translation helpful? Give feedback.
I appreciate you taking a look! I just stumbled upon a fix and while It's not the best it's definitely something!
I originally thought I should mark the annotated
@SourceURIvariable with@Fieldas well to add it to the class that Picocli was creating. This works great with the other variables in the script.When I did this it didn't work. I can't remember if the variable was
nullor if there was an exception but I abandoned it. I just reversed the order of the annotations and it worked!I'm not thrilled that the order matters here but I'm just glad it works!
I will definitely reach out to the groovy folks to find…