Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/cli/help/TextStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public CharSequence pad(final boolean addIndent, final CharSequence text) {
rest = "";
} else {
int restLen = maxWidth - text.length();
if (addIndent && restLen > indent) {
if (addIndent && restLen >= indent) {
indentPad = Util.repeatSpace(indent);
restLen -= indent;
} else {
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/apache/commons/cli/help/HelpFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,39 @@ void testPrintOptions() throws IOException {
assertEquals(expected, actual);
}

/**
* Continuation description lines must share the same indent.
*
* @see <a href="https://issues.apache.org/jira/browse/CLI-354">[CLI-354] HelpFormatter: Description indentation is incorrect</a>
*/
@Test
void testPrintHelpWrappedDescriptionIndent() throws IOException {
final StringBuilder sb = new StringBuilder();
final TextHelpAppendable serializer = new TextHelpAppendable(sb);
final HelpFormatter formatter = HelpFormatter.builder().setHelpAppendable(serializer).setShowSince(false).get();
final String description = "an argument passed to the remote command. The value will be wrapped in double quotes "
+ "and appended to the command-line. This option can be added multiple times.";
final Options options = new Options().addOption(Option.builder("V").longOpt("argument-value").hasArg().desc(description).get());

final List<String> expected = new ArrayList<>();
expected.add(" usage: cs [-V <arg>]");
expected.add("");
expected.add(" header");
expected.add("");
expected.add(" Options Description ");
expected.add(" -V, --argument-value <arg> an argument passed to the remote command. ");
expected.add(" The value will be wrapped in double quotes");
expected.add(" and appended to the command-line. This ");
expected.add(" option can be added multiple times. ");
expected.add("");
expected.add(" footer");
expected.add("");

formatter.printHelp("cs", "header", options, "footer", true);
final List<String> actual = IOUtils.readLines(new StringReader(sb.toString()));
assertEquals(expected, actual);
}

@Test
void testSetOptionFormatBuilderTest() {
final HelpFormatter.Builder underTest = HelpFormatter.builder();
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/apache/commons/cli/help/TextStyleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ static Stream<Arguments> padTestData() {
builder.setAlignment(TextStyle.Alignment.CENTER);
lst.add(Arguments.of(builder.get(), " Hello world ", " Hello world "));

// width equal to text length + indent applies indent on continuation lines
builder.setMaxWidth(16);
builder.setAlignment(TextStyle.Alignment.LEFT);
lst.add(Arguments.of(builder.get(), "Hello world ", " Hello world"));

builder.setAlignment(TextStyle.Alignment.RIGHT);
lst.add(Arguments.of(builder.get(), " Hello world", " Hello world"));

builder.setAlignment(TextStyle.Alignment.CENTER);
lst.add(Arguments.of(builder.get(), " Hello world ", " Hello world "));

// width greater than text length and less than text length + indent creates result of text length + pad
builder.setMaxWidth(14);
builder.setAlignment(TextStyle.Alignment.LEFT);
Expand Down
Loading