Skip to content

Commit

Permalink
Merge pull request #1712 from fabio-franco/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Jun 19, 2022
2 parents fbc154b + 5a3864c commit 8041ddf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
12 changes: 5 additions & 7 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7820,17 +7820,15 @@ public UsageMessageSpec width(int newValue) {
/**
* Sets the maximum usage help long options column max width to the specified value.
* This value controls the maximum width of the long options column: any positional parameter labels or long options that are longer than the specified value will overflow into the description column, and cause the description to be displayed on the next line.
* @param newValue the new maximum usage help long options column max width. Must be 20 or greater.
* @param newValue the new maximum usage help long options column max width. Must be 20 or greater, otherwise the new value will be ignored.
* @return this {@code UsageMessageSpec} for method chaining
* @throws IllegalArgumentException if the specified long options column max is less than 20
* @since 4.2 */
public UsageMessageSpec longOptionsMaxWidth(int newValue) {
if (newValue < DEFAULT_USAGE_LONG_OPTIONS_WIDTH) {
throw new InitializationException("Invalid usage long options max width " + newValue + ". Minimum value is " + DEFAULT_USAGE_LONG_OPTIONS_WIDTH);
} else if (newValue > width() - DEFAULT_USAGE_LONG_OPTIONS_WIDTH) {
throw new InitializationException("Invalid usage long options max width " + newValue + ". Value must not exceed width(" + width() + ") - " + DEFAULT_USAGE_LONG_OPTIONS_WIDTH);
if (newValue >= DEFAULT_USAGE_LONG_OPTIONS_WIDTH && newValue <= width() - DEFAULT_USAGE_LONG_OPTIONS_WIDTH) {
longOptionsMaxWidth = newValue;
}
longOptionsMaxWidth = newValue; return this;

return this;
}

private int getSysPropertyWidthOrDefault(int defaultWidth, boolean detectTerminalSize) {
Expand Down
22 changes: 10 additions & 12 deletions src/test/java/picocli/HelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4387,25 +4387,23 @@ public void testSetLongOptionsColumnLengthMinimum_ModifiesSpec() {

@Test
public void testUsageMessageSpec_LongOptionsColumnLengthMinimum_Minimum20() {
try {
CommandSpec.create().usageMessage().longOptionsMaxWidth(19);
fail("Expected exception");
} catch (InitializationException ok) {
assertEquals("Invalid usage long options max width 19. Minimum value is 20", ok.getMessage());
}
CommandLine cmd = new CommandLine(CommandSpec.create());
cmd.setUsageHelpLongOptionsMaxWidth(20);

CommandLine returnValue = cmd.setUsageHelpLongOptionsMaxWidth(19);

assertEquals(20, cmd.getUsageHelpLongOptionsMaxWidth());
assertEquals(20, cmd.getCommandSpec().usageMessage().longOptionsMaxWidth());
}

@Test
public void testUsageMessageSpec_LongOptionsColumnLengthMinimum_MaxWidthMinus20() {
UsageMessageSpec spec = CommandSpec.create().usageMessage();
spec.longOptionsMaxWidth(spec.width() - 20);
assertEquals(60, spec.longOptionsMaxWidth());
try {
spec.longOptionsMaxWidth(61);
fail("Expected exception");
} catch (InitializationException ok) {
assertEquals("Invalid usage long options max width 61. Value must not exceed width(80) - 20", ok.getMessage());
}

spec.longOptionsMaxWidth(61);
assertEquals(60, spec.longOptionsMaxWidth());
}

@Test
Expand Down

0 comments on commit 8041ddf

Please sign in to comment.