我正在使用Picocli和Groovy来创建一个CLI工具,我遵循了下面的示例:https://picocli.info/picocli-2.0-groovy-scripts-on-steroids.html
这个例子运行得很好。但是在Groovy中无法获得多个子命令的简单工作示例。我想从jar执行它,如下所示: java -jar picapp -count次数java -jar picapp -namesList of name/s
所以:
java -jar picapp count 3
outputs:
hi, hi , hi
java -jar picapp names bob john
outputs:
hi bob
hi john我猜我正在尝试以this格式实现该功能:https://github.com/remkop/picocli/blob/master/picocli-examples/src/main/java/picocli/examples/subcommands/SubCmdsViaMethods.java
下面的Groovy代码无法编译:
@Grab('info.picocli:picocli:2.0.3')
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.Spec;
import java.util.Locale;
@Command(name = "hi", subcommands = { CommandLine.HelpCommand.class },
description = "hi")
public class picapp implements Runnable {
@Command(name = "count", description = "count")
void country(@Parameters(arity = "1..*", paramLabel = "count",
description = "count") int count) {
count.times {
println("hi $it...")
}
}
@Command(name = "names", description = "names")
void language(@Parameters(arity = "1..*", paramLabel = "names",
description = "name") String[] names) {
println 'CmdLineTool says \n\tWelcome:'
names.each {
println '\t\t' + it
}
}
@Override
public void run() {
throw new ParameterException(spec.commandLine(), "Specify a subcommand");
}
public static void main(String[] args) {
CommandLine cmd = new CommandLine(new SubCmdsViaMethods());
if (args.length == 0) {
cmd.usage(System.out);
}
else {
cmd.execute(args);
}
}
}发布于 2020-05-22 09:24:39
问题是picocli版本2.0.3是旧的,不支持execute方法。在picocli 4.0中引入了execute方法。
建议始终使用最新的picocli版本。(考虑使用dependabot等工具自动升级。)
https://stackoverflow.com/questions/59680098
复制相似问题