17
17
import java .util .ArrayList ;
18
18
import java .util .List ;
19
19
import java .util .ResourceBundle ;
20
+ import java .util .function .Function ;
20
21
import java .util .regex .Pattern ;
21
22
import java .util .stream .Collectors ;
22
23
@@ -57,10 +58,22 @@ private static int printI18n(String language) {
57
58
List <String > languages = dialectProvider .getLanguages ();
58
59
59
60
if (language .equalsIgnoreCase ("help" )) {
60
- for (String code : languages ) {
61
- System .out .println (code );
61
+ if (language .equalsIgnoreCase ("help" )) {
62
+ List <GherkinDialect > dialects = new ArrayList <>();
63
+ for (String code : languages ) {
64
+ GherkinDialect dialect = dialectProvider .getDialect (code , null );
65
+ dialects .add (dialect );
66
+ }
67
+
68
+ int widestLanguage = findWidest (dialects , GherkinDialect ::getLanguage );
69
+ int widestName = findWidest (dialects , GherkinDialect ::getName );
70
+ int widestNativeName = findWidest (dialects , GherkinDialect ::getNativeName );
71
+
72
+ for (GherkinDialect dialect : dialects ) {
73
+ printDialect (dialect , widestLanguage , widestName , widestNativeName );
74
+ }
75
+ return 0 ;
62
76
}
63
- return 0 ;
64
77
}
65
78
if (languages .contains (language )) {
66
79
return printKeywordsFor (dialectProvider .getDialect (language , null ));
@@ -70,6 +83,14 @@ private static int printI18n(String language) {
70
83
return 1 ;
71
84
}
72
85
86
+ private static int findWidest (List <GherkinDialect > dialects , Function <GherkinDialect , String > getNativeName ) {
87
+ return dialects .stream ()
88
+ .map (getNativeName )
89
+ .mapToInt (String ::length )
90
+ .max ()
91
+ .orElse (0 );
92
+ }
93
+
73
94
private static int printKeywordsFor (GherkinDialect dialect ) {
74
95
StringBuilder builder = new StringBuilder ();
75
96
List <List <String >> table = new ArrayList <>();
@@ -108,6 +129,21 @@ private static void addKeywordRow(List<List<String>> table, String key, List<Str
108
129
table .add (asList (key , keywords .stream ().map (o -> '"' + o + '"' ).collect (joining (", " ))));
109
130
}
110
131
132
+ private static void printDialect (GherkinDialect dialect , int widestLanguage , int widestName , int widestNativeName ) {
133
+ String langCode = rightPad (dialect .getLanguage (), widestLanguage );
134
+ String name = rightPad (dialect .getName (), widestName );
135
+ String nativeName = rightPad (dialect .getNativeName (), widestNativeName );
136
+
137
+ System .out .println (langCode + name + nativeName );
138
+ }
139
+
140
+ private static String rightPad (String text , int maxWidth ) {
141
+ int padding = 7 ;
142
+ int width = maxWidth + padding ;
143
+
144
+ return String .format ("%" + -width + "s" , text );
145
+ }
146
+
111
147
RuntimeOptionsBuilder parse (List <String > args ) {
112
148
args = new ArrayList <>(args );
113
149
RuntimeOptionsBuilder parsedOptions = new RuntimeOptionsBuilder ();
@@ -122,47 +158,47 @@ RuntimeOptionsBuilder parse(List<String> args) {
122
158
System .out .println (VERSION );
123
159
System .exit (0 );
124
160
} else if (arg .equals ("--i18n" )) {
125
- String nextArg = args . remove ( 0 );
161
+ String nextArg = removeArgFor ( arg , args );
126
162
System .exit (printI18n (nextArg ));
127
163
} else if (arg .equals ("--threads" )) {
128
- int threads = Integer .parseInt (args . remove ( 0 ));
164
+ int threads = Integer .parseInt (removeArgFor ( arg , args ));
129
165
if (threads < 1 ) {
130
166
throw new CucumberException ("--threads must be > 0" );
131
167
}
132
168
parsedOptions .setThreads (threads );
133
169
} else if (arg .equals ("--glue" ) || arg .equals ("-g" )) {
134
- String gluePath = args . remove ( 0 );
170
+ String gluePath = removeArgFor ( arg , args );
135
171
URI parse = GluePath .parse (gluePath );
136
172
parsedOptions .addGlue (parse );
137
173
} else if (arg .equals ("--tags" ) || arg .equals ("-t" )) {
138
- parsedOptions .addTagFilter (args . remove ( 0 ));
174
+ parsedOptions .addTagFilter (removeArgFor ( arg , args ));
139
175
} else if (arg .equals ("--plugin" ) || arg .equals ("--add-plugin" ) || arg .equals ("-p" )) {
140
- parsedOptions .addPluginName (args . remove ( 0 ), arg .equals ("--add-plugin" ));
176
+ parsedOptions .addPluginName (removeArgFor ( arg , args ), arg .equals ("--add-plugin" ));
141
177
} else if (arg .equals ("--no-dry-run" ) || arg .equals ("--dry-run" ) || arg .equals ("-d" )) {
142
178
parsedOptions .setDryRun (!arg .startsWith ("--no-" ));
143
179
} else if (arg .equals ("--no-strict" ) || arg .equals ("--strict" ) || arg .equals ("-s" )) {
144
180
parsedOptions .setStrict (!arg .startsWith ("--no-" ));
145
181
} else if (arg .equals ("--no-monochrome" ) || arg .equals ("--monochrome" ) || arg .equals ("-m" )) {
146
182
parsedOptions .setMonochrome (!arg .startsWith ("--no-" ));
147
183
} else if (arg .equals ("--snippets" )) {
148
- String nextArg = args . remove ( 0 );
184
+ String nextArg = removeArgFor ( arg , args );
149
185
parsedOptions .setSnippetType (SnippetTypeParser .parseSnippetType (nextArg ));
150
186
} else if (arg .equals ("--name" ) || arg .equals ("-n" )) {
151
- String nextArg = args . remove ( 0 );
187
+ String nextArg = removeArgFor ( arg , args );
152
188
Pattern pattern = Pattern .compile (nextArg );
153
189
parsedOptions .addNameFilter (pattern );
154
190
} else if (arg .equals ("--wip" ) || arg .equals ("-w" )) {
155
191
parsedOptions .setWip (true );
156
192
} else if (arg .equals ("--order" )) {
157
- parsedOptions .setPickleOrder (PickleOrderParser .parse (args . remove ( 0 )));
193
+ parsedOptions .setPickleOrder (PickleOrderParser .parse (removeArgFor ( arg , args )));
158
194
} else if (arg .equals ("--count" )) {
159
- int count = Integer .parseInt (args . remove ( 0 ));
195
+ int count = Integer .parseInt (removeArgFor ( arg , args ));
160
196
if (count < 1 ) {
161
197
throw new CucumberException ("--count must be > 0" );
162
198
}
163
199
parsedOptions .setCount (count );
164
200
} else if (arg .equals ("--object-factory" )) {
165
- String objectFactoryClassName = args . remove ( 0 );
201
+ String objectFactoryClassName = removeArgFor ( arg , args );
166
202
parsedOptions .setObjectFactoryClass (parseObjectFactory (objectFactoryClassName ));
167
203
} else if (arg .startsWith ("-" )) {
168
204
printUsage ();
@@ -181,4 +217,12 @@ RuntimeOptionsBuilder parse(List<String> args) {
181
217
return parsedOptions ;
182
218
}
183
219
220
+ private String removeArgFor (String arg , List <String > args ) {
221
+ if (!args .isEmpty ()) {
222
+ return args .remove (0 );
223
+ }
224
+ printUsage ();
225
+ throw new CucumberException ("Missing argument for " + arg );
226
+ }
227
+
184
228
}
0 commit comments