Skip to content

Commit 05e7cd0

Browse files
committed
Simplified Options.helpOptions(), the loop looking into the lonOpts wasn't necessary since all options are already in shortOpts
Minor style & doc improvements git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@661699 13f79535-47bb-0310-9956-ffa450edef68
1 parent ead1f16 commit 05e7cd0

2 files changed

Lines changed: 19 additions & 47 deletions

File tree

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
package org.apache.commons.cli;
1819

1920
import java.io.Serializable;
@@ -98,10 +99,12 @@ public Options addOptionGroup(OptionGroup group)
9899

99100
/**
100101
* Lists the OptionGroups that are members of this Options instance.
102+
*
101103
* @return a Collection of OptionGroup instances.
102104
*/
103-
Collection getOptionGroups(){
104-
return new HashSet(optionGroups.values());
105+
Collection getOptionGroups()
106+
{
107+
return new HashSet(optionGroups.values());
105108
}
106109

107110
/**
@@ -185,38 +188,22 @@ public Collection getOptions()
185188
*/
186189
List helpOptions()
187190
{
188-
List opts = new ArrayList(shortOpts.values());
189-
190-
// now look through the long opts to see if there are any Long-opt
191-
// only options
192-
Iterator iter = longOpts.values().iterator();
193-
194-
while (iter.hasNext())
195-
{
196-
Object item = iter.next();
197-
198-
if (!opts.contains(item))
199-
{
200-
opts.add(item);
201-
}
202-
}
203-
204-
return new ArrayList(opts);
191+
return new ArrayList(shortOpts.values());
205192
}
206193

207194
/**
208-
* Returns the required options as a
209-
* <code>java.util.Collection</code>.
195+
* Returns the required options.
210196
*
211-
* @return Collection of required options
197+
* @return List of required options
212198
*/
213199
public List getRequiredOptions()
214200
{
215201
return requiredOpts;
216202
}
217203

218204
/**
219-
* Retrieve the named {@link Option}
205+
* Retrieve the {@link Option} matching the long or short name specified.
206+
* The leading hyphens in the name are ignored (up to 2).
220207
*
221208
* @param opt short or long name of the {@link Option}
222209
* @return the option represented by opt
@@ -278,4 +265,4 @@ public String toString()
278265

279266
return buf.toString();
280267
}
281-
}
268+
}

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,12 @@ public class OptionsTest extends TestCase
3030
{
3131
public void testHelpOptions(){
3232

33-
Option longOnly1 = OptionBuilder
34-
.withLongOpt("long-only1")
35-
.create();
36-
37-
Option longOnly2 = OptionBuilder
38-
.withLongOpt("long-only2")
39-
.create();
40-
41-
Option shortOnly1 = OptionBuilder
42-
.create("1");
43-
44-
Option shortOnly2 = OptionBuilder
45-
.create("2");
46-
47-
Option bothA = OptionBuilder
48-
.withLongOpt("bothA")
49-
.create("a");
50-
51-
Option bothB = OptionBuilder
52-
.withLongOpt("bothB")
53-
.create("b");
33+
Option longOnly1 = OptionBuilder.withLongOpt("long-only1").create();
34+
Option longOnly2 = OptionBuilder.withLongOpt("long-only2").create();
35+
Option shortOnly1 = OptionBuilder.create("1");
36+
Option shortOnly2 = OptionBuilder.create("2");
37+
Option bothA = OptionBuilder.withLongOpt("bothA").create("a");
38+
Option bothB = OptionBuilder.withLongOpt("bothB").create("b");
5439

5540
Options options = new Options();
5641
options.addOption(longOnly1);
@@ -70,8 +55,8 @@ public void testHelpOptions(){
7055

7156
Collection helpOptions = options.helpOptions();
7257

73-
assertTrue("Everything in all should be in help",helpOptions.containsAll(allOptions));
74-
assertTrue("Everything in help should be in all",allOptions.containsAll(helpOptions));
58+
assertTrue("Everything in all should be in help", helpOptions.containsAll(allOptions));
59+
assertTrue("Everything in help should be in all", allOptions.containsAll(helpOptions));
7560
}
7661

7762
public void testMissingOptionException() throws ParseException {

0 commit comments

Comments
 (0)