From 10b33a398e8205bd811cc78b81d7c21be0c84bbb Mon Sep 17 00:00:00 2001 From: bbottema Date: Fri, 17 Aug 2018 19:10:53 +0200 Subject: [PATCH] #441: Renamed fixParamLabel -> showParamSyntax, fixed javadoc entry and taking into account separator instead of using hardcoded space " " --- src/main/java/picocli/CommandLine.java | 21 +++++++------- .../java/picocli/CommandLineModelTest.java | 28 +++++++++++++++---- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 60cf42fa5..3ff6e5bf0 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -3941,7 +3941,7 @@ public abstract static class ArgSpec { // help-related fields private final boolean hidden; private final String paramLabel; - private final boolean fixParamLabel; + private final boolean showParamSyntax; private final String[] description; private final Help.Visibility showDefaultValue; @@ -3970,7 +3970,7 @@ private > ArgSpec(Builder builder) { description = builder.description == null ? new String[0] : builder.description; splitRegex = builder.splitRegex == null ? "" : builder.splitRegex; paramLabel = empty(builder.paramLabel) ? "PARAM" : builder.paramLabel; - fixParamLabel = builder.fixParamLabel; + showParamSyntax = builder.showParamSyntax; converters = builder.converters == null ? new ITypeConverter[0] : builder.converters; showDefaultValue = builder.showDefaultValue == null ? Help.Visibility.ON_DEMAND : builder.showDefaultValue; hidden = builder.hidden; @@ -4075,8 +4075,8 @@ public String[] renderedDescription() { public String paramLabel() { return paramLabel; } /** Indicates whether paramLabel should be processed the regular way or that it should be rendered as-is. - * @see #paramLabel() {@link #fixParamLabel()} */ - public boolean fixParamLabel() { return fixParamLabel; } + * @see #paramLabel() {@link #paramLabel()} */ + public boolean showParamSyntax() { return showParamSyntax; } /** Returns auxiliary type information used when the {@link #type()} is a generic {@code Collection}, {@code Map} or an abstract class. * @see Option#type() */ @@ -4245,7 +4245,7 @@ abstract static class Builder> { private boolean required; private boolean interactive; private String paramLabel; - private boolean fixParamLabel; + private boolean showParamSyntax = true; private String splitRegex; private boolean hidden; private Class type; @@ -4271,7 +4271,7 @@ abstract static class Builder> { setter = original.setter; hidden = original.hidden; paramLabel = original.paramLabel; - fixParamLabel = original.fixParamLabel; + showParamSyntax = original.showParamSyntax; required = original.required; interactive = original.interactive; showDefaultValue = original.showDefaultValue; @@ -4366,9 +4366,10 @@ abstract static class Builder> { /** Sets the name of the option or positional parameter used in the usage help message, and returns this builder. */ public T paramLabel(String paramLabel) { this.paramLabel = Assert.notNull(paramLabel, "paramLabel"); return self(); } - - /** Sets the name of the option or positional parameter used in the usage help message, and returns this builder. */ - public T fixParamLabel(boolean fixParamLabel) { this.fixParamLabel = fixParamLabel; return self(); } + + /** Indicates whether paramLabel should be processed the regular way or that it should be rendered as-is. + * @see #paramLabel() {@link #paramLabel()} */ + public T showParamSyntax(boolean showParamSyntax) { this.showParamSyntax = showParamSyntax; return self(); } /** Sets auxiliary type information, and returns this builder. * @param types the element type(s) when the {@link #type()} is a generic {@code Collection} or a {@code Map}; @@ -7714,7 +7715,7 @@ public DefaultParamLabelRenderer(CommandSpec commandSpec) { public Text renderParameterLabel(ArgSpec argSpec, Ansi ansi, List styles) { Range capacity = argSpec.isOption() ? argSpec.arity() : ((PositionalParamSpec)argSpec).capacity(); if (capacity.max == 0) { return ansi.new Text(""); } - if (argSpec.fixParamLabel()) { return ansi.apply(" " + argSpec.paramLabel(), styles); } + if (!argSpec.showParamSyntax()) { return ansi.apply(separator() + argSpec.paramLabel(), styles); } Text paramName = ansi.apply(argSpec.paramLabel(), styles); String split = argSpec.splitRegex(); diff --git a/src/test/java/picocli/CommandLineModelTest.java b/src/test/java/picocli/CommandLineModelTest.java index 3cca2e964..b51bf2f39 100644 --- a/src/test/java/picocli/CommandLineModelTest.java +++ b/src/test/java/picocli/CommandLineModelTest.java @@ -59,19 +59,37 @@ public void testEmptyModelParse() throws Exception { commandLine.parse("-p", "123", "abc"); assertEquals(Arrays.asList("-p", "123", "abc"), commandLine.getUnmatchedArguments()); } - + @Test public void testModelUsageHelp() throws Exception { CommandSpec spec = CommandSpec.create(); spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); - spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(=BOOLEAN)").arity("1").fixParamLabel(true).required(true).description("run with fixed option").build()); + spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(BOOLEAN)").arity("1").showParamSyntax(false).required(true).description("run with fixed option").build()); CommandLine commandLine = new CommandLine(spec); String actual = usageString(commandLine, Ansi.OFF); String expected = String.format("" + - "Usage:
[-hV] [-c=COUNT] -f FIXED(=BOOLEAN)%n" + - " -c, --count=COUNT number of times to execute%n" + + "Usage:
[-hV] [-c=COUNT] -f=FIXED(BOOLEAN)%n" + + " -c, --count=COUNT number of times to execute%n" + + " -f, --fix=FIXED(BOOLEAN) run with fixed option%n" + + " -h, --help show help and exit%n" + + " -V, --version show help and exit%n"); + assertEquals(expected, actual); + } + + @Test + public void testModelUsageHelpWithCustomSeparator() throws Exception { + CommandSpec spec = CommandSpec.create(); + spec.addOption(OptionSpec.builder("-h", "--help").usageHelp(true).description("show help and exit").build()); + spec.addOption(OptionSpec.builder("-V", "--version").versionHelp(true).description("show help and exit").build()); + spec.addOption(OptionSpec.builder("-c", "--count").paramLabel("COUNT").arity("1").type(int.class).description("number of times to execute").build()); + spec.addOption(OptionSpec.builder("-f", "--fix").paramLabel("FIXED(=BOOLEAN)").arity("1").showParamSyntax(false).required(true).description("run with fixed option").build()); + CommandLine commandLine = new CommandLine(spec).setSeparator(" "); + String actual = usageString(commandLine, Ansi.OFF); + String expected = String.format("" + + "Usage:
[-hV] [-c COUNT] -f FIXED(=BOOLEAN)%n" + + " -c, --count COUNT number of times to execute%n" + " -f, --fix FIXED(=BOOLEAN)%n" + " run with fixed option%n" + " -h, --help show help and exit%n" + @@ -499,7 +517,7 @@ public void testPositionalDefaultRequiredIsFalse() throws Exception { } @Test public void testPositionalDefaultFixParamLabelIsFalse() throws Exception { - assertFalse(PositionalParamSpec.builder().build().fixParamLabel()); + assertTrue(PositionalParamSpec.builder().build().showParamSyntax()); } @Test