Skip to content

Commit

Permalink
remkop#441: Renamed fixParamLabel -> showParamSyntax, fixed javadoc e…
Browse files Browse the repository at this point in the history
…ntry and taking into account separator instead of using hardcoded space " "
  • Loading branch information
bbottema committed Aug 17, 2018
1 parent 6716e36 commit 10b33a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
21 changes: 11 additions & 10 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -3970,7 +3970,7 @@ private <T extends Builder<T>> ArgSpec(Builder<T> 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;
Expand Down Expand Up @@ -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() */
Expand Down Expand Up @@ -4245,7 +4245,7 @@ abstract static class Builder<T extends Builder<T>> {
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;
Expand All @@ -4271,7 +4271,7 @@ abstract static class Builder<T extends Builder<T>> {
setter = original.setter;
hidden = original.hidden;
paramLabel = original.paramLabel;
fixParamLabel = original.fixParamLabel;
showParamSyntax = original.showParamSyntax;
required = original.required;
interactive = original.interactive;
showDefaultValue = original.showDefaultValue;
Expand Down Expand Up @@ -4366,9 +4366,10 @@ abstract static class Builder<T extends Builder<T>> {

/** 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};
Expand Down Expand Up @@ -7714,7 +7715,7 @@ public DefaultParamLabelRenderer(CommandSpec commandSpec) {
public Text renderParameterLabel(ArgSpec argSpec, Ansi ansi, List<IStyle> 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();
Expand Down
28 changes: 23 additions & 5 deletions src/test/java/picocli/CommandLineModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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: <main class> [-hV] [-c=COUNT] -f FIXED(=BOOLEAN)%n" +
" -c, --count=COUNT number of times to execute%n" +
"Usage: <main class> [-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: <main class> [-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" +
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 10b33a3

Please sign in to comment.