@@ -216,22 +216,14 @@ public ParseResult parse(final String rawInput) {
216
216
217
217
// Ensure the user specified a value if the value is mandatory or
218
218
// key and value must appear in pair
219
- boolean mandatory = !StringUtils .hasText (value ) && cliOption .mandatory ();
220
- boolean specifiedKey = !StringUtils .hasText (value ) && options .containsKey (sourcedFrom );
221
- boolean specifiedKeyWithoutValue = false ;
222
- if (specifiedKey ) {
223
- value = cliOption .specifiedDefaultValue ();
224
- if ("__NULL__" .equals (value )) {
225
- specifiedKeyWithoutValue = true ;
226
- }
227
- }
228
- if (mandatory || specifiedKeyWithoutValue ) {
229
- if ("" .equals (cliOption .key ()[0 ])) {
230
- StringBuilder message = new StringBuilder ("You should specify a default option " );
231
- if (cliOption .key ().length > 1 ) {
232
- message .append ("(otherwise known as option '" ).append (cliOption .key ()[1 ]).append ("') " );
233
- }
234
- message .append ("for this command" );
219
+ boolean mandatory = cliOption .mandatory ();
220
+ boolean specifiedKey = options .containsKey (sourcedFrom );
221
+ boolean specifiedKeyWithoutValue = specifiedKey && "" .equals (value );
222
+ if (mandatory && (!specifiedKey || specifiedKeyWithoutValue )) {
223
+ if (isDefaultOption (cliOption )) {
224
+ StringBuilder message = new StringBuilder ("You should specify " );
225
+ message .append (optionAliases (cliOption ));
226
+ message .append (" for this command" );
235
227
LOGGER .warning (message .toString ());
236
228
}
237
229
else {
@@ -241,12 +233,10 @@ public ParseResult parse(final String rawInput) {
241
233
}
242
234
243
235
// Accept a default if the user specified the option, but didn't provide a value
244
- if ("" . equals ( value ) ) {
236
+ if (specifiedKeyWithoutValue ) {
245
237
value = cliOption .specifiedDefaultValue ();
246
- }
247
-
238
+ } else if (!specifiedKey ) {
248
239
// Accept a default if the user didn't specify the option at all
249
- if (value == null ) {
250
240
value = cliOption .unspecifiedDefaultValue ();
251
241
}
252
242
@@ -336,6 +326,42 @@ private void resetCompletionInvocations() {
336
326
successiveCompletionRequests = 1 ;
337
327
}
338
328
329
+ /**
330
+ * Return true if the given option is the 'default' option, that is, one of its key is the empty String.
331
+ */
332
+ private boolean isDefaultOption (CliOption option ) {
333
+ for (String key : option .key ()) {
334
+ if ("" .equals (key )) {
335
+ return true ;
336
+ }
337
+ }
338
+ return false ;
339
+ }
340
+
341
+ /**
342
+ * Return a plain string description of the available aliases for a given option.
343
+ */
344
+ private CharSequence optionAliases (CliOption option ) {
345
+ StringBuilder result = new StringBuilder ();
346
+ if (isDefaultOption (option )) {
347
+ result .append ("a default option" );
348
+ }
349
+ if (option .key ().length > 1 ) {
350
+ result .append (", otherwise known as " );
351
+ }
352
+ boolean comma = false ;
353
+ for (String key : option .key ()) {
354
+ if (!"" .equals (key )) {
355
+ if (comma ) {
356
+ result .append (", " );
357
+ }
358
+ result .append ("'" ).append (key ).append ("'" );
359
+ comma = true ;
360
+ }
361
+ }
362
+ return result ;
363
+ }
364
+
339
365
private void reportTokenizingException (String commandKey , TokenizingException te ) {
340
366
StringBuilder caret = new StringBuilder ();
341
367
for (int i = 0 ; i < te .getOffendingOffset () + commandKey .length () + 1 ; i ++) {
@@ -364,7 +390,7 @@ private void printHintMessage(final Set<CliOption> cliOptions, Map<String, Strin
364
390
boolean found = false ;
365
391
for (String key : keys ) {
366
392
if (options .containsKey (key )) {
367
- if (!StringUtils .hasText (options .get (key ))) {
393
+ if (!StringUtils .hasLength (options .get (key ))) {
368
394
valueBuilder .append (key );
369
395
valueBuilder .append ("' for this command" );
370
396
hintForOptions = false ;
0 commit comments