Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue: #1708] Including log info for invalid values scenario #1723

Merged
merged 3 commits into from
Jun 27, 2022
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
7 changes: 5 additions & 2 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7824,10 +7824,13 @@ public UsageMessageSpec width(int newValue) {
* @return this {@code UsageMessageSpec} for method chaining
* @since 4.2 */
public UsageMessageSpec longOptionsMaxWidth(int newValue) {
if (newValue >= DEFAULT_USAGE_LONG_OPTIONS_WIDTH && newValue <= width() - DEFAULT_USAGE_LONG_OPTIONS_WIDTH) {
if (newValue < DEFAULT_USAGE_LONG_OPTIONS_WIDTH) {
CommandLine.tracer().info("Invalid usage long options max width %d. Minimum value is %d", newValue, DEFAULT_USAGE_LONG_OPTIONS_WIDTH);
} else if (newValue > width() - DEFAULT_USAGE_LONG_OPTIONS_WIDTH) {
CommandLine.tracer().info("Invalid usage long options max width %d. Value must not exceed width(%d) - %d", newValue , width(), DEFAULT_USAGE_LONG_OPTIONS_WIDTH);
fabio-franco marked this conversation as resolved.
Show resolved Hide resolved
} else {
longOptionsMaxWidth = newValue;
}

return this;
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/picocli/HelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4389,21 +4389,29 @@ public void testSetLongOptionsColumnLengthMinimum_ModifiesSpec() {
public void testUsageMessageSpec_LongOptionsColumnLengthMinimum_Minimum20() {
CommandLine cmd = new CommandLine(CommandSpec.create());
cmd.setUsageHelpLongOptionsMaxWidth(20);
TestUtil.setTraceLevel(CommandLine.TraceLevel.INFO);

CommandLine returnValue = cmd.setUsageHelpLongOptionsMaxWidth(19);

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

String expected = "Invalid usage long options max width 19. Minimum value is 20";
assertTrue(systemErrRule.getLog(), systemErrRule.getLog().contains(expected));
}

@Test
public void testUsageMessageSpec_LongOptionsColumnLengthMinimum_MaxWidthMinus20() {
UsageMessageSpec spec = CommandSpec.create().usageMessage();
spec.longOptionsMaxWidth(spec.width() - 20);
assertEquals(60, spec.longOptionsMaxWidth());
TestUtil.setTraceLevel(CommandLine.TraceLevel.INFO);

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

String expected = "Invalid usage long options max width 61. Value must not exceed width(80) - 20";
assertTrue(systemErrRule.getLog(), systemErrRule.getLog().contains(expected));
}

@Test
Expand Down