Skip to content

Commit 45c0174

Browse files
author
Robert James Oxspring
committed
Stops the PosixParser from bursting options unecessarily, i.e. if -file is an acceptible option then it won't be broken into -f -i -l -e.
PR: 32525 Submitted by: David Morris git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@130108 13f79535-47bb-0310-9956-ffa450edef68
1 parent 96923d1 commit 45c0174

3 files changed

Lines changed: 216 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @author John Keyes (john at integralsource.com)
2727
* @see Parser
28-
* @version $Revision: 1.15 $
28+
* @version $Revision: 1.16 $
2929
*/
3030
public class PosixParser extends Parser {
3131

@@ -135,7 +135,9 @@ else if (token.startsWith("-"))
135135
{
136136
processOptionToken(token, stopAtNonOption);
137137
}
138-
138+
else if (options.hasOption(token)) {
139+
tokens.add(token);
140+
}
139141
// requires bursting
140142
else
141143
{
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Copyright 2001-2004 The Apache Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.cli;
17+
18+
import junit.framework.Test;
19+
import junit.framework.TestCase;
20+
import junit.framework.TestSuite;
21+
22+
23+
public class ArgumentIsOptionTest extends TestCase {
24+
private Options options = null;
25+
private CommandLineParser parser = null;
26+
27+
public ArgumentIsOptionTest(String name) {
28+
super(name);
29+
}
30+
31+
public static Test suite() {
32+
return new TestSuite(ArgumentIsOptionTest.class);
33+
}
34+
35+
public void setUp() {
36+
options = new Options().addOption("p", false, "Option p").addOption("attr",
37+
true, "Option accepts argument");
38+
39+
parser = new PosixParser();
40+
}
41+
42+
public void tearDown() {
43+
}
44+
45+
public void testOptionAndOptionWithArgument() {
46+
String[] args = new String[] {
47+
"-p",
48+
"-attr",
49+
"value"
50+
};
51+
52+
try {
53+
CommandLine cl = parser.parse(options, args);
54+
assertTrue("Confirm -p is set", cl.hasOption("p"));
55+
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
56+
assertTrue("Confirm arg of -attr",
57+
cl.getOptionValue("attr").equals("value"));
58+
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
59+
}
60+
catch (ParseException e) {
61+
fail(e.toString());
62+
}
63+
}
64+
65+
public void testOptionWithArgument() {
66+
String[] args = new String[] {
67+
"-attr",
68+
"value"
69+
};
70+
71+
try {
72+
CommandLine cl = parser.parse(options, args);
73+
assertFalse("Confirm -p is set", cl.hasOption("p"));
74+
assertTrue("Confirm -attr is set", cl.hasOption("attr"));
75+
assertTrue("Confirm arg of -attr",
76+
cl.getOptionValue("attr").equals("value"));
77+
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
78+
}
79+
catch (ParseException e) {
80+
fail(e.toString());
81+
}
82+
}
83+
84+
public void testOption() {
85+
String[] args = new String[] {
86+
"-p"
87+
};
88+
89+
try {
90+
CommandLine cl = parser.parse(options, args);
91+
assertTrue("Confirm -p is set", cl.hasOption("p"));
92+
assertFalse("Confirm -attr is not set", cl.hasOption("attr"));
93+
assertTrue("Confirm all arguments recognized", cl.getArgs().length == 0);
94+
}
95+
catch (ParseException e) {
96+
fail(e.toString());
97+
}
98+
}
99+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Copyright 2001-2004 The Apache Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.commons.cli;
17+
18+
import junit.framework.Test;
19+
import junit.framework.TestCase;
20+
import junit.framework.TestSuite;
21+
22+
23+
/**
24+
* <p>
25+
* This is a collection of tests that test real world
26+
* applications command lines focusing on options with
27+
* long and short names.
28+
* </p>
29+
*/
30+
public class LongOptionWithShort extends TestCase {
31+
public LongOptionWithShort(String name) {
32+
super(name);
33+
}
34+
35+
public static Test suite() {
36+
return new TestSuite(LongOptionWithShort.class);
37+
}
38+
39+
/**
40+
*
41+
*/
42+
public void testLongOptionWithShort() {
43+
Option help = new Option("h", "help", false, "print this message");
44+
Option version = new Option("v", "version", false,
45+
"print version information");
46+
Option newRun = new Option("n", "new", false,
47+
"Create NLT cache entries only for new items");
48+
Option trackerRun = new Option("t", "tracker", false,
49+
"Create NLT cache entries only for tracker items");
50+
51+
Option timeLimit = OptionBuilder.withLongOpt("limit").hasArg()
52+
.withValueSeparator()
53+
.withDescription("Set time limit for execution, in mintues")
54+
.create("l");
55+
56+
Option age = OptionBuilder.withLongOpt("age").hasArg()
57+
.withValueSeparator()
58+
.withDescription("Age (in days) of cache item before being recomputed")
59+
.create("a");
60+
61+
Option server = OptionBuilder.withLongOpt("server").hasArg()
62+
.withValueSeparator()
63+
.withDescription("The NLT server address")
64+
.create("s");
65+
66+
Option numResults = OptionBuilder.withLongOpt("results").hasArg()
67+
.withValueSeparator()
68+
.withDescription("Number of results per item")
69+
.create("r");
70+
71+
Option configFile = OptionBuilder.withLongOpt("file").hasArg()
72+
.withValueSeparator()
73+
.withDescription("Use the specified configuration file")
74+
.create();
75+
76+
Options options = new Options();
77+
options.addOption(help);
78+
options.addOption(version);
79+
options.addOption(newRun);
80+
options.addOption(trackerRun);
81+
options.addOption(timeLimit);
82+
options.addOption(age);
83+
options.addOption(server);
84+
options.addOption(numResults);
85+
options.addOption(configFile);
86+
87+
// create the command line parser
88+
CommandLineParser parser = new PosixParser();
89+
90+
String[] args = new String[] {
91+
"-v",
92+
"-l",
93+
"10",
94+
"-age",
95+
"5",
96+
"-file",
97+
"filename"
98+
};
99+
100+
try {
101+
CommandLine line = parser.parse(options, args);
102+
assertTrue(line.hasOption("v"));
103+
assertEquals(line.getOptionValue("l"), "10");
104+
assertEquals(line.getOptionValue("limit"), "10");
105+
assertEquals(line.getOptionValue("a"), "5");
106+
assertEquals(line.getOptionValue("age"), "5");
107+
assertEquals(line.getOptionValue("file"), "filename");
108+
}
109+
catch (ParseException exp) {
110+
fail("Unexpected exception:" + exp.getMessage());
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)