From 5c641088ad02ce8e56177c2c11b29b744d63a4fb Mon Sep 17 00:00:00 2001 From: Rob Vesse Date: Tue, 16 Apr 2019 16:33:21 +0100 Subject: [PATCH] Adjust tests to cope with positional args (#91) Starts working on CLI command group usage generator --- .../cli/CliCommandGroupUsageGenerator.java | 45 ++++++++++++++++--- .../help/cli/CliCommandUsageGenerator.java | 5 +-- .../help/common/AbstractUsageGenerator.java | 1 - .../com/github/rvesse/airline/TestHelp.java | 2 +- .../github/rvesse/airline/TestSubGroups.java | 2 +- .../args/positional/TestPositionalArgs.java | 2 +- 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandGroupUsageGenerator.java b/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandGroupUsageGenerator.java index 66649ea49..43870d5ab 100644 --- a/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandGroupUsageGenerator.java +++ b/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandGroupUsageGenerator.java @@ -18,6 +18,9 @@ import java.io.IOException; import java.util.*; +import org.apache.commons.lang3.StringUtils; + +import com.github.rvesse.airline.builder.ParserBuilder; import com.github.rvesse.airline.help.UsageHelper; import com.github.rvesse.airline.help.common.AbstractPrintedCommandGroupUsageGenerator; import com.github.rvesse.airline.help.sections.HelpHint; @@ -26,6 +29,8 @@ import com.github.rvesse.airline.model.CommandMetadata; import com.github.rvesse.airline.model.GlobalMetadata; import com.github.rvesse.airline.model.OptionMetadata; +import com.github.rvesse.airline.model.ParserMetadata; + import static com.github.rvesse.airline.help.UsageHelper.DEFAULT_COMMAND_COMPARATOR; import static com.github.rvesse.airline.help.UsageHelper.DEFAULT_OPTION_COMPARATOR; import static com.github.rvesse.airline.help.UsageHelper.DEFAULT_HINT_COMPARATOR; @@ -86,7 +91,8 @@ protected void usage(GlobalMetadata global, CommandGroupMetadata[] groups, Us * @param groups * Group(s) meta-data * - * @throws IOException Thrown if there is a problem generating usage output + * @throws IOException + * Thrown if there is a problem generating usage output */ protected void outputOptions(UsagePrinter out, GlobalMetadata global, CommandGroupMetadata[] groups) throws IOException { @@ -119,6 +125,20 @@ protected void outputOptions(UsagePrinter out, GlobalMetadata global, Command } } + protected String getArgsUsage(CommandMetadata command) { + StringBuilder builder = new StringBuilder(); + if (command.hasPositionalArguments()) { + builder.append(toUsage(command.getPositionalArguments())); + } + if (command.hasNonPositionalArguments()) { + if (command.hasPositionalArguments()) { + builder.append(' '); + } + builder.append(toUsage(command.getArguments())); + } + return builder.toString(); + } + /** * Outputs a documentation section detailing a usage synopsis * @@ -128,7 +148,8 @@ protected void outputOptions(UsagePrinter out, GlobalMetadata global, Command * Global meta-data * @param groups * Groups meta-data - * @throws IOException Thrown if there is a problem generating usage output + * @throws IOException + * Thrown if there is a problem generating usage output */ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, CommandGroupMetadata[] groups) throws IOException { @@ -137,6 +158,8 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman CommandGroupMetadata group = groups[groups.length - 1]; List commands = sortCommands(group.getCommands()); + ParserMetadata parserConfig = global != null ? global.getParserConfiguration() + : ParserBuilder. defaultConfiguration(); // Populate group info via an extra for loop through commands boolean hasDefaultCommand = group.getDefaultCommand() != null; @@ -156,17 +179,20 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman commonGroupOptions = new ArrayList<>(command.getCommandOptions()); } if (commonGroupArgs == null) { - commonGroupArgs = (command.getArguments() != null ? toUsage(command.getArguments()) : ""); + commonGroupArgs = getArgsUsage(command); } commonGroupOptions.retainAll(command.getCommandOptions()); if (command.getCommandOptions().size() > commonGroupOptions.size()) { hasCommandSpecificOptions = true; } - if (commonGroupArgs != (command.getArguments() != null ? toUsage(command.getArguments()) : "")) { + if (!StringUtils.equals(commonGroupArgs, getArgsUsage(command))) { hasCommandSpecificArgs = true; } } + if (commands.size() == 1) { + hasCommandSpecificArgs = true; + } for (CommandGroupMetadata subGroup : group.getSubGroups()) { groupNames.add(subGroup.getName()); if (commonGroupOptions == null) { @@ -206,7 +232,7 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman synopsis.append(" | "); } } - synopsis.append("} [--]"); + synopsis.append("}"); if (commonGroupOptions.size() > 0) { synopsis.appendWords(toSynopsisUsage(commonGroupOptions)); } @@ -214,9 +240,11 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman synopsis.append(" [cmd-options]"); } if (hasCommandSpecificArgs) { + synopsis.append(String.format("[%s]", parserConfig.getArgumentsSeparator())); synopsis.append(" "); } synopsis.newline(); + synopsis.flush(); Map cmdOptions = new TreeMap<>(); Map cmdArguments = new TreeMap<>(); Map subGroups = new TreeMap<>(); @@ -249,6 +277,7 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman grps.append(groupName + ": " + subGroups.get(groupName)).newline(); } } + synopsis.flush(); for (CommandMetadata command : commands) { @@ -264,7 +293,7 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman } if (hasCommandSpecificArgs) { cmdArguments.put(command.getName(), - (command.getArguments() != null ? toUsage(command.getArguments()) : "")); + getArgsUsage(command)); } } } @@ -288,6 +317,7 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman synopsis.newline().append("See").append("'" + global.getName()).append("help ") .appendWords(UsageHelper.toGroupNames(Arrays.asList(groups))) .appendOnOneLine(" ' for more information on a specific command.").newline(); + synopsis.flush(); } /** @@ -299,7 +329,8 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata global, Comman * Global meta-data * @param groups * Groups meta-data - * @throws IOException Thrown if there is a problem generating usage output + * @throws IOException + * Thrown if there is a problem generating usage output */ protected void outputDescription(UsagePrinter out, GlobalMetadata global, CommandGroupMetadata[] groups) throws IOException { diff --git a/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandUsageGenerator.java b/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandUsageGenerator.java index f068b06d8..0abf25c20 100644 --- a/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandUsageGenerator.java +++ b/airline-core/src/main/java/com/github/rvesse/airline/help/cli/CliCommandUsageGenerator.java @@ -88,8 +88,7 @@ public void usage(String programName, String[] groupNames, String commandNam // Options ArgumentsMetadata arguments = command.getArguments(); - if (options.size() > 0 || (arguments != null - || (command.getPositionalArguments() != null && command.getPositionalArguments().size() > 0))) { + if (options.size() > 0 || command.hasAnyArguments()) { outputOptionsAndArguments(out, command, options, command.getPositionalArguments(), arguments, parserConfig); } @@ -162,7 +161,7 @@ protected List outputSynopsis(UsagePrinter out, String progr boolean needsArgumentsSeparator = command.hasAnyArguments(); if (needsArgumentsSeparator) { - synopsis.append("[").append(parserConfig.getArgumentsSeparator()).append("]"); + synopsis.append(String.format("[%s]", parserConfig.getArgumentsSeparator())); } if (command.hasPositionalArguments()) { diff --git a/airline-core/src/main/java/com/github/rvesse/airline/help/common/AbstractUsageGenerator.java b/airline-core/src/main/java/com/github/rvesse/airline/help/common/AbstractUsageGenerator.java index 2afee8785..4d41e5b3e 100644 --- a/airline-core/src/main/java/com/github/rvesse/airline/help/common/AbstractUsageGenerator.java +++ b/airline-core/src/main/java/com/github/rvesse/airline/help/common/AbstractUsageGenerator.java @@ -187,7 +187,6 @@ protected String toUsage(ArgumentsMetadata arguments) { StringBuilder stringBuilder = new StringBuilder(); // NB Any additional arguments are either considered all required or - // optional whether that is actually the case or not. If users want fine // grained control over whether each argument is required or not they // need to use positional arguments instead diff --git a/airline-core/src/test/java/com/github/rvesse/airline/TestHelp.java b/airline-core/src/test/java/com/github/rvesse/airline/TestHelp.java index 619f1088a..3e7a1d0e3 100644 --- a/airline-core/src/test/java/com/github/rvesse/airline/TestHelp.java +++ b/airline-core/src/test/java/com/github/rvesse/airline/TestHelp.java @@ -335,7 +335,7 @@ public void testGit() throws IOException { " git remote - Manage set of tracked repositories\n" + "\n" + "SYNOPSIS\n" + - " git [ -v ] remote { add | show* } [--] [cmd-options] \n" + + " git [ -v ] remote { add | show* } [cmd-options] [--] \n" + "\n" + " Where command-specific options [cmd-options] are:\n" + " add: [ -t ]\n" + diff --git a/airline-core/src/test/java/com/github/rvesse/airline/TestSubGroups.java b/airline-core/src/test/java/com/github/rvesse/airline/TestSubGroups.java index a5455fbef..cf1ea7f97 100644 --- a/airline-core/src/test/java/com/github/rvesse/airline/TestSubGroups.java +++ b/airline-core/src/test/java/com/github/rvesse/airline/TestSubGroups.java @@ -209,7 +209,7 @@ public void sub_groups_help_03() throws IOException { " test foo -", "", "SYNOPSIS", - " test foo { bar } [--]", + " test foo { bar }", "", " Where command groups contain the following sub-groups and commands:", " bar: help*", diff --git a/airline-core/src/test/java/com/github/rvesse/airline/args/positional/TestPositionalArgs.java b/airline-core/src/test/java/com/github/rvesse/airline/args/positional/TestPositionalArgs.java index 6a567a216..479c07e4e 100644 --- a/airline-core/src/test/java/com/github/rvesse/airline/args/positional/TestPositionalArgs.java +++ b/airline-core/src/test/java/com/github/rvesse/airline/args/positional/TestPositionalArgs.java @@ -173,6 +173,6 @@ public void positional_args_help_01() throws IOException { String actual = output.toString(StandardCharsets.UTF_8.name()); - assertTrue(actual.contains("ArgsPositional [ -- ] [ ] [ ... ]")); + assertTrue(actual.contains("ArgsPositional [--] [ ] [ ... ]")); } }