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 | 154 | public class TrimExccessValues implements ConverterProcessor<Entry<RawOption, List<String>>> |
17 | |
{ |
18 | |
private final OptionsSpecification<?> specification; |
19 | |
private final List<String> validatedUnparsedArguments; |
20 | |
|
21 | 226 | public TrimExccessValues(final OptionsSpecification<?> specification, final List<String> validatedUnparsedArguments) { |
22 | 226 | this.specification = specification; |
23 | 226 | this.validatedUnparsedArguments = validatedUnparsedArguments; |
24 | 226 | } |
25 | |
|
26 | |
@Override |
27 | |
public Entry<RawOption, List<String>> convert(final Entry<RawOption, List<String>> from) { |
28 | 154 | final List<String> fromValues = from.getValue(); |
29 | |
List<String> convertedValues; |
30 | 154 | if (!specification.hasUnparsedSpecification()) { |
31 | 130 | convertedValues = fromValues; |
32 | |
} else { |
33 | 24 | final int maximumArgumentConsumption = min(fromValues.size(), specification.getSpecification(from.getKey().stringValue()).maximumArgumentConsumption()); |
34 | 24 | validatedUnparsedArguments.addAll(0, fromValues.subList(maximumArgumentConsumption, fromValues.size())); |
35 | 24 | convertedValues = FluentDollar._(fromValues.subList(0, maximumArgumentConsumption)); |
36 | |
} |
37 | |
|
38 | 154 | return FluentDollar.$.mapEntry(from.getKey(), convertedValues); |
39 | |
} |
40 | |
} |