Skip to content

Commit 1d3197e

Browse files
committed
Sort members
1 parent 3485401 commit 1d3197e

3 files changed

Lines changed: 65 additions & 65 deletions

File tree

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,23 +109,23 @@ public Builder setPrintWriter(final PrintWriter printWriter) {
109109
/**
110110
* Sets whether to show deprecated options.
111111
*
112-
* @param useDefaultFormat if {@code true} use the default format, otherwise clear the formatter.
112+
* @param showDeprecatedFunc Specify the format for the deprecated options.
113113
* @return this.
114+
* @since 1.8.0
114115
*/
115-
public Builder setShowDeprecated(final boolean useDefaultFormat) {
116-
return setShowDeprecated(useDefaultFormat ? DEFAULT_DEPRECATED_FORMAT : null);
116+
public Builder setShowDeprecated(final BiFunction<String, Option, String> showDeprecatedFunc) {
117+
this.deprecatedFormatFunc = showDeprecatedFunc;
118+
return this;
117119
}
118120

119121
/**
120122
* Sets whether to show deprecated options.
121123
*
122-
* @param showDeprecatedFunc Specify the format for the deprecated options.
124+
* @param useDefaultFormat if {@code true} use the default format, otherwise clear the formatter.
123125
* @return this.
124-
* @since 1.8.0
125126
*/
126-
public Builder setShowDeprecated(final BiFunction<String, Option, String> showDeprecatedFunc) {
127-
this.deprecatedFormatFunc = showDeprecatedFunc;
128-
return this;
127+
public Builder setShowDeprecated(final boolean useDefaultFormat) {
128+
return setShowDeprecated(useDefaultFormat ? DEFAULT_DEPRECATED_FORMAT : null);
129129
}
130130
}
131131

src/test/java/org/apache/commons/cli/AbstractParserTestCase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ public void testMultiple() throws Exception {
421421
}
422422

423423
@Test
424-
public void testMultipleWithNull() throws Exception {
425-
final String[] args = { null, "-c", null, "foobar", null, "-b", null, "toast", null };
424+
public void testMultipleWithLong() throws Exception {
425+
final String[] args = { "--copt", "foobar", "--bfile", "toast" };
426426

427427
CommandLine cl = parser.parse(options, args, true);
428428
assertTrue(cl.hasOption("c"), "Confirm -c is set");
@@ -438,8 +438,8 @@ public void testMultipleWithNull() throws Exception {
438438
}
439439

440440
@Test
441-
public void testMultipleWithLong() throws Exception {
442-
final String[] args = { "--copt", "foobar", "--bfile", "toast" };
441+
public void testMultipleWithNull() throws Exception {
442+
final String[] args = { null, "-c", null, "foobar", null, "-b", null, "toast", null };
443443

444444
CommandLine cl = parser.parse(options, args, true);
445445
assertTrue(cl.hasOption("c"), "Confirm -c is set");

src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,39 @@ Licensed to the Apache Software Foundation (ASF) under one or more
4040
public class HelpFormatterTest {
4141
private static final String EOL = System.lineSeparator();
4242

43+
static Stream<Arguments> deprecatedOptionsProvider() {
44+
List<Arguments> lst = new ArrayList<>();
45+
Option option = Option.builder("a").longOpt("aaa").desc("dddd dddd dddd")
46+
.deprecated(DeprecatedAttributes.builder().setForRemoval(true).setSince("now")
47+
.setDescription("Why why why").get())
48+
.build();
49+
50+
HelpFormatter hf = HelpFormatter.builder().setShowDeprecated(false).get();
51+
lst.add(Arguments.of(hf, option, "dddd dddd dddd"));
52+
53+
hf = HelpFormatter.builder().setShowDeprecated(true).get();
54+
lst.add(Arguments.of(hf, option, "[Deprecated] dddd dddd dddd"));
55+
56+
hf = HelpFormatter.builder().setShowDeprecated((d, o) -> String.format("%s [%s]", d, o.getDeprecated())).get();
57+
lst.add(Arguments.of(hf, option, "dddd dddd dddd [Deprecated for removal since now: Why why why]"));
58+
59+
option = Option.builder("a").longOpt("aaa")
60+
.deprecated(DeprecatedAttributes.builder().setForRemoval(true).setSince("now")
61+
.setDescription("Why why why").get())
62+
.build();
63+
64+
hf = HelpFormatter.builder().setShowDeprecated(false).get();
65+
lst.add(Arguments.of(hf, option, ""));
66+
67+
hf = HelpFormatter.builder().setShowDeprecated(true).get();
68+
lst.add(Arguments.of(hf, option, "[Deprecated]"));
69+
70+
hf = HelpFormatter.builder().setShowDeprecated((d, o) -> String.format("%s [%s]", d, o.getDeprecated())).get();
71+
lst.add(Arguments.of(hf, option, "[Deprecated for removal since now: Why why why]"));
72+
73+
return lst.stream();
74+
}
75+
4376
@Test
4477
public void testAccessors() {
4578
final HelpFormatter formatter = new HelpFormatter();
@@ -366,6 +399,26 @@ public void testOptionWithoutShortFormat2() {
366399
//@formatter:on
367400
}
368401

402+
@ParameterizedTest
403+
@MethodSource("deprecatedOptionsProvider")
404+
public void testPrintDeprecatedOptions(final HelpFormatter hf, final Option option, final String expectedTxt) {
405+
final StringBuffer sb = new StringBuffer();
406+
407+
final int leftPad = 1;
408+
final int descPad = 3;
409+
final String lpad = hf.createPadding(leftPad);
410+
final String dpad = hf.createPadding(descPad);
411+
Options options;
412+
String expected = lpad + "-a,--aaa";
413+
414+
options = new Options().addOption(option);
415+
if (expectedTxt.length() > 0) {
416+
expected = expected + dpad + expectedTxt;
417+
}
418+
hf.renderOptions(sb, 160, options, leftPad, descPad);
419+
assertEquals(expected, sb.toString());
420+
}
421+
369422
@Test
370423
public void testPrintHelpNewlineFooter() {
371424
final HelpFormatter formatter = new HelpFormatter();
@@ -490,59 +543,6 @@ public void testPrintOptions() {
490543
assertEquals(expected, sb.toString(), "multiple wrapped options");
491544
}
492545

493-
@ParameterizedTest
494-
@MethodSource("deprecatedOptionsProvider")
495-
public void testPrintDeprecatedOptions(final HelpFormatter hf, final Option option, final String expectedTxt) {
496-
final StringBuffer sb = new StringBuffer();
497-
498-
final int leftPad = 1;
499-
final int descPad = 3;
500-
final String lpad = hf.createPadding(leftPad);
501-
final String dpad = hf.createPadding(descPad);
502-
Options options;
503-
String expected = lpad + "-a,--aaa";
504-
505-
options = new Options().addOption(option);
506-
if (expectedTxt.length() > 0) {
507-
expected = expected + dpad + expectedTxt;
508-
}
509-
hf.renderOptions(sb, 160, options, leftPad, descPad);
510-
assertEquals(expected, sb.toString());
511-
}
512-
513-
static Stream<Arguments> deprecatedOptionsProvider() {
514-
List<Arguments> lst = new ArrayList<>();
515-
Option option = Option.builder("a").longOpt("aaa").desc("dddd dddd dddd")
516-
.deprecated(DeprecatedAttributes.builder().setForRemoval(true).setSince("now")
517-
.setDescription("Why why why").get())
518-
.build();
519-
520-
HelpFormatter hf = HelpFormatter.builder().setShowDeprecated(false).get();
521-
lst.add(Arguments.of(hf, option, "dddd dddd dddd"));
522-
523-
hf = HelpFormatter.builder().setShowDeprecated(true).get();
524-
lst.add(Arguments.of(hf, option, "[Deprecated] dddd dddd dddd"));
525-
526-
hf = HelpFormatter.builder().setShowDeprecated((d, o) -> String.format("%s [%s]", d, o.getDeprecated())).get();
527-
lst.add(Arguments.of(hf, option, "dddd dddd dddd [Deprecated for removal since now: Why why why]"));
528-
529-
option = Option.builder("a").longOpt("aaa")
530-
.deprecated(DeprecatedAttributes.builder().setForRemoval(true).setSince("now")
531-
.setDescription("Why why why").get())
532-
.build();
533-
534-
hf = HelpFormatter.builder().setShowDeprecated(false).get();
535-
lst.add(Arguments.of(hf, option, ""));
536-
537-
hf = HelpFormatter.builder().setShowDeprecated(true).get();
538-
lst.add(Arguments.of(hf, option, "[Deprecated]"));
539-
540-
hf = HelpFormatter.builder().setShowDeprecated((d, o) -> String.format("%s [%s]", d, o.getDeprecated())).get();
541-
lst.add(Arguments.of(hf, option, "[Deprecated for removal since now: Why why why]"));
542-
543-
return lst.stream();
544-
}
545-
546546
@Test
547547
public void testPrintOptionWithEmptyArgNameUsage() {
548548
final Option option = new Option("f", true, null);

0 commit comments

Comments
 (0)