1
1
package io .airlift .airline ;
2
2
3
- import com .google .common .base .Function ;
4
- import com .google .common .base .Joiner ;
5
3
import com .google .common .collect .ComparisonChain ;
6
- import com .google .common .collect .ImmutableList ;
7
- import com .google .common .collect .Lists ;
8
4
import io .airlift .airline .model .ArgumentsMetadata ;
9
5
import io .airlift .airline .model .CommandMetadata ;
10
6
import io .airlift .airline .model .OptionMetadata ;
11
7
12
- import javax .annotation .Nullable ;
13
-
14
8
import java .util .Comparator ;
15
9
import java .util .List ;
16
10
import java .util .Set ;
17
11
18
- import static com .google .common .collect .Iterables .filter ;
19
- import static com .google .common .collect .Iterables .transform ;
20
- import static io .airlift .airline .model .OptionMetadata .isHiddenPredicate ;
12
+ import static com .google .common .collect .ImmutableList .toImmutableList ;
13
+ import static java .util .stream .Collectors .joining ;
21
14
22
15
public class UsageHelper
23
16
{
@@ -54,35 +47,21 @@ public int compare(CommandMetadata o1, CommandMetadata o2)
54
47
55
48
public static String toDescription (OptionMetadata option )
56
49
{
57
- Set <String > options = option .getOptions ();
58
- StringBuilder stringBuilder = new StringBuilder ();
59
-
60
- final String argumentString ;
61
- if (option .getArity () > 0 ) {
62
- argumentString = Joiner .on (" " ).join (Lists .transform (ImmutableList .of (option .getTitle ()), new Function <String , String >()
63
- {
64
- public String apply (@ Nullable String argument )
65
- {
66
- return "<" + argument + ">" ;
67
- }
68
- }));
69
- }
70
- else {
71
- argumentString = null ;
72
- }
73
-
74
- Joiner .on (", " ).appendTo (stringBuilder , transform (options , new Function <String , String >()
75
- {
76
- public String apply (@ Nullable String option )
77
- {
78
- if (argumentString != null ) {
79
- return option + " " + argumentString ;
80
- }
81
- return option ;
82
- }
83
- }));
50
+ return optionString (option , ", " );
51
+ }
84
52
85
- return stringBuilder .toString ();
53
+ private static String optionString (OptionMetadata option , String delimiter )
54
+ {
55
+ String argument = (option .getArity () > 0 ) ? ("<" + option .getTitle () + ">" ) : null ;
56
+
57
+ return option .getOptions ().stream ()
58
+ .map (value -> {
59
+ if (argument != null ) {
60
+ return value + " " + argument ;
61
+ }
62
+ return value ;
63
+ })
64
+ .collect (joining (delimiter ));
86
65
}
87
66
88
67
public static String toDescription (ArgumentsMetadata arguments )
@@ -107,32 +86,7 @@ public static String toUsage(OptionMetadata option)
107
86
stringBuilder .append ('(' );
108
87
}
109
88
110
- final String argumentString ;
111
- if (option .getArity () > 0 ) {
112
- argumentString = Joiner .on (" " ).join (transform (ImmutableList .of (option .getTitle ()), new Function <String , String >()
113
- {
114
- public String apply (@ Nullable String argument )
115
- {
116
- return "<" + argument + ">" ;
117
- }
118
- }));
119
- }
120
- else {
121
- argumentString = null ;
122
- }
123
-
124
- Joiner .on (" | " ).appendTo (stringBuilder , transform (options , new Function <String , String >()
125
- {
126
- public String apply (@ Nullable String option )
127
- {
128
- if (argumentString != null ) {
129
- return option + " " + argumentString ;
130
- }
131
- else {
132
- return option ;
133
- }
134
- }
135
- }));
89
+ stringBuilder .append (optionString (option , " | " ));
136
90
137
91
if (options .size () > 1 ) {
138
92
stringBuilder .append (')' );
@@ -174,12 +128,9 @@ public static String toUsage(ArgumentsMetadata arguments)
174
128
175
129
public static List <String > toSynopsisUsage (List <OptionMetadata > options )
176
130
{
177
- return ImmutableList .copyOf (transform (filter (options , isHiddenPredicate ()), new Function <OptionMetadata , String >()
178
- {
179
- public String apply (OptionMetadata option )
180
- {
181
- return toUsage (option );
182
- }
183
- }));
131
+ return options .stream ()
132
+ .filter (input -> !input .isHidden ())
133
+ .map (UsageHelper ::toUsage )
134
+ .collect (toImmutableList ());
184
135
}
185
136
}
0 commit comments