Annotation Type PicocliScript2

  • @Documented
    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 be transformed to PicocliBaseScript2.

    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")
     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 or CompilerConfiguration of GroovyShell.


    To customize, a base script class extending PicocliBaseScript2 may be specified as the value of this annotation, for example:

     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 to this object. The type of the annotated variable must extend PicocliBaseScript2.

     import picocli.groovy.PicocliScript2
     import com.mycompany.MyScriptBaseClass
     @PicocliScript2 MyScriptBaseClass theScript;

    PicocliBaseScript2 vs PicocliBaseScript

    See the PicocliBaseScript2 documentation for details.

    See Also:
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      Class<?> value  
    • Element Detail

      • value

        Class<?> value