You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2008/06/01 12:01:51 UTC
svn commit: r662192 - in /commons/proper/cli/branches/cli-1.x/src:
java/org/apache/commons/cli/GnuParser.java
test/org/apache/commons/cli/GnuParserTest.java
Author: ebourg
Date: Sun Jun 1 03:01:50 2008
New Revision: 662192
URL: http://svn.apache.org/viewvc?rev=662192&view=rev
Log:
Fixed the parsing of --foo=bar and -f=bar options with the GnuParser (CLI-157)
Modified:
commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/GnuParser.java
commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/GnuParser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/GnuParser.java?rev=662192&r1=662191&r2=662192&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/GnuParser.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/GnuParser.java Sun Jun 1 03:01:50 2008
@@ -70,21 +70,25 @@
}
else if (arg.startsWith("-"))
{
- Option option = options.getOption(arg);
+ String opt = Util.stripLeadingHyphens(arg);
- String head = arg.substring(0, 2); // "--" if --foo, "-f" if -foo
- String tail = arg.substring(2); // "foo" if -foo, "oo" if -foo
-
- // this is not an Option
- if (option == null)
+ if (options.hasOption(opt))
{
- // handle special properties Option (-Dproperty=value for example)
- Option specialOption = options.getOption(head);
-
- if (specialOption != null)
+ tokens.add(arg);
+ }
+ else
+ {
+ if (opt.indexOf('=') != -1 && options.hasOption(opt.substring(0, opt.indexOf('='))))
{
- tokens.add(head); // -D
- tokens.add(tail); // property=value
+ // the format is --foo=value or -foo=value
+ tokens.add(arg.substring(0, arg.indexOf('='))); // --foo
+ tokens.add(arg.substring(arg.indexOf('=') + 1)); // value
+ }
+ else if (options.hasOption(arg.substring(0, 2)))
+ {
+ // the format is a special properties option (-Dproperty=value)
+ tokens.add(arg.substring(0, 2)); // -D
+ tokens.add(arg.substring(2)); // property=value
}
else
{
@@ -92,10 +96,6 @@
tokens.add(arg);
}
}
- else
- {
- tokens.add(arg);
- }
}
else
{
Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java?rev=662192&r1=662191&r2=662192&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/GnuParserTest.java Sun Jun 1 03:01:50 2008
@@ -195,4 +195,56 @@
CommandLine cl = parser.parse(options, args);
assertEquals("-1", cl.getOptionValue("a"));
}
+
+ public void testShortWithEqual() throws Exception
+ {
+ String[] args = new String[] { "-f=bar" };
+
+ Options options = new Options();
+ options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+ Parser parser = new GnuParser();
+ CommandLine cl = parser.parse(options, args);
+
+ assertEquals("bar", cl.getOptionValue("foo"));
+ }
+
+ public void testShortWithoutEqual() throws Exception
+ {
+ String[] args = new String[] { "-fbar" };
+
+ Options options = new Options();
+ options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+ Parser parser = new GnuParser();
+ CommandLine cl = parser.parse(options, args);
+
+ assertEquals("bar", cl.getOptionValue("foo"));
+ }
+
+ public void testLongWithEqual() throws Exception
+ {
+ String[] args = new String[] { "--foo=bar" };
+
+ Options options = new Options();
+ options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+ Parser parser = new GnuParser();
+ CommandLine cl = parser.parse(options, args);
+
+ assertEquals("bar", cl.getOptionValue("foo"));
+ }
+
+ public void testLongWithEqualSingleDash() throws Exception
+ {
+ String[] args = new String[] { "-foo=bar" };
+
+ Options options = new Options();
+ options.addOption(OptionBuilder.withLongOpt("foo").hasArg().create('f'));
+
+ Parser parser = new GnuParser();
+ CommandLine cl = parser.parse(options, args);
+
+ assertEquals("bar", cl.getOptionValue("foo"));
+ }
}