Skip to content

Commit aa050a6

Browse files
committed
Fixed the parsing of --foo=bar and -f=bar options with the GnuParser (CLI-157)
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@662192 13f79535-47bb-0310-9956-ffa450edef68
1 parent b8f8259 commit aa050a6

2 files changed

Lines changed: 68 additions & 16 deletions

File tree

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,32 @@ else if ("-".equals(arg))
7070
}
7171
else if (arg.startsWith("-"))
7272
{
73-
Option option = options.getOption(arg);
73+
String opt = Util.stripLeadingHyphens(arg);
7474

75-
String head = arg.substring(0, 2); // "--" if --foo, "-f" if -foo
76-
String tail = arg.substring(2); // "foo" if -foo, "oo" if -foo
77-
78-
// this is not an Option
79-
if (option == null)
75+
if (options.hasOption(opt))
8076
{
81-
// handle special properties Option (-Dproperty=value for example)
82-
Option specialOption = options.getOption(head);
83-
84-
if (specialOption != null)
77+
tokens.add(arg);
78+
}
79+
else
80+
{
81+
if (opt.indexOf('=') != -1 && options.hasOption(opt.substring(0, opt.indexOf('='))))
8582
{
86-
tokens.add(head); // -D
87-
tokens.add(tail); // property=value
83+
// the format is --foo=value or -foo=value
84+
tokens.add(arg.substring(0, arg.indexOf('='))); // --foo
85+
tokens.add(arg.substring(arg.indexOf('=') + 1)); // value
86+
}
87+
else if (options.hasOption(arg.substring(0, 2)))
88+
{
89+
// the format is a special properties option (-Dproperty=value)
90+
tokens.add(arg.substring(0, 2)); // -D
91+
tokens.add(arg.substring(2)); // property=value
8892
}
8993
else
9094
{
9195
eatTheRest = stopAtNonOption;
9296
tokens.add(arg);
9397
}
9498
}
95-
else
96-
{
97-
tokens.add(arg);
98-
}
9999
}
100100
else
101101
{

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,56 @@ public void testNegativeArgument() throws Exception
195195
CommandLine cl = parser.parse(options, args);
196196
assertEquals("-1", cl.getOptionValue("a"));
197197
}
198+
199+
public void testShortWithEqual() throws Exception
200+
{
201+
String[] args = new String[] { "-f=bar" };
202+
203+
Options options = new Options();
204+
options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
205+
206+
Parser parser = new GnuParser();
207+
CommandLine cl = parser.parse(options, args);
208+
209+
assertEquals("bar", cl.getOptionValue("foo"));
210+
}
211+
212+
public void testShortWithoutEqual() throws Exception
213+
{
214+
String[] args = new String[] { "-fbar" };
215+
216+
Options options = new Options();
217+
options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
218+
219+
Parser parser = new GnuParser();
220+
CommandLine cl = parser.parse(options, args);
221+
222+
assertEquals("bar", cl.getOptionValue("foo"));
223+
}
224+
225+
public void testLongWithEqual() throws Exception
226+
{
227+
String[] args = new String[] { "--foo=bar" };
228+
229+
Options options = new Options();
230+
options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
231+
232+
Parser parser = new GnuParser();
233+
CommandLine cl = parser.parse(options, args);
234+
235+
assertEquals("bar", cl.getOptionValue("foo"));
236+
}
237+
238+
public void testLongWithEqualSingleDash() throws Exception
239+
{
240+
String[] args = new String[] { "-foo=bar" };
241+
242+
Options options = new Options();
243+
options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
244+
245+
Parser parser = new GnuParser();
246+
CommandLine cl = parser.parse(options, args);
247+
248+
assertEquals("bar", cl.getOptionValue("foo"));
249+
}
198250
}

0 commit comments

Comments
 (0)