in log4j-core/src/main/java/org/apache/logging/log4j/core/tools/picocli/CommandLine.java [2467:2526]
private int applyValueToSingleValuedField(
final Field field,
final Range arity,
final Stack<String> args,
final Class<?> cls,
final Set<Field> initialized,
final String argDescription)
throws Exception {
final boolean noMoreValues = args.isEmpty();
String value = args.isEmpty() ? null : trim(args.pop()); // unquote the value
int result = arity.min; // the number or args we need to consume
// special logic for booleans: BooleanConverter accepts only "true" or "false".
if ((cls == Boolean.class || cls == Boolean.TYPE) && arity.min <= 0) {
// boolean option with arity = 0..1 or 0..*: value MAY be a param
if (arity.max > 0 && ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))) {
result = 1; // if it is a varargs we only consume 1 argument if it is a boolean value
} else {
if (value != null) {
args.push(value); // we don't consume the value
}
final Boolean currentValue = (Boolean) field.get(command);
value = String.valueOf(
currentValue == null ? true : !currentValue); // #147 toggle existing boolean value
}
}
if (noMoreValues && value == null) {
return 0;
}
final ITypeConverter<?> converter = getTypeConverter(cls, field);
final Object newValue = tryConvert(field, -1, converter, value, cls);
final Object oldValue = field.get(command);
TraceLevel level = TraceLevel.INFO;
String traceMessage = "Setting %s field '%s.%s' to '%5$s' (was '%4$s') for %6$s%n";
if (initialized != null) {
if (initialized.contains(field)) {
if (!isOverwrittenOptionsAllowed()) {
throw new OverwrittenOptionException(
CommandLine.this, optionDescription("", field, 0) + " should be specified only once");
}
level = TraceLevel.WARN;
traceMessage = "Overwriting %s field '%s.%s' value '%s' with '%s' for %s%n";
}
initialized.add(field);
}
if (tracer.level.isEnabled(level)) {
level.print(
tracer,
traceMessage,
field.getType().getSimpleName(),
field.getDeclaringClass().getSimpleName(),
field.getName(),
String.valueOf(oldValue),
String.valueOf(newValue),
argDescription);
}
field.set(command, newValue);
return result;
}