Skip to content

Commit 9903ac8

Browse files
author
Robert James Oxspring
committed
Parser now correctly allows option arguments to be the same as other options, less the initial '-'. E.g. with options -a, the value 'a' is now allowed.
Also corrected typo in test: testNoCyclesPresnet(). PR: 25044 Submitted by: David Morris git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@130109 13f79535-47bb-0310-9956-ffa450edef68
1 parent 45c0174 commit 9903ac8

4 files changed

Lines changed: 35 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* @author John Keyes (john at integralsource.com)
2929
* @see Parser
30-
* @version $Revision: 1.15 $
30+
* @version $Revision: 1.16 $
3131
*/
3232
public abstract class Parser implements CommandLineParser {
3333

@@ -319,7 +319,8 @@ public void processArgs(Option opt, ListIterator iter)
319319
{
320320
String str = (String) iter.next();
321321

322-
// found an Option
322+
// found an Option, not an argument
323+
//if (options.hasOption(str) && str.startsWith("-"))
323324
if (options.hasOption(str))
324325
{
325326
iter.previous();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ public void testOptionAndOptionWithArgument() {
4646
String[] args = new String[] {
4747
"-p",
4848
"-attr",
49-
"value"
49+
"p"
5050
};
5151

5252
try {
5353
CommandLine cl = parser.parse(options, args);
5454
assertTrue("Confirm -p is set", cl.hasOption("p"));
5555
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
5656
assertTrue("Confirm arg of -attr",
57-
cl.getOptionValue("attr").equals("value"));
57+
cl.getOptionValue("attr").equals("p"));
5858
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
5959
}
6060
catch (ParseException e) {
@@ -65,15 +65,15 @@ public void testOptionAndOptionWithArgument() {
6565
public void testOptionWithArgument() {
6666
String[] args = new String[] {
6767
"-attr",
68-
"value"
68+
"p"
6969
};
7070

7171
try {
7272
CommandLine cl = parser.parse(options, args);
7373
assertFalse("Confirm -p is set", cl.hasOption("p"));
7474
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
7575
assertTrue("Confirm arg of -attr",
76-
cl.getOptionValue("attr").equals("value"));
76+
cl.getOptionValue("attr").equals("p"));
7777
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
7878
}
7979
catch (ParseException e) {

src/test/org/apache/commons/cli2/CommandLineTestCase.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Iterator;
2020
import java.util.List;
2121

22+
import org.apache.commons.cli2.builder.ArgumentBuilder;
2223
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
2324
import org.apache.commons.cli2.builder.GroupBuilder;
2425
import org.apache.commons.cli2.commandline.Parser;
@@ -484,4 +485,30 @@ public final void testGetOptionCount_Strings() throws OptionException {
484485
assertEquals(3, cl.getOptionCount("-?"));
485486
assertEquals(1, cl.getOptionCount("+display"));
486487
}
488+
489+
public final void testOptionAsArgument() throws OptionException {
490+
final Option bool = new DefaultOptionBuilder().withShortName("p").create();
491+
final Argument argument = new ArgumentBuilder().create();
492+
final Option withArgument = new DefaultOptionBuilder().withShortName("attr").withArgument(argument).create();
493+
494+
final Group group =
495+
new GroupBuilder()
496+
.withOption(bool)
497+
.withOption(withArgument)
498+
.create();
499+
500+
final Parser parser = new Parser();
501+
parser.setGroup(group);
502+
final CommandLine cl =
503+
parser.parse(
504+
new String[] {
505+
"-p",
506+
"-attr",
507+
"p" });
508+
509+
assertEquals(1, cl.getOptionCount("-p"));
510+
assertTrue(cl.hasOption("-p"));
511+
assertTrue(cl.hasOption("-attr"));
512+
assertTrue(cl.getValue("-attr").equals("p"));
513+
}
487514
}

src/test/org/apache/commons/cli2/jdepend/JDependTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testAcceptableDistance() {
7171
}
7272
}
7373

74-
public void testNoCyclesPresnet() {
74+
public void testNoCyclesPresent() {
7575
assertEquals("Cycles exist", false, dependancies.containsCycles());
7676
}
7777

0 commit comments

Comments
 (0)