Skip to content

Commit 5db0035

Browse files
committed
Add FinalParameters
1 parent 3b78b1a commit 5db0035

7 files changed

Lines changed: 51 additions & 62 deletions

File tree

src/conf/checkstyle.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<!-- Checks for Naming Conventions. -->
4646
<!-- See http://checkstyle.sf.net/config_naming.html -->
4747
<module name="ConstantName" />
48+
<module name="FinalParameters"/>
4849
<module name="LocalFinalVariableName" />
4950
<module name="LocalVariableName" />
5051
<module name="MemberName" />

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,18 +389,18 @@ private void handleLongOptionWithoutEqual(final String token) throws ParseExcept
389389
}
390390
}
391391

392-
private void handleOption(Option option) throws ParseException {
392+
private void handleOption(final Option option) throws ParseException {
393393
// check the previous option before handling the next one
394394
checkRequiredArgs();
395395

396-
option = (Option) option.clone();
396+
final Option copy = (Option) option.clone();
397397

398-
updateRequiredOptions(option);
398+
updateRequiredOptions(copy);
399399

400-
cmd.addOption(option);
400+
cmd.addOption(copy);
401401

402-
if (option.hasArg()) {
403-
currentOption = option;
402+
if (copy.hasArg()) {
403+
currentOption = copy;
404404
} else {
405405
currentOption = null;
406406
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -749,39 +749,41 @@ protected StringBuffer renderOptions(final StringBuffer sb, final int width, fin
749749
*
750750
* @return the StringBuffer with the rendered Options contents.
751751
*/
752-
protected StringBuffer renderWrappedText(final StringBuffer sb, final int width, int nextLineTabStop, String text) {
753-
int pos = findWrapPos(text, width, 0);
752+
protected StringBuffer renderWrappedText(final StringBuffer sb, final int width, final int nextLineTabStop, final String text) {
753+
String render = text;
754+
int nextLineTabStopPos = nextLineTabStop;
755+
int pos = findWrapPos(render, width, 0);
754756

755757
if (pos == -1) {
756-
sb.append(rtrim(text));
758+
sb.append(rtrim(render));
757759

758760
return sb;
759761
}
760-
sb.append(rtrim(text.substring(0, pos))).append(getNewLine());
762+
sb.append(rtrim(render.substring(0, pos))).append(getNewLine());
761763

762-
if (nextLineTabStop >= width) {
764+
if (nextLineTabStopPos >= width) {
763765
// stops infinite loop happening
764-
nextLineTabStop = 1;
766+
nextLineTabStopPos = 1;
765767
}
766768

767769
// all following lines must be padded with nextLineTabStop space characters
768-
final String padding = createPadding(nextLineTabStop);
770+
final String padding = createPadding(nextLineTabStopPos);
769771

770772
while (true) {
771-
text = padding + text.substring(pos).trim();
772-
pos = findWrapPos(text, width, 0);
773+
render = padding + render.substring(pos).trim();
774+
pos = findWrapPos(render, width, 0);
773775

774776
if (pos == -1) {
775-
sb.append(text);
777+
sb.append(render);
776778

777779
return sb;
778780
}
779781

780-
if (text.length() > width && pos == nextLineTabStop - 1) {
782+
if (render.length() > width && pos == nextLineTabStopPos - 1) {
781783
pos = width;
782784
}
783785

784-
sb.append(rtrim(text.substring(0, pos))).append(getNewLine());
786+
sb.append(rtrim(render.substring(0, pos))).append(getNewLine());
785787
}
786788
}
787789

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -729,14 +729,15 @@ public boolean isRequired() {
729729
*
730730
* @since 1.0.1
731731
*/
732-
private void processValue(String value) {
732+
private void processValue(final String value) {
733+
String add = value;
733734
// this Option has a separator character
734735
if (hasValueSeparator()) {
735736
// get the separator character
736737
final char sep = getValueSeparator();
737738

738739
// store the index for the value separator
739-
int index = value.indexOf(sep);
740+
int index = add.indexOf(sep);
740741

741742
// while there are more value separators
742743
while (index != -1) {
@@ -746,18 +747,18 @@ private void processValue(String value) {
746747
}
747748

748749
// store
749-
add(value.substring(0, index));
750+
add(add.substring(0, index));
750751

751752
// parse
752-
value = value.substring(index + 1);
753+
add = add.substring(index + 1);
753754

754755
// get new index
755-
index = value.indexOf(sep);
756+
index = add.indexOf(sep);
756757
}
757758
}
758759

759760
// store the actual value or the last value that has been parsed
760-
add(value);
761+
add(add);
761762
}
762763

763764
/**

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,18 @@ public Options addRequiredOption(final String opt, final String longOpt, final b
193193
* @return the options matching the partial name specified, or an empty list if none matches
194194
* @since 1.3
195195
*/
196-
public List<String> getMatchingOptions(String opt) {
197-
opt = Util.stripLeadingHyphens(opt);
198-
196+
public List<String> getMatchingOptions(final String opt) {
197+
final String clean = Util.stripLeadingHyphens(opt);
199198
final List<String> matchingOpts = new ArrayList<>();
200-
201199
// for a perfect match return the single option only
202-
if (longOpts.containsKey(opt)) {
203-
return Collections.singletonList(opt);
200+
if (longOpts.containsKey(clean)) {
201+
return Collections.singletonList(clean);
204202
}
205-
206203
for (final String longOpt : longOpts.keySet()) {
207-
if (longOpt.startsWith(opt)) {
204+
if (longOpt.startsWith(clean)) {
208205
matchingOpts.add(longOpt);
209206
}
210207
}
211-
212208
return matchingOpts;
213209
}
214210

@@ -222,11 +218,10 @@ public List<String> getMatchingOptions(String opt) {
222218
* @param opt short or long name of the {@link Option}
223219
* @return the option represented by opt
224220
*/
225-
public Option getOption(String opt) {
226-
opt = Util.stripLeadingHyphens(opt);
227-
228-
final Option option = shortOpts.get(opt);
229-
return option != null ? option : longOpts.get(opt);
221+
public Option getOption(final String opt) {
222+
final String clean = Util.stripLeadingHyphens(opt);
223+
final Option option = shortOpts.get(clean);
224+
return option != null ? option : longOpts.get(clean);
230225
}
231226

232227
/**
@@ -273,10 +268,9 @@ public List getRequiredOptions() {
273268
* @return true if the named {@link Option} is a member of this {@link Options}
274269
* @since 1.3
275270
*/
276-
public boolean hasLongOption(String opt) {
277-
opt = Util.stripLeadingHyphens(opt);
278-
279-
return longOpts.containsKey(opt);
271+
public boolean hasLongOption(final String opt) {
272+
final String clean = Util.stripLeadingHyphens(opt);
273+
return longOpts.containsKey(clean);
280274
}
281275

282276
/**
@@ -285,10 +279,9 @@ public boolean hasLongOption(String opt) {
285279
* @param opt short or long name of the {@link Option}
286280
* @return true if the named {@link Option} is a member of this {@link Options}
287281
*/
288-
public boolean hasOption(String opt) {
289-
opt = Util.stripLeadingHyphens(opt);
290-
291-
return shortOpts.containsKey(opt) || longOpts.containsKey(opt);
282+
public boolean hasOption(final String opt) {
283+
final String clean = Util.stripLeadingHyphens(opt);
284+
return shortOpts.containsKey(clean) || longOpts.containsKey(clean);
292285
}
293286

294287
/**
@@ -298,10 +291,9 @@ public boolean hasOption(String opt) {
298291
* @return true if the named {@link Option} is a member of this {@link Options}
299292
* @since 1.3
300293
*/
301-
public boolean hasShortOption(String opt) {
302-
opt = Util.stripLeadingHyphens(opt);
303-
304-
return shortOpts.containsKey(opt);
294+
public boolean hasShortOption(final String opt) {
295+
final String clean = Util.stripLeadingHyphens(opt);
296+
return shortOpts.containsKey(clean);
305297
}
306298

307299
/**
@@ -321,13 +313,11 @@ List<Option> helpOptions() {
321313
@Override
322314
public String toString() {
323315
final StringBuilder buf = new StringBuilder();
324-
325316
buf.append("[ Options: [ short ");
326317
buf.append(shortOpts.toString());
327318
buf.append(" ] [ long ");
328319
buf.append(longOpts);
329320
buf.append(" ]");
330-
331321
return buf.toString();
332322
}
333323
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public CommandLine parse(final Options options, final String[] arguments, final
141141
*
142142
* @since 1.1
143143
*/
144-
public CommandLine parse(final Options options, String[] arguments, final Properties properties, final boolean stopAtNonOption) throws ParseException {
144+
public CommandLine parse(final Options options, final String[] arguments, final Properties properties, final boolean stopAtNonOption)
145+
throws ParseException {
145146
// clear out the data in options in case it's been used before (CLI-71)
146147
for (final Option opt : options.helpOptions()) {
147148
opt.clearValues();
@@ -159,11 +160,7 @@ public CommandLine parse(final Options options, String[] arguments, final Proper
159160

160161
boolean eatTheRest = false;
161162

162-
if (arguments == null) {
163-
arguments = new String[0];
164-
}
165-
166-
final List<String> tokenList = Arrays.asList(flatten(getOptions(), arguments, stopAtNonOption));
163+
final List<String> tokenList = Arrays.asList(flatten(getOptions(), arguments == null ? new String[0] : arguments, stopAtNonOption));
167164

168165
final ListIterator<String> iterator = tokenList.listIterator();
169166

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ final class Util {
3333
* @param str The string from which the leading and trailing quotes should be removed.
3434
* @return The string without the leading and trailing quotes.
3535
*/
36-
static String stripLeadingAndTrailingQuotes(String str) {
36+
static String stripLeadingAndTrailingQuotes(final String str) {
3737
final int length = str.length();
3838
if (length > 1 && str.startsWith("\"") && str.endsWith("\"") && str.substring(1, length - 1).indexOf('"') == -1) {
39-
str = str.substring(1, length - 1);
39+
return str.substring(1, length - 1);
4040
}
41-
4241
return str;
4342
}
4443

@@ -58,7 +57,6 @@ static String stripLeadingHyphens(final String str) {
5857
if (str.startsWith("-")) {
5958
return str.substring(1);
6059
}
61-
6260
return str;
6361
}
6462
}

0 commit comments

Comments
 (0)