From 0e7508c9ae0a5b0663f7cfb6a8f099a8f7973668 Mon Sep 17 00:00:00 2001 From: rpopma Date: Thu, 5 Oct 2017 00:41:53 +0900 Subject: [PATCH] #194 Better usage help for split regex Closes #194 --- RELEASE-NOTES.md | 1 + src/main/java/picocli/CommandLine.java | 2 ++ src/test/java/picocli/CommandLineHelpTest.java | 10 ++++++---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index e2b8f88f9..aaaa65403 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -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). diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 71afe1b0d..e0604ff0d 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -3173,9 +3173,11 @@ public DefaultParamLabelRenderer(String separator) { public Text renderParameterLabel(Field field, Ansi ansi, List 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 = " "; diff --git a/src/test/java/picocli/CommandLineHelpTest.java b/src/test/java/picocli/CommandLineHelpTest.java index b05dc8656..9b0a5f490 100644 --- a/src/test/java/picocli/CommandLineHelpTest.java +++ b/src/test/java/picocli/CommandLineHelpTest.java @@ -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 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 map; } String actual = usageString(new App(), Help.Ansi.OFF); String expected = String.format("" + - "Usage:
[-P=TIMEUNIT=VALUE]... FIXTAG=VALUE FIXTAG=VALUE%n" + + "Usage:
[-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);