1
2
3
4
5 package com.lexicalscope.jewel.cli.validation;
6
7 import static java.lang.Math.min;
8
9 import com.lexicalscope.fluent.FluentDollar;
10 import com.lexicalscope.fluent.adapters.ConverterProcessor;
11 import com.lexicalscope.jewel.cli.specification.OptionsSpecification;
12
13 import java.util.List;
14 import java.util.Map.Entry;
15
16 public class TrimExccessValues implements ConverterProcessor<Entry<RawOption, List<String>>>
17 {
18 private final OptionsSpecification<?> specification;
19 private final List<String> validatedUnparsedArguments;
20
21 public TrimExccessValues(final OptionsSpecification<?> specification, final List<String> validatedUnparsedArguments) {
22 this.specification = specification;
23 this.validatedUnparsedArguments = validatedUnparsedArguments;
24 }
25
26 @Override
27 public Entry<RawOption, List<String>> convert(final Entry<RawOption, List<String>> from) {
28 final List<String> fromValues = from.getValue();
29 List<String> convertedValues;
30 if (!specification.hasUnparsedSpecification()) {
31 convertedValues = fromValues;
32 } else {
33 final int maximumArgumentConsumption = min(fromValues.size(), specification.getSpecification(from.getKey().stringValue()).maximumArgumentConsumption());
34 validatedUnparsedArguments.addAll(0, fromValues.subList(maximumArgumentConsumption, fromValues.size()));
35 convertedValues = FluentDollar._(fromValues.subList(0, maximumArgumentConsumption));
36 }
37
38 return FluentDollar.$.mapEntry(from.getKey(), convertedValues);
39 }
40 }