CliBuilder在Groovy 2.5中进行了更新,增加了对picocli支持实现的支持(groovy.cli.picocli.CliBuilder
).This article显示了CliBuilder中现在可用的新功能的许多详细信息.
然而,即使在这个版本的CliBuilder中,公共API也只支持位置参数列表,并且该列表只有一个描述.
@Unparsed(description = 'positional parameters')
List positionals
遗憾的是,CliBuilder API目前无法在应用程序的使用帮助消息中显示单独的位置参数和单独的描述.
如果您的目标是拥有单独的位置参数,可能是使用单独的类型、单独的默认值等,并让用法帮助消息显示每个位置参数的单独描述,那么您可能需要考虑在Groovy脚本或Groovy应用程序中直接使用picocli(不使用CliBuilder).
picocli用户手册有许多Groovy示例,其中有一节专门介绍using picocli in Groovy applications and Groovy scripts.本文(Groovy Scripts on Steroids)也可能有用.
下面是一个示例task-profile
Groovy脚本,其中包含三个位置参数:
// task-profile.groovy
@Grab('info.picocli:picocli-groovy:4.6.3')
@GrabConfig(systemClassLoader=true)
@Command(name = "task-profile", version = "task-profile 1.0",
mixinStandardHelpOptions = true, // add --help and --version options
description = "Task Profile")
@picocli.groovy.PicocliScript2
import groovy.transform.Field
import static picocli.CommandLine.*
@Parameters(index = "0", description = "The first task")
@Field String task1;
@Parameters(index = "1", description = "The second task")
@Field String task2;
@Parameters(index = "2", description = "The third task")
@Field String task3;
// PicocliBaseScript2 prints usage help or version if requested by the user
println "hi, the selected tasks are $task1, $task2 and $task3"