【问题标题】:PicoCli Mutually Dependent Options question (resolve values)PicoCli 相互依赖的选项问题(解析值)
【发布时间】:2020-04-20 22:39:47
【问题描述】:

我有下面的代码。

我的用例是,当使用选项--install 时,您还必须使用选项--version。这在使用以下 Dependent 组时效果很好。

但是我无法获得变量版本的值。我如何提取/解决它? 有什么方法可以通过 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

【问题讨论】:

    标签: java picocli


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2011-12-16
      • 2021-07-31
      • 2023-03-29
      • 1970-01-01
      • 2019-05-04
      相关资源
      最近更新 更多