Author: britter
Date: Sat Mar 25 18:32:55 2017
New Revision: 1788678
URL: http://svn.apache.org/viewvc?rev=1788678&view=rev
Log:
CLI-271: CommandLine.getXXX and CommandLine.hasXXX should accept an Option as a
parameter. Thanks to Christoph Läubrich. This also fixes #9 from GitHub
Modified:
commons/proper/cli/trunk/src/changes/changes.xml
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ValueTest.java
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI133Test.java
Modified: commons/proper/cli/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/changes/changes.xml?rev=1788678&r1=1788677&r2=1788678&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/changes/changes.xml (original)
+++ commons/proper/cli/trunk/src/changes/changes.xml Sat Mar 25 18:32:55 2017
@@ -23,6 +23,9 @@
<body>
<release version="1.5" date="tba" description="tba">
+ <action type="add" dev="britter" due-to="Christoph Läubrich"
issue="CLI-271">
+ CommandLine.getXXX and CommandLine.hasXXX should accept an Option as a
parameter
+ </action>
</release>
<release version="1.4" date="2017-03-09" description="New features and bug
fixes">
Modified:
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java?rev=1788678&r1=1788677&r2=1788678&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
(original)
+++
commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/CommandLine.java
Sat Mar 25 18:32:55 2017
@@ -55,6 +55,18 @@ public class CommandLine implements Seri
{
// nothing to do
}
+
+ /**
+ * Query to see if an option has been set.
+ *
+ * @param opt the option to check
+ * @return true if set, false if not
+ * @since 1.4
+ */
+ public boolean hasOption(Option opt)
+ {
+ return options.contains(opt);
+ }
/**
* Query to see if an option has been set.
@@ -64,9 +76,9 @@ public class CommandLine implements Seri
*/
public boolean hasOption(String opt)
{
- return options.contains(resolveOption(opt));
+ return hasOption(resolveOption(opt));
}
-
+
/**
* Query to see if an option has been set.
*
@@ -98,32 +110,62 @@ public class CommandLine implements Seri
return null;
}
}
-
+
/**
* Return a version of this <code>Option</code> converted to a particular
type.
*
- * @param opt the name of the option
+ * @param option the name of the option
* @return the value parsed into a particular object
* @throws ParseException if there are problems turning the option value
into the desired type
* @see PatternOptionBuilder
- * @since 1.2
+ * @since 1.4
*/
- public Object getParsedOptionValue(String opt) throws ParseException
+ public Object getParsedOptionValue(Option option) throws ParseException
{
- String res = getOptionValue(opt);
- Option option = resolveOption(opt);
-
- if (option == null || res == null)
+ if (option == null)
+ {
+ return null;
+ }
+ String res = getOptionValue(option);
+ if (res == null)
{
return null;
}
-
return TypeHandler.createValue(res, option.getType());
}
/**
+ * Return a version of this <code>Option</code> converted to a particular
type.
+ *
+ * @param opt the name of the option
+ * @return the value parsed into a particular object
+ * @throws ParseException if there are problems turning the option value
into the desired type
+ * @see PatternOptionBuilder
+ * @since 1.2
+ */
+ public Object getParsedOptionValue(String opt) throws ParseException
+ {
+ return getParsedOptionValue(resolveOption(opt));
+ }
+
+ /**
+ * Return a version of this <code>Option</code> converted to a particular
type.
+ *
+ * @param opt the name of the option
+ * @return the value parsed into a particular object
+ * @throws ParseException if there are problems turning the option value
into the desired type
+ * @see PatternOptionBuilder
+ * @since 1.2
+ */
+ public Object getParsedOptionValue(char opt) throws ParseException
+ {
+ return getParsedOptionValue(String.valueOf(opt));
+ }
+
+ /**
* Return the <code>Object</code> type of this <code>Option</code>.
*
+ * @deprecated due to System.err message. Instead use
getParsedOptionValue(char)
* @param opt the name of the option
* @return the type of opt
*/
@@ -131,6 +173,24 @@ public class CommandLine implements Seri
{
return getOptionObject(String.valueOf(opt));
}
+
+ /**
+ * Retrieve the first argument, if any, of this option.
+ *
+ * @param option the name of the option
+ * @return Value of the argument if option is set, and has an argument,
+ * otherwise null.
+ * @since 1.4
+ */
+ public String getOptionValue(Option option)
+ {
+ if (option == null)
+ {
+ return null;
+ }
+ String[] values = getOptionValues(option);
+ return (values == null) ? null : values[0];
+ }
/**
* Retrieve the first argument, if any, of this option.
@@ -141,9 +201,7 @@ public class CommandLine implements Seri
*/
public String getOptionValue(String opt)
{
- String[] values = getOptionValues(opt);
-
- return (values == null) ? null : values[0];
+ return getOptionValue(resolveOption(opt));
}
/**
@@ -157,29 +215,42 @@ public class CommandLine implements Seri
{
return getOptionValue(String.valueOf(opt));
}
-
+
/**
* Retrieves the array of values, if any, of an option.
*
- * @param opt string name of the option
+ * @param option string name of the option
* @return Values of the argument if option is set, and has an argument,
* otherwise null.
+ * @since 1.4
*/
- public String[] getOptionValues(String opt)
+ public String[] getOptionValues(Option option)
{
List<String> values = new ArrayList<String>();
- for (Option option : options)
+ for (Option processedOption : options)
{
- if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt()))
+ if (processedOption.equals(option))
{
- values.addAll(option.getValuesList());
+ values.addAll(processedOption.getValuesList());
}
}
return values.isEmpty() ? null : values.toArray(new
String[values.size()]);
}
+ /**
+ * Retrieves the array of values, if any, of an option.
+ *
+ * @param opt string name of the option
+ * @return Values of the argument if option is set, and has an argument,
+ * otherwise null.
+ */
+ public String[] getOptionValues(String opt)
+ {
+ return getOptionValues(resolveOption(opt));
+ }
+
/**
* Retrieves the option object given the long or short option as a String
*
@@ -216,6 +287,22 @@ public class CommandLine implements Seri
{
return getOptionValues(String.valueOf(opt));
}
+
+ /**
+ * Retrieve the first argument, if any, of an option.
+ *
+ * @param option name of the option
+ * @param defaultValue is the default value to be returned if the option
+ * is not specified
+ * @return Value of the argument if option is set, and has an argument,
+ * otherwise <code>defaultValue</code>.
+ * @since 1.4
+ */
+ public String getOptionValue(Option option, String defaultValue)
+ {
+ String answer = getOptionValue(option);
+ return (answer != null) ? answer : defaultValue;
+ }
/**
* Retrieve the first argument, if any, of an option.
@@ -228,9 +315,7 @@ public class CommandLine implements Seri
*/
public String getOptionValue(String opt, String defaultValue)
{
- String answer = getOptionValue(opt);
-
- return (answer != null) ? answer : defaultValue;
+ return getOptionValue(resolveOption(opt), defaultValue);
}
/**
@@ -246,6 +331,44 @@ public class CommandLine implements Seri
{
return getOptionValue(String.valueOf(opt), defaultValue);
}
+
+ /**
+ * Retrieve the map of values associated to the option. This is convenient
+ * for options specifying Java properties like <tt>-Dparam1=value1
+ * -Dparam2=value2</tt>. The first argument of the option is the key, and
+ * the 2nd argument is the value. If the option has only one argument
+ * (<tt>-Dfoo</tt>) it is considered as a boolean flag and the value is
+ * <tt>"true"</tt>.
+ *
+ * @param option name of the option
+ * @return The Properties mapped by the option, never <tt>null</tt>
+ * even if the option doesn't exists
+ * @since 1.4
+ */
+ public Properties getOptionProperties(Option option)
+ {
+ Properties props = new Properties();
+
+ for (Option processedOption : options)
+ {
+ if (processedOption.equals(option))
+ {
+ List<String> values = processedOption.getValuesList();
+ if (values.size() >= 2)
+ {
+ // use the first 2 arguments as the key/value pair
+ props.put(values.get(0), values.get(1));
+ }
+ else if (values.size() == 1)
+ {
+ // no explicit value, handle it as a boolean
+ props.put(values.get(0), "true");
+ }
+ }
+ }
+
+ return props;
+ }
/**
* Retrieve the map of values associated to the option. This is convenient
Modified:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java?rev=1788678&r1=1788677&r2=1788678&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
(original)
+++
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/CommandLineTest.java
Sat Mar 25 18:32:55 2017
@@ -19,6 +19,7 @@ package org.apache.commons.cli;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.util.Properties;
@@ -49,6 +50,31 @@ public class CommandLineTest
assertEquals("property with long format", "bar",
cl.getOptionProperties("property").getProperty("foo"));
}
+
+ @Test
+ public void testGetOptionPropertiesWithOption() throws Exception
+ {
+ String[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2",
"-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" };
+
+ Options options = new Options();
+ Option option_D =
OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D');
+ Option option_property =
OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create();
+ options.addOption(option_D);
+ options.addOption(option_property);
+
+ Parser parser = new GnuParser();
+ CommandLine cl = parser.parse(options, args);
+
+ Properties props = cl.getOptionProperties(option_D);
+ assertNotNull("null properties", props);
+ assertEquals("number of properties in " + props, 4, props.size());
+ assertEquals("property 1", "value1", props.getProperty("param1"));
+ assertEquals("property 2", "value2", props.getProperty("param2"));
+ assertEquals("property 3", "true", props.getProperty("param3"));
+ assertEquals("property 4", "value4", props.getProperty("param4"));
+
+ assertEquals("property with long format", "bar",
cl.getOptionProperties(option_property).getProperty("foo"));
+ }
@Test
public void testGetOptions()
@@ -76,6 +102,47 @@ public class CommandLineTest
assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue());
assertEquals("foo", cmd.getParsedOptionValue("f"));
}
+
+ @Test
+ public void testGetParsedOptionValueWithChar() throws Exception {
+ Options options = new Options();
+
options.addOption(Option.builder("i").hasArg().type(Number.class).build());
+ options.addOption(Option.builder("f").hasArg().build());
+
+ CommandLineParser parser = new DefaultParser();
+ CommandLine cmd = parser.parse(options, new String[] { "-i", "123",
"-f", "foo" });
+
+ assertEquals(123, ((Number) cmd.getParsedOptionValue('i')).intValue());
+ assertEquals("foo", cmd.getParsedOptionValue('f'));
+ }
+
+ @Test
+ public void testGetParsedOptionValueWithOption() throws Exception {
+ Options options = new Options();
+ Option opt_i = Option.builder("i").hasArg().type(Number.class).build();
+ Option opt_f = Option.builder("f").hasArg().build();
+ options.addOption(opt_i);
+ options.addOption(opt_f);
+
+ CommandLineParser parser = new DefaultParser();
+ CommandLine cmd = parser.parse(options, new String[] { "-i", "123",
"-f", "foo" });
+
+ assertEquals(123, ((Number)
cmd.getParsedOptionValue(opt_i)).intValue());
+ assertEquals("foo", cmd.getParsedOptionValue(opt_f));
+ }
+
+ @Test
+ public void testNullhOption() throws Exception {
+ Options options = new Options();
+ Option opt_i = Option.builder("i").hasArg().type(Number.class).build();
+ Option opt_f = Option.builder("f").hasArg().build();
+ options.addOption(opt_i);
+ options.addOption(opt_f);
+ CommandLineParser parser = new DefaultParser();
+ CommandLine cmd = parser.parse(options, new String[] { "-i", "123",
"-f", "foo" });
+ assertNull(cmd.getOptionValue((Option)null));
+ assertNull(cmd.getParsedOptionValue((Option)null));
+ }
@Test
public void testBuilder()
Modified:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java?rev=1788678&r1=1788677&r2=1788678&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java
(original)
+++
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionTest.java
Sat Mar 25 18:32:55 2017
@@ -19,6 +19,7 @@ package org.apache.commons.cli;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
@@ -71,6 +72,13 @@ public class OptionTest
assertEquals(0, a.getValuesList().size());
assertEquals(2, b.getValues().length);
}
+
+ @Test
+ public void testHashCode() {
+ assertNotEquals(Option.builder("test").build().hashCode(),
Option.builder("test2").build().hashCode()) ;
+ assertNotEquals(Option.builder("test").build().hashCode(),
Option.builder().longOpt("test").build().hashCode()) ;
+ assertNotEquals(Option.builder("test").build().hashCode(),
Option.builder("test").longOpt("long test").build().hashCode()) ;
+ }
private static class DefaultOption extends Option
{
Modified:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ValueTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ValueTest.java?rev=1788678&r1=1788677&r2=1788678&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ValueTest.java
(original)
+++
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ValueTest.java
Sat Mar 25 18:32:55 2017
@@ -62,6 +62,13 @@ public class ValueTest
assertTrue( _cl.hasOption("a") );
assertNull( _cl.getOptionValue("a") );
}
+
+ @Test
+ public void testShortNoArgWithOption()
+ {
+ assertTrue( _cl.hasOption(opts.getOption("a")) );
+ assertNull( _cl.getOptionValue(opts.getOption("a")) );
+ }
@Test
public void testShortWithArg()
@@ -70,6 +77,14 @@ public class ValueTest
assertNotNull( _cl.getOptionValue("b") );
assertEquals( _cl.getOptionValue("b"), "foo");
}
+
+ @Test
+ public void testShortWithArgWithOption()
+ {
+ assertTrue( _cl.hasOption(opts.getOption("b")) );
+ assertNotNull( _cl.getOptionValue(opts.getOption("b")) );
+ assertEquals( _cl.getOptionValue(opts.getOption("b")), "foo");
+ }
@Test
public void testLongNoArg()
@@ -77,6 +92,13 @@ public class ValueTest
assertTrue( _cl.hasOption("c") );
assertNull( _cl.getOptionValue("c") );
}
+
+ @Test
+ public void testLongNoArgWithOption()
+ {
+ assertTrue( _cl.hasOption(opts.getOption("c")) );
+ assertNull( _cl.getOptionValue(opts.getOption("c")) );
+ }
@Test
public void testLongWithArg()
@@ -85,6 +107,14 @@ public class ValueTest
assertNotNull( _cl.getOptionValue("d") );
assertEquals( _cl.getOptionValue("d"), "bar");
}
+
+ @Test
+ public void testLongWithArgWithOption()
+ {
+ assertTrue( _cl.hasOption(opts.getOption("d")) );
+ assertNotNull( _cl.getOptionValue(opts.getOption("d")) );
+ assertEquals( _cl.getOptionValue(opts.getOption("d")), "bar");
+ }
@Test
public void testShortOptionalArgNoValue() throws Exception
@@ -96,6 +126,17 @@ public class ValueTest
assertTrue( cmd.hasOption("e") );
assertNull( cmd.getOptionValue("e") );
}
+
+ @Test
+ public void testShortOptionalArgNoValueWithOption() throws Exception
+ {
+ String[] args = new String[] { "-e" };
+
+ Parser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption(opts.getOption("e")) );
+ assertNull( cmd.getOptionValue(opts.getOption("e")) );
+ }
@Test
public void testShortOptionalArgValue() throws Exception
@@ -107,6 +148,17 @@ public class ValueTest
assertTrue( cmd.hasOption("e") );
assertEquals( "everything", cmd.getOptionValue("e") );
}
+
+ @Test
+ public void testShortOptionalArgValueWithOption() throws Exception
+ {
+ String[] args = new String[] { "-e", "everything" };
+
+ Parser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption(opts.getOption("e")) );
+ assertEquals( "everything", cmd.getOptionValue(opts.getOption("e")) );
+ }
@Test
public void testLongOptionalNoValue() throws Exception
@@ -118,6 +170,17 @@ public class ValueTest
assertTrue( cmd.hasOption("fish") );
assertNull( cmd.getOptionValue("fish") );
}
+
+ @Test
+ public void testLongOptionalNoValueWithOption() throws Exception
+ {
+ String[] args = new String[] { "--fish" };
+
+ Parser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption(opts.getOption("fish")) );
+ assertNull( cmd.getOptionValue(opts.getOption("fish")) );
+ }
@Test
public void testLongOptionalArgValue() throws Exception
@@ -129,6 +192,17 @@ public class ValueTest
assertTrue( cmd.hasOption("fish") );
assertEquals( "face", cmd.getOptionValue("fish") );
}
+
+ @Test
+ public void testLongOptionalArgValueWithOption() throws Exception
+ {
+ String[] args = new String[] { "--fish", "face" };
+
+ Parser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption(opts.getOption("fish")) );
+ assertEquals( "face", cmd.getOptionValue(opts.getOption("fish")) );
+ }
@Test
public void testShortOptionalArgValues() throws Exception
@@ -143,6 +217,20 @@ public class ValueTest
assertEquals( "idea", cmd.getOptionValues("j")[1] );
assertEquals( cmd.getArgs().length, 0 );
}
+
+ @Test
+ public void testShortOptionalArgValuesWithOption() throws Exception
+ {
+ String[] args = new String[] { "-j", "ink", "idea" };
+
+ Parser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption(opts.getOption("j")) );
+ assertEquals( "ink", cmd.getOptionValue(opts.getOption("j")) );
+ assertEquals( "ink", cmd.getOptionValues(opts.getOption("j"))[0] );
+ assertEquals( "idea", cmd.getOptionValues(opts.getOption("j"))[1] );
+ assertEquals( cmd.getArgs().length, 0 );
+ }
@Test
public void testLongOptionalArgValues() throws Exception
@@ -157,6 +245,20 @@ public class ValueTest
assertEquals( "garden", cmd.getOptionValues("gravy")[1] );
assertEquals( cmd.getArgs().length, 0 );
}
+
+ @Test
+ public void testLongOptionalArgValuesWithOption() throws Exception
+ {
+ String[] args = new String[] { "--gravy", "gold", "garden" };
+
+ Parser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption(opts.getOption("gravy")) );
+ assertEquals( "gold", cmd.getOptionValue(opts.getOption("gravy")) );
+ assertEquals( "gold", cmd.getOptionValues(opts.getOption("gravy"))[0]
);
+ assertEquals( "garden",
cmd.getOptionValues(opts.getOption("gravy"))[1] );
+ assertEquals( cmd.getArgs().length, 0 );
+ }
@Test
public void testShortOptionalNArgValues() throws Exception
@@ -173,6 +275,22 @@ public class ValueTest
assertEquals( "isotope", cmd.getArgs()[0] );
assertEquals( "ice", cmd.getArgs()[1] );
}
+
+ @Test
+ public void testShortOptionalNArgValuesWithOption() throws Exception
+ {
+ String[] args = new String[] { "-i", "ink", "idea", "isotope", "ice" };
+
+ Parser parser = new PosixParser();
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption("i") );
+ assertEquals( "ink", cmd.getOptionValue(opts.getOption("i")) );
+ assertEquals( "ink", cmd.getOptionValues(opts.getOption("i"))[0] );
+ assertEquals( "idea", cmd.getOptionValues(opts.getOption("i"))[1] );
+ assertEquals( cmd.getArgs().length, 2 );
+ assertEquals( "isotope", cmd.getArgs()[0] );
+ assertEquals( "ice", cmd.getArgs()[1] );
+ }
@Test
public void testLongOptionalNArgValues() throws Exception
@@ -191,4 +309,22 @@ public class ValueTest
assertEquals( cmd.getArgs().length, 1 );
assertEquals( "head", cmd.getArgs()[0] );
}
+
+ @Test
+ public void testLongOptionalNArgValuesWithOption() throws Exception
+ {
+ String[] args = new String[] {
+ "--hide", "house", "hair", "head"
+ };
+
+ Parser parser = new PosixParser();
+
+ CommandLine cmd = parser.parse(opts,args);
+ assertTrue( cmd.hasOption(opts.getOption("hide")) );
+ assertEquals( "house", cmd.getOptionValue(opts.getOption("hide")) );
+ assertEquals( "house", cmd.getOptionValues(opts.getOption("hide"))[0]
);
+ assertEquals( "hair", cmd.getOptionValues(opts.getOption("hide"))[1] );
+ assertEquals( cmd.getArgs().length, 1 );
+ assertEquals( "head", cmd.getArgs()[0] );
+ }
}
Modified:
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI133Test.java
URL:
http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI133Test.java?rev=1788678&r1=1788677&r2=1788678&view=diff
==============================================================================
---
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI133Test.java
(original)
+++
commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI133Test.java
Sat Mar 25 18:32:55 2017
@@ -36,6 +36,6 @@ public class BugCLI133Test
opts.addOption(optionA);
PosixParser posixParser = new PosixParser();
CommandLine line = posixParser.parse(opts, null);
- assertFalse(line.hasOption(null));
+ assertFalse(line.hasOption((String)null));
}
}