1

我正在将 Picocli 与 Groovy 一起使用来创建 CLI 工具,我按照此处的示例进行操作: https ://picocli.info/picocli-2.0-groovy-scripts-on-steroids.html

这个例子效果很好。但是无法在 Groovy 中获得多个子命令的简单工作示例。我想从 jar 中执行它,例如: java -jar picapp -count [number of times] java -jar picapp -names[List of name/s]

所以:

java -jar picapp count 3 
outputs: 
hi, hi , hi

java -jar picapp names bob john
outputs:
hi bob
hi john

我想我正在尝试以这种格式实现功能: 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);
        }
    }
}
4

1 回答 1

1

问题是picocli 2.0.3版本太旧,不支持该execute方法。该execute方法是在 picocli 4.0 中引入的。

建议始终使用最新的 picocli 版本。(考虑使用像dependabot这样的工具自动升级。)

于 2020-05-22T01:24:38.953 回答