Skip to content

Commit 028893d

Browse files
author
Oliver Heger
committed
CLI-124: Provide better support for displaying usage information for optional child groups.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@681185 13f79535-47bb-0310-9956-ffa450edef68
1 parent 34d2ee9 commit 028893d

5 files changed

Lines changed: 86 additions & 32 deletions

File tree

src/java/org/apache/commons/cli2/DisplaySetting.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public class DisplaySetting {
3131
private static final Set all = new HashSet();
3232

3333
/**
34-
* A Set guarenteed to contain all possible DisplaySetting values
34+
* A Set guaranteed to contain all possible DisplaySetting values
3535
*/
3636
public static final Set ALL = Collections.unmodifiableSet(all);
3737

3838
/**
39-
* A Set guarenteed to contain no DisplaySetting values
39+
* A Set guaranteed to contain no DisplaySetting values
4040
*/
4141
public static final Set NONE = Collections.EMPTY_SET;
4242

@@ -52,6 +52,13 @@ public class DisplaySetting {
5252
public static final DisplaySetting DISPLAY_OPTIONAL =
5353
new DisplaySetting("DISPLAY_OPTIONAL");
5454

55+
/**
56+
* Indicates that optional child groups should be displayed in square
57+
* brackets.
58+
*/
59+
public static final DisplaySetting DISPLAY_OPTIONAL_CHILD_GROUP =
60+
new DisplaySetting("DISPLAY_OPTIONAL_CHILD_GROUP");
61+
5562
/**
5663
* Indicates that property options should be included
5764
*/

src/java/org/apache/commons/cli2/option/GroupImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,9 @@ public void appendUsage(final StringBuffer buffer,
304304
final String separator) {
305305
final Set helpSettingsCopy = new HashSet(helpSettings);
306306

307-
final boolean optional =
308-
(minimum == 0) && helpSettingsCopy.contains(DisplaySetting.DISPLAY_OPTIONAL);
307+
final boolean optional = !isRequired()
308+
&& (helpSettingsCopy.contains(DisplaySetting.DISPLAY_OPTIONAL) ||
309+
helpSettingsCopy.contains(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP));
309310

310311
final boolean expanded =
311312
(name == null) || helpSettingsCopy.contains(DisplaySetting.DISPLAY_GROUP_EXPANDED);

src/java/org/apache/commons/cli2/util/HelpFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.commons.cli2.util;
1818

1919
import java.io.PrintWriter;
20-
2120
import java.util.ArrayList;
2221
import java.util.Collections;
2322
import java.util.Comparator;
@@ -84,6 +83,7 @@ public class HelpFormatter {
8483
final Set fullUsage = new HashSet(DisplaySetting.ALL);
8584
fullUsage.remove(DisplaySetting.DISPLAY_ALIASES);
8685
fullUsage.remove(DisplaySetting.DISPLAY_GROUP_NAME);
86+
fullUsage.remove(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
8787
DEFAULT_FULL_USAGE_SETTINGS = Collections.unmodifiableSet(fullUsage);
8888

8989
final Set lineUsage = new HashSet();

src/test/org/apache/commons/cli2/option/GroupTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,34 @@ public void testAppendUsage_WithArgs() {
344344
assertEquals("[ant (--help (-?,-h)) [<target1> [<target2> ...]]]", buffer.toString());
345345
}
346346

347+
public void testAppendUsage_OptionalChildGroup() {
348+
final Option option = buildRequiredTestGroup(false, 2).getParent();
349+
final StringBuffer buffer = new StringBuffer();
350+
final Set settings = new HashSet(DisplaySetting.ALL);
351+
option.appendUsage(buffer, settings, null);
352+
353+
assertEquals("[parent ([test ()])]", buffer.toString());
354+
}
355+
356+
public void testAppendUsage_OptionalChildGroupNoSetting() {
357+
final Option option = buildRequiredTestGroup(false, 2).getParent();
358+
final StringBuffer buffer = new StringBuffer();
359+
final Set settings = new HashSet(DisplaySetting.ALL);
360+
settings.remove(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
361+
option.appendUsage(buffer, settings, null);
362+
363+
assertEquals("[parent (test ())]", buffer.toString());
364+
}
365+
366+
public void testAppendUsage_RequiredChildGroup() {
367+
final Option option = buildRequiredTestGroup(true, 2).getParent();
368+
final StringBuffer buffer = new StringBuffer();
369+
final Set settings = new HashSet(DisplaySetting.ALL);
370+
option.appendUsage(buffer, settings, null);
371+
372+
assertEquals("[parent (test ())]", buffer.toString());
373+
}
374+
347375
/*
348376
* (non-Javadoc)
349377
*

src/test/org/apache/commons/cli2/option/NestedGroupTest.java

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@
1616
*/
1717
package org.apache.commons.cli2.option;
1818

19+
import java.io.BufferedReader;
20+
import java.io.IOException;
21+
import java.io.PrintWriter;
22+
import java.io.StringReader;
23+
import java.io.StringWriter;
24+
import java.util.ArrayList;
25+
import java.util.Arrays;
26+
import java.util.HashSet;
27+
import java.util.List;
28+
import java.util.Set;
29+
1930
import org.apache.commons.cli2.CLITestCase;
2031
import org.apache.commons.cli2.CommandLine;
32+
import org.apache.commons.cli2.DisplaySetting;
2133
import org.apache.commons.cli2.Group;
2234
import org.apache.commons.cli2.OptionException;
2335
import org.apache.commons.cli2.builder.ArgumentBuilder;
@@ -26,20 +38,28 @@
2638
import org.apache.commons.cli2.commandline.Parser;
2739
import org.apache.commons.cli2.util.HelpFormatter;
2840

29-
import java.io.BufferedReader;
30-
import java.io.IOException;
31-
import java.io.PrintWriter;
32-
import java.io.StringReader;
33-
import java.io.StringWriter;
34-
35-
import java.util.ArrayList;
36-
import java.util.List;
37-
3841

3942
/**
4043
* Test to exercise nested groups developed to demonstrate bug 32533
4144
*/
4245
public class NestedGroupTest extends CLITestCase {
46+
private static final String[] EXPECTED_USAGE = new String[] {
47+
"Usage: ",
48+
" [-h -k -e|-d -b|-3 -f <file>|-s <string>] ",
49+
"encryptionService ",
50+
" -h (--help) Print this message ",
51+
" -k (--key) Encryption key ",
52+
" Action Action ",
53+
" -e (--encrypt) Encrypt input ",
54+
" -d (--decrypt) Decrypt input ",
55+
" Algorithm Encryption Algorithm ",
56+
" -b (--blowfish) Blowfish ",
57+
" -3 (--3DES) Triple DES ",
58+
" Input Input ",
59+
" -f (--file) file Input file ",
60+
" -s (--string) string Input string "
61+
};
62+
4363
final static DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
4464
final static ArgumentBuilder abuilder = new ArgumentBuilder();
4565
final static GroupBuilder gbuilder = new GroupBuilder();
@@ -127,13 +147,27 @@ public void testNestedGroup()
127147
}
128148

129149
public void testNestedGroupHelp() {
150+
checkNestedGroupHelp(new HelpFormatter(), EXPECTED_USAGE);
151+
}
152+
153+
public void testNestedGroupHelpOptional()
154+
{
155+
HelpFormatter helpFormatter = new HelpFormatter();
156+
Set dispOptions = new HashSet(helpFormatter.getFullUsageSettings());
157+
dispOptions.add(DisplaySetting.DISPLAY_OPTIONAL_CHILD_GROUP);
158+
List expLines = new ArrayList(Arrays.asList(EXPECTED_USAGE));
159+
expLines.set(1," [-h -k -e|-d [-b|-3] -f <file>|-s <string>] ");
160+
helpFormatter.setFullUsageSettings(dispOptions);
161+
checkNestedGroupHelp(helpFormatter, (String[]) expLines
162+
.toArray(new String[expLines.size()]));
163+
}
164+
165+
private void checkNestedGroupHelp(HelpFormatter helpFormatter, String[] expected) {
130166
Group[] nestedGroups = {
131167
buildActionGroup(),
132168
buildAlgorithmGroup(),
133169
buildInputGroup()
134170
};
135-
136-
HelpFormatter helpFormatter = new HelpFormatter();
137171
helpFormatter.setGroup(buildEncryptionServiceGroup(nestedGroups));
138172

139173
final StringWriter out = new StringWriter();
@@ -144,22 +178,6 @@ public void testNestedGroupHelp() {
144178

145179
final BufferedReader bufferedReader = new BufferedReader(new StringReader(
146180
out.toString()));
147-
final String[] expected = new String[] {
148-
"Usage: ",
149-
" [-h -k -e|-d -b|-3 -f <file>|-s <string>] ",
150-
"encryptionService ",
151-
" -h (--help) Print this message ",
152-
" -k (--key) Encryption key ",
153-
" Action Action ",
154-
" -e (--encrypt) Encrypt input ",
155-
" -d (--decrypt) Decrypt input ",
156-
" Algorithm Encryption Algorithm ",
157-
" -b (--blowfish) Blowfish ",
158-
" -3 (--3DES) Triple DES ",
159-
" Input Input ",
160-
" -f (--file) file Input file ",
161-
" -s (--string) string Input string "
162-
};
163181

164182
List actual = new ArrayList(expected.length);
165183
String input;

0 commit comments

Comments
 (0)