Skip to content

Commit f2f2ac3

Browse files
committed
[#1273] Bugfix: call Help.createDefaultOptionRenderer when calculating col widths
Closes #1273
1 parent 1c4a6fd commit f2f2ac3

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ Attributes that are _not_ copied include:
266266
* [#1225] Bugfix: Error message for unmatched positional argument reports incorrect index when value equals a previously matched argument. Thanks to [Vitaly Shukela](https://github.com/vi) for raising this.
267267
* [#1250] Bugfix: Inherited positional parameter should not be overridden by default value if placed after subcommand. Thanks to [Daniel Gray](https://github.com/danielthegray) for the pull request.
268268
* [#1183] Bugfix: Prevent `MissingParameterException` thrown when subcommand has required options and help option is specified on parent command. Thanks to [drkilikil](https://github.com/drkilikil) for raising this.
269+
* [#1273] Bugfix: The `Help.calcLongOptionColumnWidth` now calls `Help.createDefaultOptionRenderer`, so overriding `createDefaultOptionRenderer` uses the correct column width in the options and parameters list.
269270
* [#1215] DOC: User manual improvements, including more tabs with Kotlin source code. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.
270271
* [#1219] DOC: User manual improvements: added more tabs with Kotlin code. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.
271272
* [#1220] DOC: User manual improvements: corrections, more Kotlin tabs. Thanks to [Andreas Deininger](https://github.com/deining) for the pull request.

src/main/java/picocli/CommandLine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15473,7 +15473,7 @@ private Layout createLayout(int longOptionsColumnWidth, ColorScheme aColorScheme
1547315473
* @since 4.6 */
1547415474
public int calcLongOptionColumnWidth(List<OptionSpec> options, List<PositionalParamSpec> positionals, ColorScheme aColorScheme) {
1547515475
int max = 0;
15476-
IOptionRenderer optionRenderer = new DefaultOptionRenderer(false, " ");
15476+
IOptionRenderer optionRenderer = createDefaultOptionRenderer();
1547715477
boolean cjk = commandSpec.usageMessage().adjustLineBreaksForWideCJKCharacters();
1547815478
int longOptionsColWidth = commandSpec.usageMessage().longOptionsMaxWidth() + 1; // add 1 space for indentation
1547915479
for (OptionSpec option : options) {

src/test/java/picocli/HelpTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5005,4 +5005,56 @@ class MyTool {
50055005
String actual = new CommandLine(new MyTool()).getUsageMessage();
50065006
assertEquals(expected, actual);
50075007
}
5008+
@Test
5009+
public void testIssue1273() {
5010+
class Issue1273 {
5011+
@Option(names = {"-u", "--user"}, description = "The connecting user name.")
5012+
private String user;
5013+
5014+
@Option(names = {"-p", "--password"}, interactive = true, description = "Password for the user.")
5015+
private String password;
5016+
5017+
@Option(names = {"-o", "--port"}, description = "Listening port.")
5018+
private int port;
5019+
}
5020+
5021+
CommandLine cmd = new CommandLine(new Issue1273());
5022+
cmd.setHelpFactory(new IHelpFactory() {
5023+
public Help create(final CommandSpec commandSpec, ColorScheme colorScheme) {
5024+
return new Help(commandSpec, colorScheme) {
5025+
@Override
5026+
public IOptionRenderer createDefaultOptionRenderer() {
5027+
return new IOptionRenderer() {
5028+
public Text[][] render(OptionSpec option, IParamLabelRenderer ignored, ColorScheme scheme) {
5029+
// assume one line of description text (may contain embedded %n line separators)
5030+
String[] description = option.description();
5031+
Text[] descriptionFirstLines = scheme.text(description[0]).splitLines();
5032+
5033+
Text EMPTY = Ansi.EMPTY_TEXT;
5034+
List<Text[]> result = new ArrayList<Text[]>();
5035+
result.add(new Text[] {
5036+
scheme.optionText(String.valueOf(
5037+
option.command().usageMessage().requiredOptionMarker())),
5038+
scheme.optionText(option.shortestName()),
5039+
scheme.text(","), // we assume every option has a short and a long name
5040+
scheme.optionText(option.longestName()), // just the option name without parameter
5041+
descriptionFirstLines[0] });
5042+
for (int i = 1; i < descriptionFirstLines.length; i++) {
5043+
result.add(new Text[] { EMPTY, EMPTY, EMPTY, EMPTY, descriptionFirstLines[i] });
5044+
}
5045+
return result.toArray(new Text[result.size()][]);
5046+
}
5047+
};
5048+
}
5049+
};
5050+
}
5051+
});
5052+
String expected = String.format("" +
5053+
"Usage: <main class> [-p] [-o=<port>] [-u=<user>]%n" +
5054+
" -o, --port Listening port.%n" +
5055+
" -p, --password Password for the user.%n" +
5056+
" -u, --user The connecting user name.%n");
5057+
String actual = cmd.getUsageMessage();
5058+
assertEquals(expected, actual);
5059+
}
50085060
}

0 commit comments

Comments
 (0)