-
@Documented @Retention(SOURCE) @Target({LOCAL_VARIABLE,PACKAGE,TYPE,FIELD}) public @interface PicocliScript2
Annotation to give Groovy scripts convenient access to picocli functionality, updated for picocli version 4 and greater. Scripts may annotate the package statement, an import statement or a local variable with
@PicocliScript2
and the script base class will betransformed
toPicocliBaseScript2
.Also, any
CommandLine.Command
annotation on the same variable or import statement will be added to the script class. With the@Command
annotation scripts can customize elements shown in the usage message like command name, description, headers, footers etc.Example usage
@Command(name = "myCommand", description = "does something special") @PicocliScript2 import picocli.groovy.PicocliScript2 import picocli.CommandLine.Command import picocli.CommandLine.Option import groovy.transform.Field @Option(names = "-x", description = "number of repetitions") @Field int count; @Option(names = ["-h", "--help"], usageHelp = true, description = "print this help message and exit") @Field boolean helpRequested; //if (helpRequested) { CommandLine.usage(this, System.err); return 0; } // PicocliBaseScript takes care of this count.times { println "hi" } assert this == theScript assert this.commandLine.commandName == "myCommand"
Otherwise, this annotation works similar to the Groovy built-in
BaseScript
. Using this annotation will override the base script set by Groovy compiler orCompilerConfiguration
ofGroovyShell
.Customizing
To customize, a base script class extending
PicocliBaseScript2
may be specified as the value of this annotation, for example:@PicocliScript2(com.mycompany.MyScriptBaseClass) import picocli.groovy.PicocliScript2
An alternative way to customize the base script is annotating a local variable with
@PicocliScript2
. This way the variable type will be used as the base script class and the annotated variable will become a shortcut tothis
object. The type of the annotated variable must extendPicocliBaseScript2
.import picocli.groovy.PicocliScript2 import com.mycompany.MyScriptBaseClass @PicocliScript2 MyScriptBaseClass theScript;
PicocliBaseScript2 vs PicocliBaseScript
See the
PicocliBaseScript2
documentation for details.- Since:
- 2.0
- See Also:
PicocliScriptASTTransformation
-
-
Element Detail
-
value
Class<?> value
- Default:
- picocli.groovy.PicocliBaseScript2.class
-
-