Skip to content

Commit

Permalink
Adjust tests to cope with positional args (#91)
Browse files Browse the repository at this point in the history
Starts working on CLI command group usage generator
  • Loading branch information
rvesse committed Apr 16, 2019
1 parent 7aeeb4c commit 5c64108
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -86,7 +91,8 @@ protected void usage(GlobalMetadata<T> 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<T> global, CommandGroupMetadata[] groups)
throws IOException {
Expand Down Expand Up @@ -119,6 +125,20 @@ protected void outputOptions(UsagePrinter out, GlobalMetadata<T> 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
*
Expand All @@ -128,7 +148,8 @@ protected void outputOptions(UsagePrinter out, GlobalMetadata<T> 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<T> global, CommandGroupMetadata[] groups)
throws IOException {
Expand All @@ -137,6 +158,8 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata<T> global, Comman

CommandGroupMetadata group = groups[groups.length - 1];
List<CommandMetadata> commands = sortCommands(group.getCommands());
ParserMetadata<T> parserConfig = global != null ? global.getParserConfiguration()
: ParserBuilder.<T> defaultConfiguration();

// Populate group info via an extra for loop through commands
boolean hasDefaultCommand = group.getDefaultCommand() != null;
Expand All @@ -156,17 +179,20 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata<T> 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) {
Expand Down Expand Up @@ -206,17 +232,19 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata<T> global, Comman
synopsis.append(" | ");
}
}
synopsis.append("} [--]");
synopsis.append("}");
if (commonGroupOptions.size() > 0) {
synopsis.appendWords(toSynopsisUsage(commonGroupOptions));
}
if (hasCommandSpecificOptions) {
synopsis.append(" [cmd-options]");
}
if (hasCommandSpecificArgs) {
synopsis.append(String.format("[%s]", parserConfig.getArgumentsSeparator()));
synopsis.append(" <cmd-args>");
}
synopsis.newline();
synopsis.flush();
Map<String, String> cmdOptions = new TreeMap<>();
Map<String, String> cmdArguments = new TreeMap<>();
Map<String, String> subGroups = new TreeMap<>();
Expand Down Expand Up @@ -249,6 +277,7 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata<T> global, Comman
grps.append(groupName + ": " + subGroups.get(groupName)).newline();
}
}
synopsis.flush();

for (CommandMetadata command : commands) {

Expand All @@ -264,7 +293,7 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata<T> global, Comman
}
if (hasCommandSpecificArgs) {
cmdArguments.put(command.getName(),
(command.getArguments() != null ? toUsage(command.getArguments()) : ""));
getArgsUsage(command));
}
}
}
Expand All @@ -288,6 +317,7 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata<T> global, Comman
synopsis.newline().append("See").append("'" + global.getName()).append("help ")
.appendWords(UsageHelper.toGroupNames(Arrays.asList(groups)))
.appendOnOneLine(" <command>' for more information on a specific command.").newline();
synopsis.flush();
}

/**
Expand All @@ -299,7 +329,8 @@ protected void outputSynopsis(UsagePrinter out, GlobalMetadata<T> 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<T> global, CommandGroupMetadata[] groups)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public <T> 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);
}

Expand Down Expand Up @@ -162,7 +161,7 @@ protected <T> List<OptionMetadata> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] <cmd-args>\n" +
" git [ -v ] remote { add | show* } [cmd-options] [--] <cmd-args>\n" +
"\n" +
" Where command-specific options [cmd-options] are:\n" +
" add: [ -t <branch> ]\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void sub_groups_help_03() throws IOException {
" test foo -",
"",
"SYNOPSIS",
" test foo { bar <sub-command> } [--]",
" test foo { bar <sub-command> }",
"",
" Where command groups contain the following sub-groups and commands:",
" bar: help*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,6 @@ public void positional_args_help_01() throws IOException {

String actual = output.toString(StandardCharsets.UTF_8.name());

assertTrue(actual.contains("ArgsPositional [ -- ] <File> [ <Mode> ] [ <ExtraArg>... ]"));
assertTrue(actual.contains("ArgsPositional [--] <File> [ <Mode> ] [ <ExtraArg>... ]"));
}
}

0 comments on commit 5c64108

Please sign in to comment.