Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/main/java/org/apache/commons/cli/HelpFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,10 @@ <A extends Appendable> A appendOptions(final A sb, final int width, final Option
// -a is opt and --aaa is long opt; in parallel look for
// the longest opt string this list will be then used to
// sort options ascending
int max = 0;
final int maxSince = showSince ? determineMaxSinceLength(options) + leftPad : 0;
// determineMaxSinceLength() already reserves the width of the "Since" header; the option column needs the
// same floor for the "Options" header, otherwise the gap in front of "Since" below is computed negative.
int max = showSince ? HEADER_OPTIONS.length() + maxSince : 0;
final List<StringBuilder> prefixList = new ArrayList<>();
final List<Option> optList = options.helpOptions();
if (getOptionComparator() != null) {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/apache/commons/cli/HelpFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,23 @@ void testPrintHelpWithSince() {
assertArrayEquals(expected, baos.toString().split(System.lineSeparator()));
}

@Test
void testPrintHelpWithSinceShortOptions() {
// the option column is narrower than the "Options" header
final String[] expected = {"usage: Command syntax", "Header", "Options Since Description", " -n - Description for n",
" -W 1.19.0 Descripton for W", "footer"};
final Options options = new Options()
.addOption(Option.builder("W").since("1.19.0").desc("Descripton for W").get())
.addOption(Option.builder("n").desc("Description for n").get());

final HelpFormatter formatter = HelpFormatter.builder().setShowSince(true).get();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos))) {
formatter.printHelp(pw, 80, "Command syntax", "Header", options, 2, 5, "footer", false);
}
assertArrayEquals(expected, baos.toString().split(System.lineSeparator()));
}

@Test
void testPrintOptionGroupUsage() {
final OptionGroup optionGroup = new OptionGroup();
Expand Down