Skip to content

Commit

Permalink
[#1870] Bugfix: prevent StringIndexOutOfBoundsException in usage he…
Browse files Browse the repository at this point in the history
…lp when subcommand has many long aliases

Closes #1870
  • Loading branch information
remkop committed Jan 25, 2023
1 parent 1956a65 commit 3c3c670
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Artifacts in this release are signed by Remko Popma (6601 E5C0 8DCC BB96).
* [#1886][#1896] Bugfix: AsciiDoc generator now correctly outputs options even if all options are in ArgGroups. Thanks to [Ruud Senden](https://github.com/rsenden) for the discussion and the pull request.
* [#1878][#1876] Bugfix: Annotation processor now avoids loading resource bundles at compile time. Thanks to [Ruud Senden](https://github.com/rsenden) for the discussion and the pull request.
* [#1911] Avoid using boxed boolean in `CommandLine.Interpreter.applyValueToSingleValuedField`. Thanks to [Jiehong](https://github.com/Jiehong) for the pull request.
* [#1870] Bugfix: `StringIndexOutOfBoundsException` in usage help when command has too many (and long) aliases. Thanks to [Martin](https://github.com/martlin2cz) for raising this.ji
* [#1930] Bugfix: Ensure tests pass in environments for Java 5-18.
* [#1881] DOC: Many documentation improvements. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.
* [#1855][#1857] DOC: Add new user manual section called [Rare Use Cases](https://picocli.info/#_rare_use_cases) detailing `System.exit` usage. Thanks to [Tadaya Tsuyukubo](https://github.com/ttddyy) for the pull request.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -16340,7 +16340,8 @@ public String commandList() {
* @since 4.4 */
public String commandList(Map<String, Help> subcommands) {
if (subcommands.isEmpty()) { return ""; }
int commandLength = maxLength(subcommands.keySet());
int maxCommandLength = width() / 2;
int commandLength = Math.min(maxLength(subcommands.keySet()), maxCommandLength);
Help.TextTable textTable = Help.TextTable.forColumns(colorScheme().ansi(),
new Help.Column(commandLength + 2, 2, Help.Column.Overflow.SPAN),
new Help.Column(width() - (commandLength + 2), 2, Help.Column.Overflow.WRAP));
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/picocli/HelpSubCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import static java.lang.String.format;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -501,4 +502,32 @@ public void testHelpAllSubcommands() {
assertEquals(1, help.subcommands().size());
assertEquals(new HashSet<String>(Arrays.asList("foo")), help.subcommands().keySet());
}

@Command(name = "import-from-excel",
aliases = { "import-from-xls", "import-from-xlsx", "import-from-csv", "import-from-txt" },
description = "Imports the data from various excel files (xls, xlsx, csv or even txt).")
static class ImportCommand implements Runnable {

@Parameters(arity = "1..*")
public List<String> files;

@Override
public void run() {
System.out.println("ImportCommand.run()");
}
}
@Test
public void testIssue1870StringIndexOutOfBounds() {
@Command(name = "top", subcommands = ImportCommand.class) class Top { }
String actual = usageString(new CommandLine(new Top()), Help.Ansi.OFF);
String expected = String.format("" +
"Usage: top [COMMAND]%n" +
"Commands:%n" +
" import-from-excel, import-from-xls, import-from-xlsx, import-from-csv,%n" +
" import-from-txt%n" +
" Imports the data from various excel%n" +
" files (xls, xlsx, csv or even%n" +
" txt).%n");
assertEquals(expected, actual);
}
}

0 comments on commit 3c3c670

Please sign in to comment.