1

我在下面有以下代码。

我的用例是,当使用选项--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

4

1 回答 1

1

您应该能够从类中的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;
    }
}
于 2020-04-21T10:51:04.533 回答