1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package com.lexicalscope.jewel.cli.validation; |
6 | |
|
7 | |
import static com.lexicalscope.fluent.FluentDollar.$; |
8 | |
import static com.lexicalscope.jewel.cli.validation.RawOptionMatchers.isLastOption; |
9 | |
|
10 | |
import java.util.List; |
11 | |
import java.util.Map.Entry; |
12 | |
|
13 | |
import ch.lambdaj.function.convert.Converter; |
14 | |
|
15 | |
import com.lexicalscope.fluent.functions.BiConverter; |
16 | |
import com.lexicalscope.fluent.map.transforms.MapPipelineBuilder; |
17 | |
import com.lexicalscope.jewel.cli.ValidationErrorBuilder; |
18 | |
import com.lexicalscope.jewel.cli.specification.OptionsSpecification; |
19 | |
import com.lexicalscope.jewel.cli.specification.ParsedOptionSpecification; |
20 | |
|
21 | |
class ValidationPipeline |
22 | |
{ |
23 | |
private final OptionsSpecification<?> specification; |
24 | |
private final ValidationErrorBuilder validationErrorBuilder; |
25 | |
|
26 | |
ValidationPipeline(final OptionsSpecification<?> specification, final ValidationErrorBuilder validationErrorBuilder) |
27 | 226 | { |
28 | 226 | this.specification = specification; |
29 | 226 | this.validationErrorBuilder = validationErrorBuilder; |
30 | 226 | } |
31 | |
|
32 | |
MapPipelineBuilder<RawOption, List<String>, ParsedOptionSpecification, List<String>> buildValidationPipeline(final List<String> validatedUnparsedArguments) |
33 | |
{ |
34 | 226 | return $.<RawOption, List<String>>mapPipeline(). |
35 | |
allowKeys(thatAreKnownOptions()). |
36 | |
processPuts(trimExcessOptionsInto(validatedUnparsedArguments)). |
37 | |
convertKeys(lookupSpecifcationFromOption()). |
38 | |
allowKeys(butRejectHelpOption()). |
39 | |
vetoPuts(thatHaveTheWrongNumberOfValues()). |
40 | |
vetoPuts(thatHaveValuesInTheWrongFormat()); |
41 | |
} |
42 | |
|
43 | |
private KnownOptions thatAreKnownOptions() |
44 | |
{ |
45 | 226 | return new KnownOptions(specification, validationErrorBuilder); |
46 | |
} |
47 | |
|
48 | |
private Converter<Entry<RawOption, List<String>>, Entry<RawOption, List<String>>> trimExcessOptionsInto(final List<String> validatedUnparsedArguments) |
49 | |
{ |
50 | 226 | return $.predicatedConverter(isLastOption(), new TrimExccessValues(specification, validatedUnparsedArguments)); |
51 | |
} |
52 | |
|
53 | |
private BiConverter<ParsedOptionSpecification, RawOption> lookupSpecifcationFromOption() |
54 | |
{ |
55 | 226 | return new ConverterRawOptionToParsedOptionSpecification(specification); |
56 | |
} |
57 | |
|
58 | |
private ReportWrongFormatValues thatHaveValuesInTheWrongFormat() |
59 | |
{ |
60 | 226 | return new ReportWrongFormatValues(validationErrorBuilder); |
61 | |
} |
62 | |
|
63 | |
private ReportWrongNumberOfValues thatHaveTheWrongNumberOfValues() |
64 | |
{ |
65 | 226 | return new ReportWrongNumberOfValues(validationErrorBuilder); |
66 | |
} |
67 | |
|
68 | |
private RejectHelpOption butRejectHelpOption() |
69 | |
{ |
70 | 226 | return new RejectHelpOption(specification); |
71 | |
} |
72 | |
} |