我有类似的选择
@CommandLine.Option(names = "-D", description = "Define a symbol.")
/* A list of defines provided by the user. */
Map<String, String> defines = new LinkedHashMap<String, String>();
当我执行以下操作时,它确实起作用:
-Dkey=value
但是我什么时候做
-Dkey
它不起作用。是否可以为没有关联值的键添加默认值?
答案 0 :(得分:1)
这可以通过custom parameterConsumer完成。例如:
/* A list of defines provided by the user. */
@Option(names = "-D", parameterConsumer = MyMapParameterConsumer.class,
description = "Define a symbol.")
Map<String, String> defines = new LinkedHashMap<String, String>();
... MyMapParameterConsumer
如下所示:
class MyMapParameterConsumer implements IParameterConsumer {
@Override
public void consumeParameters(
Stack<String> args,
ArgSpec argSpec,
CommandSpec commandSpec) {
if (args.isEmpty()) {
throw new ParameterException(commandSpec.commandLine(),
"Missing required parameter");
}
String parameter = args.pop();
String[] keyValue = parameter.split("=", 1);
String key = keyValue[0];
String value = keyValue.length > 1
? keyValue[1]
: "MY_DEFAULT";
Map<String, String> map = argSpec.getValue();
map.put(key, value);
}
}