Withnoe
Withnoe

Reputation: 125

Picocli Groovy script error Annotation @picocli.CommandLine$Option is not allowed on element LOCAL_VARIABLE

I am trying in a groovy script to use picocli to get an interactive password prompt and get the error @picocli.CommandLine$Option is not allowed on element LOCAL_VARIABLE. I am using Groovy 4.0.5 and Picocli 4.6.3. Below is my code.

import static picocli.CommandLine.*
import groovy.transform.Field
import java.security.MessageDigest

@Command(name = 'checksum', mixinStandardHelpOptions = true, version = 'checksum 4.0',
        description = 'Prints the checksum (SHA-256 by default) of a file to STDOUT.')
@picocli.groovy.PicocliScript

@Parameters(index = '0', description = 'The file whose checksum to calculate.')
@Field File file

@Option(names = ['-a', '--algorithm'], description = 'MD5, SHA-1, SHA-256, ...')
@Field String algorithm = 'SHA-256'

 @Option(names = [ '-p','--password'], description = "Passphrase", interactive = true)
 char[] password;



 println MessageDigest.getInstance(algorithm).digest(file.bytes).encodeHex().toString()

At the commandline I tried

groovy PicocliTest.groovy findClassPath.groovy -p

Upvotes: 1

Views: 113

Answers (1)

Remko Popma
Remko Popma

Reputation: 36834

I suspect you are missing the picocli-groovy dependency.

The Groovy example in the manual uses @Grab to accomplish this:

@Grab('info.picocli:picocli-groovy:4.6.3')
@GrabConfig(systemClassLoader=true)
@Command(name = "myScript",
        mixinStandardHelpOptions = true,
        description = "@|bold Groovy script|@ @|underline picocli|@ example")
@picocli.groovy.PicocliScript2
import groovy.transform.Field
import static picocli.CommandLine.*

@Option(names = ["-c", "--count"], description = "number of repetitions")
@Field int count = 1;

count.times {
   println "hi"
}

Upvotes: 0

Related Questions