Skip to content

Commit

Permalink
#194 Better usage help for split regex
Browse files Browse the repository at this point in the history
Closes #194
  • Loading branch information
remkop committed Oct 4, 2017
1 parent 53aba4c commit 0e7508c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Args {
- #191 Bugfix: Arity should not limit the total number of values put in an array or collection. Thanks to [RobertZenz](https://github.com/RobertZenz).
- #186 Bugfix: Confusing usage message for collection options. Thanks to [AlexFalappa](https://github.com/AlexFalappa).
- #181 Bugfix: Incorrect help message was displayed for short options with paramLabel when arity > 1
- #194 Bugfix: Usage help should show split regex for option/parameters
- #195 Enhancement: Usage help should show Map types if paramLabel not specified
- #185 Enhancement: Exception message text for missing options should not use field names but be more descriptive and consistent with usage help. Thanks to [AlexFalappa](https://github.com/AlexFalappa).
- #184 Doc: Improved CommandLine.setSeparator javadoc to clarify that this affects parsing only and link to the `@Command` `separator` annotation attribute. Thanks to [defnull](https://github.com/defnull).
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -3173,9 +3173,11 @@ public DefaultParamLabelRenderer(String separator) {
public Text renderParameterLabel(Field field, Ansi ansi, List<IStyle> styles) {
boolean isOptionParameter = field.isAnnotationPresent(Option.class);
Range arity = isOptionParameter ? Range.optionArity(field) : Range.parameterArity(field);
String split = isOptionParameter ? field.getAnnotation(Option.class).split() : field.getAnnotation(Parameters.class).split();
Text result = ansi.new Text("");
String sep = isOptionParameter ? separator : "";
Text paramName = ansi.apply(renderParameterName(field), styles);
if (!empty(split)) { paramName = paramName.append("[" + split).append(paramName).append("]..."); } // #194
for (int i = 0; i < arity.min; i++) {
result = result.append(sep).append(paramName);
sep = " ";
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/picocli/CommandLineHelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2286,22 +2286,24 @@ class Versioned {}
@Test
public void testMapFieldHelp() throws Exception {
class App {
@Parameters(arity = "2", split = "\\|", type = {Integer.class, String.class},
@Parameters(arity = "2", split = "\\|",
paramLabel = "FIXTAG=VALUE",
description = "Exactly two lists of vertical bar '|'-separated FIXTAG=VALUE pairs.")
Map<Integer,String> message;

@Option(names = {"-P", "-map"}, split = ",", type = {TimeUnit.class, String.class},
@Option(names = {"-P", "-map"}, split = ",",
paramLabel = "TIMEUNIT=VALUE",
description = "Any number of TIMEUNIT=VALUE pairs. These may be specified separately (-PTIMEUNIT=VALUE) or as a comma-separated list.")
Map<TimeUnit, String> map;
}
String actual = usageString(new App(), Help.Ansi.OFF);
String expected = String.format("" +
"Usage: <main class> [-P=TIMEUNIT=VALUE]... FIXTAG=VALUE FIXTAG=VALUE%n" +
"Usage: <main class> [-P=TIMEUNIT=VALUE[,TIMEUNIT=VALUE]...]... FIXTAG=VALUE%n" +
" [\\|FIXTAG=VALUE]... FIXTAG=VALUE[\\|FIXTAG=VALUE]...%n" +
" FIXTAG=VALUE Exactly two lists of vertical bar '|'-separated%n" +
" FIXTAG=VALUE pairs.%n" +
" -P, -map=TIMEUNIT=VALUE Any number of TIMEUNIT=VALUE pairs. These may be%n" +
" -P, -map=TIMEUNIT=VALUE[,TIMEUNIT=VALUE]...%n" +
" Any number of TIMEUNIT=VALUE pairs. These may be%n" +
" specified separately (-PTIMEUNIT=VALUE) or as a%n" +
" comma-separated list.%n");
assertEquals(expected, actual);
Expand Down

0 comments on commit 0e7508c

Please sign in to comment.