jcommander icon indicating copy to clipboard operation
jcommander copied to clipboard

IDefaultProvider for lists duplicates defaults for each name

Open zentol opened this issue 7 years ago • 0 comments

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.

zentol avatar Sep 28 '18 12:09 zentol