You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2008/07/30 21:55:22 UTC
svn commit: r681185 - in /commons/proper/cli/trunk/src:
java/org/apache/commons/cli2/ java/org/apache/commons/cli2/option/
java/org/apache/commons/cli2/util/ test/org/apache/commons/cli2/option/
Author: oheger
Date: Wed Jul 30 12:55:21 2008
New Revision: 681185
URL: http://svn.apache.org/viewvc?rev=681185&view=rev
Log:
CLI-124: Provide better support for displaying usage information for optional child groups.
Modified:
commons/proper/cli/trunk/src/java/org/apache/commons/cli2/DisplaySetting.java
commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java
commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java
commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/DisplaySetting.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/DisplaySetting.java?rev=681185&r1=681184&r2=681185&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/DisplaySetting.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/DisplaySetting.java Wed Jul 30 12:55:21 2008
@@ -31,12 +31,12 @@
private static final Set all = new HashSet();
/**
- * A Set guarenteed to contain all possible DisplaySetting values
+ * A Set guaranteed to contain all possible DisplaySetting values
*/
public static final Set ALL = Collections.unmodifiableSet(all);
/**
- * A Set guarenteed to contain no DisplaySetting values
+ * A Set guaranteed to contain no DisplaySetting values
*/
public static final Set NONE = Collections.EMPTY_SET;
@@ -53,6 +53,13 @@
new DisplaySetting("DISPLAY_OPTIONAL");
/**
+ * Indicates that optional child groups should be displayed in square
+ * brackets.
+ */
+ public static final DisplaySetting DISPLAY_OPTIONAL_CHILD_GROUP =
+ new DisplaySetting("DISPLAY_OPTIONAL_CHILD_GROUP");
+
+ /**
* Indicates that property options should be included
*/
public static final DisplaySetting DISPLAY_PROPERTY_OPTION =
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java?rev=681185&r1=681184&r2=681185&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/GroupImpl.java Wed Jul 30 12:55:21 2008
@@ -304,8 +304,9 @@
final String separator) {
final Set helpSettingsCopy = new HashSet(helpSettings);
- final boolean optional =
- (minimum == 0) && helpSettingsCopy.contains(DisplaySetting.DISPLAY_OPTIONAL);
+ final boolean optional = !isRequired()
+ && (helpSettingsCopy.contains(DisplaySetting.DISPLAY_OPTIONAL) ||
+ helpSettingsCopy.contains(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP));
final boolean expanded =
(name == null) || helpSettingsCopy.contains(DisplaySetting.DISPLAY_GROUP_EXPANDED);
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java?rev=681185&r1=681184&r2=681185&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java Wed Jul 30 12:55:21 2008
@@ -17,7 +17,6 @@
package org.apache.commons.cli2.util;
import java.io.PrintWriter;
-
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -84,6 +83,7 @@
final Set fullUsage = new HashSet(DisplaySetting.ALL);
fullUsage.remove(DisplaySetting.DISPLAY_ALIASES);
fullUsage.remove(DisplaySetting.DISPLAY_GROUP_NAME);
+ fullUsage.remove(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
DEFAULT_FULL_USAGE_SETTINGS = Collections.unmodifiableSet(fullUsage);
final Set lineUsage = new HashSet();
Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java?rev=681185&r1=681184&r2=681185&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/GroupTest.java Wed Jul 30 12:55:21 2008
@@ -344,6 +344,34 @@
assertEquals("[ant (--help (-?,-h)) [<target1> [<target2> ...]]]", buffer.toString());
}
+ public void testAppendUsage_OptionalChildGroup() {
+ final Option option = buildRequiredTestGroup(false, 2).getParent();
+ final StringBuffer buffer = new StringBuffer();
+ final Set settings = new HashSet(DisplaySetting.ALL);
+ option.appendUsage(buffer, settings, null);
+
+ assertEquals("[parent ([test ()])]", buffer.toString());
+ }
+
+ public void testAppendUsage_OptionalChildGroupNoSetting() {
+ final Option option = buildRequiredTestGroup(false, 2).getParent();
+ final StringBuffer buffer = new StringBuffer();
+ final Set settings = new HashSet(DisplaySetting.ALL);
+ settings.remove(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
+ option.appendUsage(buffer, settings, null);
+
+ assertEquals("[parent (test ())]", buffer.toString());
+ }
+
+ public void testAppendUsage_RequiredChildGroup() {
+ final Option option = buildRequiredTestGroup(true, 2).getParent();
+ final StringBuffer buffer = new StringBuffer();
+ final Set settings = new HashSet(DisplaySetting.ALL);
+ option.appendUsage(buffer, settings, null);
+
+ assertEquals("[parent (test ())]", buffer.toString());
+ }
+
/*
* (non-Javadoc)
*
Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java?rev=681185&r1=681184&r2=681185&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/option/NestedGroupTest.java Wed Jul 30 12:55:21 2008
@@ -16,8 +16,20 @@
*/
package org.apache.commons.cli2.option;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
import org.apache.commons.cli2.CLITestCase;
import org.apache.commons.cli2.CommandLine;
+import org.apache.commons.cli2.DisplaySetting;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.OptionException;
import org.apache.commons.cli2.builder.ArgumentBuilder;
@@ -26,20 +38,28 @@
import org.apache.commons.cli2.commandline.Parser;
import org.apache.commons.cli2.util.HelpFormatter;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringReader;
-import java.io.StringWriter;
-
-import java.util.ArrayList;
-import java.util.List;
-
/**
* Test to exercise nested groups developed to demonstrate bug 32533
*/
public class NestedGroupTest extends CLITestCase {
+ private static final String[] EXPECTED_USAGE = new String[] {
+ "Usage: ",
+ " [-h -k -e|-d -b|-3 -f <file>|-s <string>] ",
+ "encryptionService ",
+ " -h (--help) Print this message ",
+ " -k (--key) Encryption key ",
+ " Action Action ",
+ " -e (--encrypt) Encrypt input ",
+ " -d (--decrypt) Decrypt input ",
+ " Algorithm Encryption Algorithm ",
+ " -b (--blowfish) Blowfish ",
+ " -3 (--3DES) Triple DES ",
+ " Input Input ",
+ " -f (--file) file Input file ",
+ " -s (--string) string Input string "
+ };
+
final static DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
final static ArgumentBuilder abuilder = new ArgumentBuilder();
final static GroupBuilder gbuilder = new GroupBuilder();
@@ -127,13 +147,27 @@
}
public void testNestedGroupHelp() {
+ checkNestedGroupHelp(new HelpFormatter(), EXPECTED_USAGE);
+ }
+
+ public void testNestedGroupHelpOptional()
+ {
+ HelpFormatter helpFormatter = new HelpFormatter();
+ Set dispOptions = new HashSet(helpFormatter.getFullUsageSettings());
+ dispOptions.add(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
+ List expLines = new ArrayList(Arrays.asList(EXPECTED_USAGE));
+ expLines.set(1," [-h -k -e|-d [-b|-3] -f <file>|-s <string>] ");
+ helpFormatter.setFullUsageSettings(dispOptions);
+ checkNestedGroupHelp(helpFormatter, (String[]) expLines
+ .toArray(new String[expLines.size()]));
+ }
+
+ private void checkNestedGroupHelp(HelpFormatter helpFormatter, String[] expected) {
Group[] nestedGroups = {
buildActionGroup(),
buildAlgorithmGroup(),
buildInputGroup()
};
-
- HelpFormatter helpFormatter = new HelpFormatter();
helpFormatter.setGroup(buildEncryptionServiceGroup(nestedGroups));
final StringWriter out = new StringWriter();
@@ -144,22 +178,6 @@
final BufferedReader bufferedReader = new BufferedReader(new StringReader(
out.toString()));
- final String[] expected = new String[] {
- "Usage: ",
- " [-h -k -e|-d -b|-3 -f <file>|-s <string>] ",
- "encryptionService ",
- " -h (--help) Print this message ",
- " -k (--key) Encryption key ",
- " Action Action ",
- " -e (--encrypt) Encrypt input ",
- " -d (--decrypt) Decrypt input ",
- " Algorithm Encryption Algorithm ",
- " -b (--blowfish) Blowfish ",
- " -3 (--3DES) Triple DES ",
- " Input Input ",
- " -f (--file) file Input file ",
- " -s (--string) string Input string "
- };
List actual = new ArrayList(expected.length);
String input;