Skip to content

Commit a8a8ec0

Browse files
author
Robert James Oxspring
committed
Lots of minor refactoring and tidying to avoid compiler warnings
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@155293 13f79535-47bb-0310-9956-ffa450edef68
1 parent 16431d8 commit a8a8ec0

37 files changed

Lines changed: 291 additions & 346 deletions

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class CommandLine {
5959
*/
6060
CommandLine()
6161
{
62+
// nothing to do
6263
}
6364

6465
/**
@@ -149,8 +150,6 @@ public String getOptionValue(char opt)
149150
*/
150151
public String[] getOptionValues(String opt)
151152
{
152-
List values = new java.util.ArrayList();
153-
154153
opt = Util.stripLeadingHyphens(opt);
155154

156155
String key = opt;

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,7 @@ protected StringBuffer renderWrappedText(StringBuffer sb, int width,
751751

752752
return sb;
753753
}
754-
else
755-
{
756-
sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
757-
}
754+
sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
758755

759756
// all following lines must be padded with nextLineTabStop space
760757
// characters
@@ -821,20 +818,18 @@ else if ((startPos + width) >= text.length())
821818
{
822819
return pos;
823820
}
824-
else
825-
{
826-
// must look for the first whitespace chearacter after startPos
827-
// + width
828-
pos = startPos + width;
829-
830-
while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ')
831-
&& (c != '\n') && (c != '\r'))
832-
{
833-
++pos;
834-
}
821+
822+
// must look for the first whitespace chearacter after startPos
823+
// + width
824+
pos = startPos + width;
835825

836-
return (pos == text.length()) ? (-1) : pos;
826+
while ((pos <= text.length()) && ((c = text.charAt(pos)) != ' ')
827+
&& (c != '\n') && (c != '\r'))
828+
{
829+
++pos;
837830
}
831+
832+
return (pos == text.length()) ? (-1) : pos;
838833
}
839834

840835
/**

src/java/org/apache/commons/cli/OptionBuilder.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class OptionBuilder {
5959
*/
6060
private OptionBuilder()
6161
{
62+
// hide the constructor
6263
}
6364

6465
/**
@@ -82,12 +83,12 @@ private static void reset()
8283
/**
8384
* The next Option created will have the following long option value.
8485
*
85-
* @param longopt the long option value
86+
* @param newLongopt the long option value
8687
* @return the OptionBuilder instance
8788
*/
88-
public static OptionBuilder withLongOpt(String longopt)
89+
public static OptionBuilder withLongOpt(String newLongopt)
8990
{
90-
instance.longopt = longopt;
91+
OptionBuilder.longopt = newLongopt;
9192

9293
return instance;
9394
}
@@ -99,7 +100,7 @@ public static OptionBuilder withLongOpt(String longopt)
99100
*/
100101
public static OptionBuilder hasArg()
101102
{
102-
instance.numberOfArgs = 1;
103+
OptionBuilder.numberOfArgs = 1;
103104

104105
return instance;
105106
}
@@ -113,7 +114,7 @@ public static OptionBuilder hasArg()
113114
*/
114115
public static OptionBuilder hasArg(boolean hasArg)
115116
{
116-
instance.numberOfArgs = (hasArg == true) ? 1 : Option.UNINITIALIZED;
117+
OptionBuilder.numberOfArgs = (hasArg == true) ? 1 : Option.UNINITIALIZED;
117118

118119
return instance;
119120
}
@@ -127,7 +128,7 @@ public static OptionBuilder hasArg(boolean hasArg)
127128
*/
128129
public static OptionBuilder withArgName(String name)
129130
{
130-
instance.argName = name;
131+
OptionBuilder.argName = name;
131132

132133
return instance;
133134
}
@@ -139,7 +140,7 @@ public static OptionBuilder withArgName(String name)
139140
*/
140141
public static OptionBuilder isRequired()
141142
{
142-
instance.required = true;
143+
OptionBuilder.required = true;
143144

144145
return instance;
145146
}
@@ -164,7 +165,7 @@ public static OptionBuilder isRequired()
164165
*/
165166
public static OptionBuilder withValueSeparator(char sep)
166167
{
167-
instance.valuesep = sep;
168+
OptionBuilder.valuesep = sep;
168169

169170
return instance;
170171
}
@@ -187,7 +188,7 @@ public static OptionBuilder withValueSeparator(char sep)
187188
*/
188189
public static OptionBuilder withValueSeparator()
189190
{
190-
instance.valuesep = '=';
191+
OptionBuilder.valuesep = '=';
191192

192193
return instance;
193194
}
@@ -196,12 +197,12 @@ public static OptionBuilder withValueSeparator()
196197
* The next Option created will be required if <code>required</code>
197198
* is true.
198199
*
199-
* @param required if true then the Option is required
200+
* @param newRequired if true then the Option is required
200201
* @return the OptionBuilder instance
201202
*/
202-
public static OptionBuilder isRequired(boolean required)
203+
public static OptionBuilder isRequired(boolean newRequired)
203204
{
204-
instance.required = required;
205+
OptionBuilder.required = newRequired;
205206

206207
return instance;
207208
}
@@ -213,7 +214,7 @@ public static OptionBuilder isRequired(boolean required)
213214
*/
214215
public static OptionBuilder hasArgs()
215216
{
216-
instance.numberOfArgs = Option.UNLIMITED_VALUES;
217+
OptionBuilder.numberOfArgs = Option.UNLIMITED_VALUES;
217218

218219
return instance;
219220
}
@@ -227,7 +228,7 @@ public static OptionBuilder hasArgs()
227228
*/
228229
public static OptionBuilder hasArgs(int num)
229230
{
230-
instance.numberOfArgs = num;
231+
OptionBuilder.numberOfArgs = num;
231232

232233
return instance;
233234
}
@@ -239,8 +240,8 @@ public static OptionBuilder hasArgs(int num)
239240
*/
240241
public static OptionBuilder hasOptionalArg()
241242
{
242-
instance.numberOfArgs = 1;
243-
instance.optionalArg = true;
243+
OptionBuilder.numberOfArgs = 1;
244+
OptionBuilder.optionalArg = true;
244245

245246
return instance;
246247
}
@@ -253,8 +254,8 @@ public static OptionBuilder hasOptionalArg()
253254
*/
254255
public static OptionBuilder hasOptionalArgs()
255256
{
256-
instance.numberOfArgs = Option.UNLIMITED_VALUES;
257-
instance.optionalArg = true;
257+
OptionBuilder.numberOfArgs = Option.UNLIMITED_VALUES;
258+
OptionBuilder.optionalArg = true;
258259

259260
return instance;
260261
}
@@ -269,8 +270,8 @@ public static OptionBuilder hasOptionalArgs()
269270
*/
270271
public static OptionBuilder hasOptionalArgs(int numArgs)
271272
{
272-
instance.numberOfArgs = numArgs;
273-
instance.optionalArg = true;
273+
OptionBuilder.numberOfArgs = numArgs;
274+
OptionBuilder.optionalArg = true;
274275

275276
return instance;
276277
}
@@ -279,25 +280,25 @@ public static OptionBuilder hasOptionalArgs(int numArgs)
279280
* The next Option created will have a value that will be an instance
280281
* of <code>type</code>.
281282
*
282-
* @param type the type of the Options argument value
283+
* @param newType the type of the Options argument value
283284
* @return the OptionBuilder instance
284285
*/
285-
public static OptionBuilder withType(Object type)
286+
public static OptionBuilder withType(Object newType)
286287
{
287-
instance.type = type;
288+
OptionBuilder.type = newType;
288289

289290
return instance;
290291
}
291292

292293
/**
293294
* The next Option created will have the specified description
294295
*
295-
* @param description a description of the Option's purpose
296+
* @param newDescription a description of the Option's purpose
296297
* @return the OptionBuilder instance
297298
*/
298-
public static OptionBuilder withDescription(String description)
299+
public static OptionBuilder withDescription(String newDescription)
299300
{
300-
instance.description = description;
301+
OptionBuilder.description = newDescription;
301302

302303
return instance;
303304
}
@@ -363,7 +364,7 @@ public static Option create(String opt)
363364

364365

365366
// reset the OptionBuilder properties
366-
instance.reset();
367+
OptionBuilder.reset();
367368

368369
// return the Option instance
369370
return option;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class Options {
5757
*/
5858
public Options()
5959
{
60+
// nothing to do
6061
}
6162

6263
/**

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -323,24 +323,18 @@ public void processArgs(Option opt, ListIterator iter)
323323
if (options.hasOption(str) && str.startsWith("-"))
324324
{
325325
iter.previous();
326-
327326
break;
328327
}
329328

330329
// found a value
331-
else
330+
try
332331
{
333-
334-
try
335-
{
336-
opt.addValue( Util.stripLeadingAndTrailingQuotes(str) );
337-
}
338-
catch (RuntimeException exp)
339-
{
340-
iter.previous();
341-
342-
break;
343-
}
332+
opt.addValue( Util.stripLeadingAndTrailingQuotes(str) );
333+
}
334+
catch (RuntimeException exp)
335+
{
336+
iter.previous();
337+
break;
344338
}
345339
}
346340

@@ -366,9 +360,6 @@ public void processArgs(Option opt, ListIterator iter)
366360
private void processOption(String arg, ListIterator iter)
367361
throws ParseException
368362
{
369-
// get the option represented by arg
370-
Option opt = null;
371-
372363
boolean hasOption = options.hasOption(arg);
373364

374365
// if there is no option throw an UnrecognisedOptionException
@@ -377,10 +368,9 @@ private void processOption(String arg, ListIterator iter)
377368
throw new UnrecognizedOptionException("Unrecognized option: "
378369
+ arg);
379370
}
380-
else
381-
{
382-
opt = (Option) options.getOption(arg);
383-
}
371+
372+
// get the option represented by arg
373+
final Option opt = options.getOption(arg);
384374

385375
// if the option is a required option remove the option from
386376
// the requiredOptions list
@@ -393,7 +383,7 @@ private void processOption(String arg, ListIterator iter)
393383
// option of the group
394384
if (options.getOptionGroup(opt) != null)
395385
{
396-
OptionGroup group = (OptionGroup) options.getOptionGroup(opt);
386+
OptionGroup group = options.getOptionGroup(opt);
397387

398388
if (group.isRequired())
399389
{

src/java/org/apache/commons/cli/PatternOptionBuilder.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,12 @@ public static Options parsePattern(String pattern)
173173
{
174174
if (opt != ' ')
175175
{
176+
OptionBuilder.hasArg(type != null);
177+
OptionBuilder.isRequired(required);
178+
OptionBuilder.withType(type);
179+
176180
// we have a previous one to deal with
177-
options.addOption(
178-
OptionBuilder.hasArg(type != null)
179-
.isRequired(required).withType(type)
180-
.create(opt));
181+
options.addOption(OptionBuilder.create(opt));
181182
required = false;
182183
type = null;
183184
opt = ' ';
@@ -197,10 +198,12 @@ else if (ch == '!')
197198

198199
if (opt != ' ')
199200
{
201+
OptionBuilder.hasArg(type != null);
202+
OptionBuilder.isRequired(required);
203+
OptionBuilder.withType(type);
204+
200205
// we have a final one to deal with
201-
options.addOption(
202-
OptionBuilder.hasArg(type != null).isRequired(required)
203-
.withType(type).create(opt));
206+
options.addOption(OptionBuilder.create(opt));
204207
}
205208

206209
return options;

src/java/org/apache/commons/cli2/DisplaySetting.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ public boolean equals(final Object that) {
145145
if (that instanceof DisplaySetting) {
146146
return name.compareTo(that.toString()) == 0;
147147
}
148-
else {
149-
return false;
150-
}
148+
return false;
151149
}
152150

153151
public String toString() {

src/java/org/apache/commons/cli2/Option.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.util.ListIterator;
2121
import java.util.Set;
2222

23-
import org.apache.commons.cli2.util.HelpFormatter;
24-
2523
/**
2624
* The super type of all options representing a particular element of the
2725
* command line interface.

src/java/org/apache/commons/cli2/OptionException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class OptionException extends Exception {
3333
public static final Set HELP_SETTINGS =
3434
Collections.unmodifiableSet(
3535
Collections.singleton(
36-
DisplaySetting.DISPLAY_PROPERTY_OPTION));;
36+
DisplaySetting.DISPLAY_PROPERTY_OPTION));
3737

3838
/** The Option the exception relates to */
3939
private final Option option;

0 commit comments

Comments
 (0)