jcommander
jcommander copied to clipboard
IDefaultProvider for lists duplicates defaults for each name
When using a list parameter with multiple names
@Parameter(names = {"--label", "-l"},)
List<String> labels;
and a IDefaultProvider
public static final class ConstantDefaultProvider implements IDefaultProvider {
@Override
public String getDefaultValueFor(String optionName) {
return "hello";
}
}
the labels parameter will contain the default for each name, in this case twice, once parsing is complete.
The reason is that separate a ParameterDescription is created for each name of a parameter, which are however backed by single Parameter object. The default provider is called once for each description, which is acceptable for single value fields since the field will be simply overwritten (provided that the default provider handles both cases the same way), but in the case of collections we are mutating the field instead causing duplicates.