Skip to content

Commit e4925b2

Browse files
author
Robert James Oxspring
committed
HelpFormatter now applys a minimum description width of 1 character
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@130105 13f79535-47bb-0310-9956-ffa450edef68
1 parent cb93afb commit e4925b2

2 files changed

Lines changed: 64 additions & 19 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class HelpFormatter {
8888
final Set lineUsage = new HashSet();
8989
lineUsage.add(DisplaySetting.DISPLAY_ALIASES);
9090
lineUsage.add(DisplaySetting.DISPLAY_GROUP_NAME);
91+
lineUsage.add(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
9192
DEFAULT_LINE_USAGE_SETTINGS = Collections.unmodifiableSet(lineUsage);
9293

9394
final Set displayUsage = new HashSet(DisplaySetting.ALL);
@@ -221,8 +222,8 @@ public void printHelp() throws IOException {
221222
for (int i = 0; i < usageWidth; i++) {
222223
blankBuffer.append(' ');
223224
}
224-
final int descriptionWidth =
225-
pageWidth - gutterCenter.length() - usageWidth;
225+
final int descriptionWidth = Math.max(1,
226+
pageWidth - gutterCenter.length() - usageWidth);
226227
for (final Iterator i = helpLines.iterator(); i.hasNext();) {
227228
final HelpLine helpLine = (HelpLine)i.next();
228229
final List descriptionLines =
@@ -347,6 +348,10 @@ protected static void pad(
347348
}
348349

349350
protected static List wrap(final String text, final int width) {
351+
if(width<1){
352+
throw new IllegalArgumentException("width must be positive");
353+
}
354+
350355
if (text == null) {
351356
return Collections.singletonList("");
352357
}

src/test/org/apache/commons/cli2/util/HelpFormatterTest.java

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import junit.framework.TestCase;
2626

27+
import org.apache.commons.cli2.Group;
2728
import org.apache.commons.cli2.Option;
2829
import org.apache.commons.cli2.OptionException;
2930
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
@@ -34,6 +35,7 @@
3435
public class HelpFormatterTest extends TestCase {
3536
private HelpFormatter helpFormatter;
3637
private Option verbose;
38+
private Group options;
3739

3840
public void setUp() {
3941
helpFormatter = new HelpFormatter("|*", "*-*", "*|", 80);
@@ -49,23 +51,24 @@ public void setUp() {
4951
.withDescription("print the version information and exit")
5052
.create();
5153

52-
helpFormatter.setGroup(
53-
new GroupBuilder()
54-
.withName("options")
55-
.withOption(DefaultOptionTest.buildHelpOption())
56-
.withOption(ArgumentTest.buildTargetsArgument())
57-
.withOption(
58-
new DefaultOptionBuilder()
59-
.withLongName("diagnostics")
60-
.withDescription("print information that might be helpful to diagnose or report problems.")
61-
.create())
62-
.withOption(
63-
new DefaultOptionBuilder()
64-
.withLongName("projecthelp")
65-
.withDescription("print project help information")
66-
.create())
67-
.withOption(verbose)
68-
.create());
54+
options = new GroupBuilder()
55+
.withName("options")
56+
.withOption(DefaultOptionTest.buildHelpOption())
57+
.withOption(ArgumentTest.buildTargetsArgument())
58+
.withOption(
59+
new DefaultOptionBuilder()
60+
.withLongName("diagnostics")
61+
.withDescription("print information that might be helpful to diagnose or report problems.")
62+
.create())
63+
.withOption(
64+
new DefaultOptionBuilder()
65+
.withLongName("projecthelp")
66+
.withDescription("print project help information")
67+
.create())
68+
.withOption(verbose)
69+
.create();
70+
71+
helpFormatter.setGroup(options);
6972
}
7073

7174
public void testPrint() throws IOException {
@@ -190,6 +193,27 @@ public void testPrintHelp_WithException() throws IOException {
190193
assertNull(reader.readLine());
191194
}
192195

196+
public void testPrintHelp_TooNarrow() throws IOException {
197+
final StringWriter writer = new StringWriter();
198+
helpFormatter = new HelpFormatter("<","=",">",4);
199+
helpFormatter.setGroup(options);
200+
helpFormatter.setPrintWriter(new PrintWriter(writer));
201+
helpFormatter.printHelp();
202+
System.out.println(writer);
203+
final BufferedReader reader =
204+
new BufferedReader(new StringReader(writer.toString()));
205+
assertEquals(
206+
"<options = >",
207+
reader.readLine());
208+
assertEquals(
209+
"< --help (-?,-h) =D>",
210+
reader.readLine());
211+
assertEquals(
212+
"< =i>",
213+
reader.readLine());
214+
// lots more lines unchecked
215+
}
216+
193217
public void testPrintException() throws IOException {
194218
final StringWriter writer = new StringWriter();
195219
helpFormatter.setPrintWriter(new PrintWriter(writer));
@@ -322,6 +346,16 @@ public void testWrap_NewLine() {
322346
assertEquals("", i.next());
323347
assertFalse(i.hasNext());
324348
}
349+
350+
public void testWrap_Below1Length() {
351+
try{
352+
HelpFormatter.wrap("Apache Software Foundation",-1);
353+
fail("IllegalArgumentException");
354+
}
355+
catch(IllegalArgumentException e) {
356+
assertEquals("width must be positive",e.getMessage());
357+
}
358+
}
325359

326360
public void testPad() throws IOException {
327361
final StringWriter writer = new StringWriter();
@@ -340,4 +374,10 @@ public void testPad_TooLong() throws IOException {
340374
HelpFormatter.pad("hello world", 10, writer);
341375
assertEquals("hello world", writer.toString());
342376
}
377+
378+
public void testPad_TooShort() throws IOException {
379+
final StringWriter writer = new StringWriter();
380+
HelpFormatter.pad("hello world", -5, writer);
381+
assertEquals("hello world", writer.toString());
382+
}
343383
}

0 commit comments

Comments
 (0)