我认为你可以通过改变
--list
从布尔值到字符串数组或集合的选项。
例如:
@Command(name = "status", description = "checks the status of a service")
public void status(
@Option(names = "--all", description = "checks all services.") boolean all,
@Option(names = "--list", arity = "1..*", paramLabel = "<service>",
description = "checks specified services.") List<String> services) {
if (all) {
System.out.println("check all");
} else if (services != null && !services.isEmpty()) {
System.out.println("check listed");
}
}
如果选项是互斥的,您可以使用
ArgGroup
.
但对于这种情况,最简单的解决方案可能是没有选择,只有一个要检查的服务列表。如果用户没有指定服务,则应用程序将检查所有服务。
在代码中:
@Command(name = "status",
description = "Checks the status of all services, or only the specified services.")
public void status(
@Parameters(paramLabel = "<service>", arity = "0..*",
description = "A list of service names. Omit to check all services.")
List<String> services) {
if (services == null || services.isEmpty()) {
System.out.println("check all");
} else {
System.out.println("check listed");
}
}