Skip to content

Commit 9136957

Browse files
committed
Merge branch 'master' into release
2 parents c4d8376 + 757934a commit 9136957

8 files changed

Lines changed: 167 additions & 154 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
<commons.release.next>1.8.1</commons.release.next>
7575
<commons.release.name>commons-cli-${commons.release.version}</commons.release.name>
7676
<commons.release.isDistModule>true</commons.release.isDistModule>
77-
<commons.rc.version>RC1</commons.rc.version>
77+
<commons.rc.version>RC2</commons.rc.version>
7878
<commons.bc.version>1.7.0</commons.bc.version>
7979
<commons.osgi.symbolicName>org.apache.commons.cli</commons.osgi.symbolicName>
8080
<commons.jira.id>CLI</commons.jira.id>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private void handleProperties(final Properties properties) throws ParseException
449449
final String value = properties.getProperty(option);
450450

451451
if (opt.hasArg()) {
452-
if (opt.getValues() == null || opt.getValues().length == 0) {
452+
if (Util.isEmpty(opt.getValues())) {
453453
opt.processValue(stripLeadingAndTrailingQuotesDefaultOff(value));
454454
}
455455
} else if (!("yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value) || "1".equalsIgnoreCase(value))) {

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3030
import java.util.Iterator;
3131
import java.util.List;
3232
import java.util.Objects;
33-
import java.util.function.BiFunction;
33+
import java.util.function.Function;
3434
import java.util.function.Supplier;
3535

3636
/**
@@ -78,12 +78,12 @@ public static final class Builder implements Supplier<HelpFormatter> {
7878
/**
7979
* A function to convert a description (not null) and a deprecated Option (not null) to help description
8080
*/
81-
private static final BiFunction<String, Option, String> DEFAULT_DEPRECATED_FORMAT = (d, o) -> "[Deprecated] " + d;
81+
private static final Function<Option, String> DEFAULT_DEPRECATED_FORMAT = o -> "[Deprecated] " + getDescription(o);
8282

8383
/**
8484
* Formatter for deprecated options.
8585
*/
86-
private BiFunction<String, Option, String> deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT;
86+
private Function<Option, String> deprecatedFormatFunc = DEFAULT_DEPRECATED_FORMAT;
8787

8888
/**
8989
* The output PrintWriter, defaults to wrapping {@link System#out}.
@@ -109,23 +109,23 @@ public Builder setPrintWriter(final PrintWriter printWriter) {
109109
/**
110110
* Sets whether to show deprecated options.
111111
*
112-
* @param showDeprecatedFunc Specify the format for the deprecated options.
112+
* @param useDefaultFormat if {@code true} use the default format, otherwise clear the formatter.
113113
* @return this.
114-
* @since 1.8.0
115114
*/
116-
public Builder setShowDeprecated(final BiFunction<String, Option, String> showDeprecatedFunc) {
117-
this.deprecatedFormatFunc = showDeprecatedFunc;
118-
return this;
115+
public Builder setShowDeprecated(final boolean useDefaultFormat) {
116+
return setShowDeprecated(useDefaultFormat ? DEFAULT_DEPRECATED_FORMAT : null);
119117
}
120118

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

@@ -151,7 +151,6 @@ public int compare(final Option opt1, final Option opt2) {
151151
return opt1.getKey().compareToIgnoreCase(opt2.getKey());
152152
}
153153
}
154-
155154
/** Default number of characters per line */
156155
public static final int DEFAULT_WIDTH = 74;
157156

@@ -194,6 +193,17 @@ private static PrintWriter createDefaultPrintWriter() {
194193
return new PrintWriter(System.out);
195194
}
196195

196+
/**
197+
* Gets the option description or an empty string if the description is {@code null}.
198+
* @param option The option to get the description from.
199+
* @return the option description or an empty string if the description is {@code null}.
200+
* @since 1.8.0
201+
*/
202+
public static String getDescription(final Option option) {
203+
final String desc = option.getDescription();
204+
return desc == null ? "" : desc;
205+
}
206+
197207
/**
198208
* Number of characters per line
199209
*
@@ -266,9 +276,9 @@ private static PrintWriter createDefaultPrintWriter() {
266276
protected Comparator<Option> optionComparator = new OptionComparator();
267277

268278
/**
269-
* BiFunction to format the description for a deprecated option.
279+
* Function to format the description for a deprecated option.
270280
*/
271-
private final BiFunction<String, Option, String> deprecatedFormatFunc;
281+
private final Function<Option, String> deprecatedFormatFunc;
272282

273283
/**
274284
* Where to print help.
@@ -291,7 +301,7 @@ public HelpFormatter() {
291301
* Constructs a new instance.
292302
* @param printStream TODO
293303
*/
294-
private HelpFormatter(final BiFunction<String, Option, String> deprecatedFormatFunc, final PrintWriter printStream) {
304+
private HelpFormatter(final Function<Option, String> deprecatedFormatFunc, final PrintWriter printStream) {
295305
// TODO All other instance HelpFormatter instance variables.
296306
// Make HelpFormatter immutable for 2.0
297307
this.deprecatedFormatFunc = deprecatedFormatFunc;
@@ -570,7 +580,7 @@ public void printHelp(final PrintWriter pw, final int width, final String cmdLin
570580
*/
571581
public void printHelp(final PrintWriter pw, final int width, final String cmdLineSyntax, final String header, final Options options, final int leftPad,
572582
final int descPad, final String footer, final boolean autoUsage) {
573-
if (cmdLineSyntax == null || cmdLineSyntax.isEmpty()) {
583+
if (Util.isEmpty(cmdLineSyntax)) {
574584
throw new IllegalArgumentException("cmdLineSyntax not provided");
575585
}
576586
if (autoUsage) {
@@ -795,7 +805,7 @@ protected StringBuffer renderOptions(final StringBuffer sb, final int width, fin
795805
optBuf.append(dpad);
796806
final int nextLineTabStop = max + descPad;
797807
if (deprecatedFormatFunc != null && option.isDeprecated()) {
798-
optBuf.append(deprecatedFormatFunc.apply(option.getDescription() == null ? "" : option.getDescription(), option).trim());
808+
optBuf.append(deprecatedFormatFunc.apply(option).trim());
799809
} else if (option.getDescription() != null) {
800810
optBuf.append(option.getDescription());
801811
}
@@ -881,7 +891,7 @@ private Appendable renderWrappedTextBlock(final StringBuffer sb, final int width
881891
* @return The String of without the trailing padding
882892
*/
883893
protected String rtrim(final String s) {
884-
if (s == null || s.isEmpty()) {
894+
if (Util.isEmpty(s)) {
885895
return s;
886896
}
887897
int pos = s.length();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,6 @@ public String getValue(final int index) throws IndexOutOfBoundsException {
684684
*/
685685
public String getValue(final String defaultValue) {
686686
final String value = getValue();
687-
688687
return value != null ? value : defaultValue;
689688
}
690689

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected void processProperties(final Properties properties) throws ParseExcept
278278
// get the value from the properties instance
279279
final String value = properties.getProperty(option);
280280
if (opt.hasArg()) {
281-
if (opt.getValues() == null || opt.getValues().length == 0) {
281+
if (Util.isEmpty(opt.getValues())) {
282282
try {
283283
opt.processValue(value);
284284
} catch (final RuntimeException exp) { // NOPMD

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,23 @@ final class Util {
2727
*/
2828
static final String[] EMPTY_STRING_ARRAY = {};
2929

30+
/**
31+
* Tests whether the given array is null or empty.
32+
*
33+
* @param array the array to test.
34+
* @return the given array is null or empty.
35+
*/
36+
static boolean isEmpty(final Object[] array) {
37+
return array == null || array.length == 0;
38+
}
39+
3040
/**
3141
* Tests whether the given string is null or empty.
3242
*
3343
* @param str The string to test.
3444
* @return Whether the given string is null or empty.
3545
*/
36-
private static boolean isEmpty(final String str) {
46+
static boolean isEmpty(final String str) {
3747
return str == null || str.isEmpty();
3848
}
3949

0 commit comments

Comments
 (0)