Picocli Groovy脚本错误元素LOCAL_VARIABLE上不允许使用Annotation @picocli.CommandLine$选项

jucafojl  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(177)

我正在一个groovy脚本中尝试使用picocli来获得一个交互式密码提示,并得到错误@picocli。元素LOCAL_VARIABLE上不允许使用CommandLine$Option。我使用的是Groovy 4.0.5和Picocli 4.6.3。下面是我的代码。

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()

在命令行我尝试

groovy PicocliTest.groovy findClassPath.groovy -p
zpf6vheq

zpf6vheq1#

我怀疑您缺少picocli-groovy依赖项。
手册中的Groovy示例使用@Grab来完成此操作:

@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"
}

相关问题