PicoCli相互依赖选项问题(解析值)

时间:2020-04-20 22:39:47

标签: java picocli

我在下面有以下代码。

我的用例是,使用选项--install时,还必须使用选项--version。使用下面的从属组时,此方法运行良好。

但是我无法获得变量版本的值。我如何提取/解决它? 有什么办法可以通过call()方法做到这一点?

@CommandLine.Command(name = "rt")
@Component
public class Artifactory implements Callable<Integer> {


    @CommandLine.ArgGroup(exclusive = false)
    Dependent dependent;

    static class Dependent{
        @CommandLine.Option(names = {"-i", "--install"}, description = "The Artifactory version")
        boolean install;

        @CommandLine.Option(names = {"-v", "--version"}, required = false, description = "The Artifactory version")
        String version;
    }

    @Override
    public Integer call() throws Exception {
        System.out.println("We are installing Artifactory version from @Artifactory: " + version);
        return 0;
    }

}

感谢您的帮助:D

1 个答案:

答案 0 :(得分:0)

您应该能够从dependent类的Artifactory字段中获取指定的值。例如:

@Command(name = "rt")
@Component
public class Artifactory implements Callable<Integer> {


    @ArgGroup(exclusive = false, multiplicity = "1")
    Dependent dependent;

    static class Dependent{
        @Option(names = {"-i", "--install"}, description = "Install or not.")
        boolean install;

        @Option(names = {"-v", "--version"}, required = false,
          description = "The Artifactory version.")
        String version;
    }

    @Override
    public Integer call() throws Exception {
        // I made the arg-group multiplicity=1, so it cannot be `null`.
        // The default is `multiplicity = "0..1"`;
        // in that case you need to check that dependent != null (if the group was not specified).
        System.out.printf(
                "We are %sinstalling Artifactory version %s%n",
                (dependent.install ? "" : "not "), dependent.version);

        return 0;
    }
}