1 | |
package com.lexicalscope.jewel.cli; |
2 | |
|
3 | |
import static com.lexicalscope.fluentreflection.ReflectionMatchers.annotatedWith; |
4 | |
|
5 | |
import java.util.Map; |
6 | |
|
7 | |
import com.lexicalscope.fluentreflection.FluentClass; |
8 | |
import com.lexicalscope.fluentreflection.FluentMethod; |
9 | |
import com.lexicalscope.jewel.cli.specification.OptionsSpecification; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
class InstanceArgumentPresentingStrategy<O> implements ArgumentPresentingStrategy<O> { |
28 | |
private final O options; |
29 | |
private final FluentClass<O> klass; |
30 | |
private final OptionsSpecification<O> specification; |
31 | |
|
32 | |
public InstanceArgumentPresentingStrategy( |
33 | |
final OptionsSpecification<O> specification, |
34 | |
final FluentClass<O> klass, |
35 | 14 | final O options) { |
36 | 14 | this.specification = specification; |
37 | 14 | this.klass = klass; |
38 | 14 | this.options = options; |
39 | 14 | } |
40 | |
|
41 | |
@Override public O presentArguments(final Map<String, Object> argumentMap) { |
42 | 12 | for (final FluentMethod reflectedMethod : klass.methods(annotatedWith(Option.class))) { |
43 | 28 | final boolean isBoolean = specification.getSpecification(reflectedMethod).isBoolean(); |
44 | 28 | setValueOnOptions(argumentMap, reflectedMethod, isBoolean); |
45 | 28 | } |
46 | 12 | for (final FluentMethod reflectedMethod : klass.methods(annotatedWith(Unparsed.class))) { |
47 | 2 | setValueOnOptions(argumentMap, reflectedMethod, false); |
48 | 2 | } |
49 | 12 | return options; |
50 | |
} |
51 | |
|
52 | |
private void setValueOnOptions( |
53 | |
final Map<String, Object> argumentMap, |
54 | |
final FluentMethod reflectedMethod, |
55 | |
final boolean isBoolean) { |
56 | 30 | if (argumentMap.containsKey(reflectedMethod.property())) |
57 | |
{ |
58 | 22 | if (isBoolean) |
59 | |
{ |
60 | 6 | reflectedMethod.call(options, argumentMap.containsKey(reflectedMethod.property())); |
61 | |
} |
62 | 16 | else if (argumentMap.get(reflectedMethod.property()) != null) |
63 | |
{ |
64 | 14 | reflectedMethod.call(options, argumentMap.get(reflectedMethod.property())); |
65 | |
} |
66 | |
} |
67 | 30 | } |
68 | |
} |