Skip to content

Commit 12124d4

Browse files
authored
[CLI-336] Deprecation not always reported (#284)
* added methods with OptionGroup args + tests * Updated OptionGroup documentation to indicate where deprecation is no reported * Added tests * added tests + cleaned up checkstyle * added more tests
1 parent 89f6471 commit 12124d4

3 files changed

Lines changed: 856 additions & 155 deletions

File tree

src/main/java/org/apache/commons/cli/CommandLine.java

Lines changed: 123 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public String getOptionValue(final char opt, final Supplier<String> defaultValue
314314
/**
315315
* Gets the first argument, if any, of this option.
316316
*
317-
* @param option the name of the option.
317+
* @param option the option.
318318
* @return Value of the argument if option is set, and has an argument, otherwise null.
319319
* @since 1.5.0
320320
*/
@@ -326,7 +326,7 @@ public String getOptionValue(final Option option) {
326326
/**
327327
* Gets the first argument, if any, of an option.
328328
*
329-
* @param option name of the option.
329+
* @param option the option.
330330
* @param defaultValue is the default value to be returned if the option is not specified.
331331
* @return Value of the argument if option is set, and has an argument, otherwise {@code defaultValue}.
332332
* @since 1.5.0
@@ -338,7 +338,7 @@ public String getOptionValue(final Option option, final String defaultValue) {
338338
/**
339339
* Gets the first argument, if any, of an option.
340340
*
341-
* @param option name of the option.
341+
* @param option the option.
342342
* @param defaultValue is a supplier for the default value to be returned if the option is not specified.
343343
* @return Value of the argument if option is set, and has an argument, otherwise {@code defaultValue}.
344344
* @since 1.7.0
@@ -348,6 +348,44 @@ public String getOptionValue(final Option option, final Supplier<String> default
348348
return answer != null ? answer : get(defaultValue);
349349
}
350350

351+
/**
352+
* Gets the first argument, if any, of this option group.
353+
*
354+
* @param optionGroup the option group.
355+
* @return Value of the argument if option group is selected, and has an argument, otherwise null.
356+
* @since 1.9.0
357+
*/
358+
public String getOptionValue(final OptionGroup optionGroup) {
359+
final String[] values = getOptionValues(optionGroup);
360+
return values == null ? null : values[0];
361+
}
362+
363+
/**
364+
* Gets the first argument, if any, of an option group.
365+
*
366+
* @param optionGroup the option group.
367+
* @param defaultValue is the default value to be returned if the option group is not selected.
368+
* @return Value of the argument if option group is selected, and has an argument, otherwise {@code defaultValue}.
369+
* @since 1.9.0
370+
*/
371+
public String getOptionValue(final OptionGroup optionGroup, final String defaultValue) {
372+
return getOptionValue(optionGroup, () -> defaultValue);
373+
}
374+
375+
/**
376+
* Gets the first argument, if any, of an option group.
377+
*
378+
* @param optionGroup the option group..
379+
* @param defaultValue is a supplier for the default value to be returned if the option group is not selected.
380+
* @return Value of the argument if option group is selected, and has an argument, otherwise {@code defaultValue}.
381+
* @since 1.9.0
382+
*/
383+
public String getOptionValue(final OptionGroup optionGroup, final Supplier<String> defaultValue) {
384+
final String answer = getOptionValue(optionGroup);
385+
return answer != null ? answer : get(defaultValue);
386+
}
387+
388+
351389
/**
352390
* Gets the first argument, if any, of this option.
353391
*
@@ -395,26 +433,40 @@ public String[] getOptionValues(final char opt) {
395433
/**
396434
* Gets the array of values, if any, of an option.
397435
*
398-
* @param option string name of the option.
436+
* @param option the option.
399437
* @return Values of the argument if option is set, and has an argument, otherwise null.
400438
* @since 1.5.0
401439
*/
402440
public String[] getOptionValues(final Option option) {
403441
if (option == null) {
404442
return null;
405443
}
406-
if (option.isDeprecated()) {
407-
handleDeprecated(option);
408-
}
409444
final List<String> values = new ArrayList<>();
410445
for (final Option processedOption : options) {
411446
if (processedOption.equals(option)) {
447+
if (option.isDeprecated()) {
448+
handleDeprecated(option);
449+
}
412450
values.addAll(processedOption.getValuesList());
413451
}
414452
}
415453
return values.isEmpty() ? null : values.toArray(Util.EMPTY_STRING_ARRAY);
416454
}
417455

456+
/**
457+
* Gets the array of values, if any, of an option group.
458+
*
459+
* @param optionGroup the option group.
460+
* @return Values of the argument if option group is selected, and has an argument, otherwise null.
461+
* @since 1.9.0
462+
*/
463+
public String[] getOptionValues(final OptionGroup optionGroup) {
464+
if (optionGroup == null || !optionGroup.isSelected()) {
465+
return null;
466+
}
467+
return getOptionValues(optionGroup.getSelected());
468+
}
469+
418470
/**
419471
* Gets the array of values, if any, of an option.
420472
*
@@ -472,7 +524,7 @@ public <T> T getParsedOptionValue(final char opt, final T defaultValue) throws P
472524
/**
473525
* Gets a version of this {@code Option} converted to a particular type.
474526
*
475-
* @param option the name of the option.
527+
* @param option the option.
476528
* @param <T> The return type for the method.
477529
* @return the value parsed into a particular object.
478530
* @throws ParseException if there are problems turning the option value into the desired type
@@ -486,7 +538,7 @@ public <T> T getParsedOptionValue(final Option option) throws ParseException {
486538
/**
487539
* Gets a version of this {@code Option} converted to a particular type.
488540
*
489-
* @param option the name of the option.
541+
* @param option the option.
490542
* @param defaultValue the default value to return if opt is not set.
491543
* @param <T> The return type for the method.
492544
* @return the value parsed into a particular object.
@@ -513,7 +565,7 @@ public <T> T getParsedOptionValue(final Option option, final Supplier<T> default
513565
/**
514566
* Gets a version of this {@code Option} converted to a particular type.
515567
*
516-
* @param option the name of the option.
568+
* @param option the option.
517569
* @param defaultValue the default value to return if opt is not set.
518570
* @param <T> The return type for the method.
519571
* @return the value parsed into a particular object.
@@ -525,6 +577,53 @@ public <T> T getParsedOptionValue(final Option option, final T defaultValue) thr
525577
return getParsedOptionValue(option, () -> defaultValue);
526578
}
527579

580+
/**
581+
* Gets a version of this {@code OptionGroup} converted to a particular type.
582+
*
583+
* @param optionGroup the option group.
584+
* @param <T> The return type for the method.
585+
* @return the value parsed into a particular object.
586+
* @throws ParseException if there are problems turning the selected option value into the desired type
587+
* @see PatternOptionBuilder
588+
* @since 1.9.0
589+
*/
590+
public <T> T getParsedOptionValue(final OptionGroup optionGroup) throws ParseException {
591+
return getParsedOptionValue(optionGroup, () -> null);
592+
}
593+
594+
/**
595+
* Gets a version of this {@code OptionGroup} converted to a particular type.
596+
*
597+
* @param optionGroup the option group.
598+
* @param defaultValue the default value to return if opt is not set.
599+
* @param <T> The return type for the method.
600+
* @return the value parsed into a particular object.
601+
* @throws ParseException if there are problems turning the selected option value into the desired type
602+
* @see PatternOptionBuilder
603+
* @since 1.9.0
604+
*/
605+
public <T> T getParsedOptionValue(final OptionGroup optionGroup, final Supplier<T> defaultValue) throws ParseException {
606+
if (optionGroup == null || !optionGroup.isSelected()) {
607+
return get(defaultValue);
608+
}
609+
return getParsedOptionValue(optionGroup.getSelected(), defaultValue);
610+
}
611+
612+
/**
613+
* Gets a version of this {@code OptionGroup} converted to a particular type.
614+
*
615+
* @param optionGroup the option group.
616+
* @param defaultValue the default value to return if an option is not selected.
617+
* @param <T> The return type for the method.
618+
* @return the value parsed into a particular object.
619+
* @throws ParseException if there are problems turning the option value into the desired type
620+
* @see PatternOptionBuilder
621+
* @since 1.9.0
622+
*/
623+
public <T> T getParsedOptionValue(final OptionGroup optionGroup, final T defaultValue) throws ParseException {
624+
return getParsedOptionValue(optionGroup, () -> defaultValue);
625+
}
626+
528627
/**
529628
* Gets a version of this {@code Option} converted to a particular type.
530629
*
@@ -623,6 +722,20 @@ public boolean hasOption(final Option opt) {
623722
return result;
624723
}
625724

725+
/**
726+
* Tests to see if an option has been set.
727+
*
728+
* @param optionGroup the option group to check.
729+
* @return true if set, false if not.
730+
* @since 1.9.0
731+
*/
732+
public boolean hasOption(final OptionGroup optionGroup) {
733+
if (optionGroup == null || !optionGroup.isSelected()) {
734+
return false;
735+
}
736+
return hasOption(optionGroup.getSelected());
737+
}
738+
626739
/**
627740
* Tests to see if an option has been set.
628741
*

src/main/java/org/apache/commons/cli/OptionGroup.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public Collection<Option> getOptions() {
7676
/**
7777
* Gets the selected option name.
7878
*
79+
* If the selected option is deprecated <em>no warning is logged</em>.
7980
* @return the selected option name.
8081
*/
8182
public String getSelected() {
@@ -94,6 +95,7 @@ public boolean isRequired() {
9495
/**
9596
* Tests whether an option is selected.
9697
*
98+
* If an option is selected and is deprecated <em>no warning is logged</em>.
9799
* @return whether whether an option is selected.
98100
* @since 1.9.0
99101
*/
@@ -113,6 +115,7 @@ public void setRequired(final boolean required) {
113115
/**
114116
* Sets the selected option of this group to {@code name}.
115117
*
118+
* If the selected option is deprecated <em>no warning is logged</em>.
116119
* @param option the option that is selected
117120
* @throws AlreadySelectedException if an option from this group has already been selected.
118121
*/

0 commit comments

Comments
 (0)