Skip to content

Commit

Permalink
More work on positional arguments help (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
rvesse committed Apr 29, 2019
1 parent 8e77fa5 commit 70cb17d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,24 @@ private void generateCommandCompletionFunction(Writer writer, GlobalMetadata<T>
writeWordListVariable(writer, 2, "FLAG_OPTS", flagOpts.iterator());
writeWordListVariable(writer, 2, "ARG_OPTS", argOpts.iterator());
writer.append(NEWLINE);

// Check whether we've seen the arguments separator
writer.append(" $( containsElement \"").append(global.getParserConfiguration().getArgumentsSeparator()).append("\" ${COMP_WORDS[@]} )");
writer.append(" SAW_ARGS_SEPARATOR=$?");
writer.append(" if [[ ${SAW_ARGS_SEPARATOR} -eq 0 ]]; then");

// TODO Do completion specifically for arguments

writer.append(" fi");

// Check whether we are completing a value for an argument flag
// Check whether we are completing a value for an option
if (argOpts.size() > 0) {
writer.append(" $( containsElement ${PREV_WORD} ${ARG_OPTS[@]} )").append(NEWLINE);
writer.append(" SAW_ARG=$?").append(NEWLINE);
writer.append(" SAW_OPTION=$?").append(NEWLINE);

// If we previously saw an argument then we are completing that
// argument
writer.append(" if [[ ${SAW_ARG} -eq 0 ]]; then").append(NEWLINE);
writer.append(" if [[ ${SAW_OPTION} -eq 0 ]]; then").append(NEWLINE);
writer.append(" ARG_VALUES=").append(NEWLINE);
writer.append(" ARG_GENERATED_VALUES=").append(NEWLINE);
writer.append(" case ${PREV_WORD} in").append(NEWLINE);
Expand Down Expand Up @@ -439,7 +448,7 @@ private void generateCommandCompletionFunction(Writer writer, GlobalMetadata<T>
writer.append(" fi").append(DOUBLE_NEWLINE);
}

// If we previously saw a flag we could see another option or an
// If we previously saw a flag option we could see another option or an
// argument if supported
BashCompletion completion = null;
if (command.getArguments() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public <T> void usage(String programName, String[] groupNames, String commandNam
helper.outputHelpSection(printer, section);
}

List<OptionMetadata> options = outputSynopsis(printer, programName, groupNames, commandName, command);
List<OptionMetadata> options = outputSynopsis(printer, programName, groupNames, commandName, command, parserConfig);

if (options.size() > 0 || command.getArguments() != null) {
if (options.size() > 0 || command.hasAnyArguments()) {
outputOptions(printer, command, options, parserConfig);
}

Expand Down Expand Up @@ -127,7 +127,7 @@ protected <T> void outputOptions(TroffPrinter printer, CommandMetadata command,
ParserMetadata<T> parserConfig) throws IOException {
// Options
// Can end the list if there are no arguments
int optionsOutput = helper.outputOptions(printer, options, command.getArguments() == null);
int optionsOutput = helper.outputOptions(printer, options, !command.hasAnyArguments());

// Arguments
// Must start the list if there are no visible options
Expand All @@ -152,8 +152,8 @@ protected <T> void outputOptions(TroffPrinter printer, CommandMetadata command,
* @throws IOException
* Thrown if there is a problem generating usage output
*/
protected List<OptionMetadata> outputSynopsis(TroffPrinter printer, String programName, String[] groupNames,
String commandName, CommandMetadata command) throws IOException {
protected <T> List<OptionMetadata> outputSynopsis(TroffPrinter printer, String programName, String[] groupNames,
String commandName, CommandMetadata command, ParserMetadata<T> parserConfig) throws IOException {
printer.nextSection("SYNOPSIS");

List<OptionMetadata> options = new ArrayList<>();
Expand Down Expand Up @@ -200,9 +200,9 @@ protected List<OptionMetadata> outputSynopsis(TroffPrinter printer, String progr
options.addAll(aOptions);

// Command arguments (optional)
if (command.getArguments() != null) {
if (command.hasAnyArguments()) {
printer.print(" [ ");
printer.printBold("--");
printer.printBold(parserConfig.getArgumentsSeparator());
printer.print(" ] ");
this.helper.outputArgumentsSynopsis(printer, command.getPositionalArguments(), command.getArguments());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public int outputOptions(TroffPrinter printer, List<OptionMetadata> options, boo
return output;
}

public <T> void outputArguments(TroffPrinter printer, List<PositionalArgumentMetadata> posArgs, ArgumentsMetadata arguments, boolean startList,
ParserMetadata<T> parserConfig) throws IOException {
public <T> void outputArguments(TroffPrinter printer, List<PositionalArgumentMetadata> posArgs,
ArgumentsMetadata arguments, boolean startList, ParserMetadata<T> parserConfig) throws IOException {
boolean needsArgsSeparator = (posArgs != null && posArgs.size() > 0) || arguments != null;

if (needsArgsSeparator) {
// Arguments separator option
if (startList) {
Expand All @@ -113,30 +113,30 @@ public <T> void outputArguments(TroffPrinter printer, List<PositionalArgumentMet
} else {
return;
}

if (posArgs != null && posArgs.size() > 0) {
for (PositionalArgumentMetadata posArg : posArgs) {
// Argument name
printer.nextTitledListItem();
printer.printItalic(posArg.getTitle());

// Description
printer.startPlainList();
printer.println(posArg.getDescription());

// Restrictions
List<HelpHint> hints = sortArgumentsRestrictions(arguments.getRestrictions());
for (HelpHint hint : hints) {
// Safe to cast back to ArgumentsRestriction as must have come
// Safe to cast back to ArgumentsRestriction as must have
// come
// from an ArgumentsRestriction to start with
outputArgumentsRestriction(printer, arguments, (ArgumentsRestriction) hint, hint);
}
printer.endList();
}
}

if (arguments != null) {

if (arguments != null) {

// Arguments name(s)
printer.nextTitledListItem();
Expand All @@ -155,9 +155,12 @@ public <T> void outputArguments(TroffPrinter printer, List<PositionalArgumentMet
}
printer.endList();
}

// End list of options and arguments
printer.endList();

// End list of options and arguments unless there were none in which
// case we'd never have started a list here
if (needsArgsSeparator) {
printer.endList();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public <T> void usage(String programName, String[] groupNames, String commandNam
}

// Synopsis
List<OptionMetadata> options = outputSynopsis(out, programName, groupNames, commandName, command);
List<OptionMetadata> options = outputSynopsis(out, programName, groupNames, commandName, command, parserConfig);

// Options
ArgumentsMetadata arguments = command.getArguments();
Expand Down Expand Up @@ -142,8 +142,8 @@ protected <T> void outputOptionsAndArguments(UsagePrinter out, CommandMetadata c
* @throws IOException
* Thrown if there is a problem generating usage output
*/
protected List<OptionMetadata> outputSynopsis(UsagePrinter out, String programName, String[] groupNames,
String commandName, CommandMetadata command) throws IOException {
protected <T> List<OptionMetadata> outputSynopsis(UsagePrinter out, String programName, String[] groupNames,
String commandName, CommandMetadata command, ParserMetadata<T> parserConfig) throws IOException {
out.append("# SYNOPSIS").newline().newline();

List<OptionMetadata> options = new ArrayList<>();
Expand All @@ -164,8 +164,8 @@ protected List<OptionMetadata> outputSynopsis(UsagePrinter out, String programNa
options.addAll(command.getCommandOptions());

// command arguments (optional)
if (command.getArguments() != null) {
out.append("[ `--` ]");
if (command.hasAnyArguments()) {
out.append(String.format("[ `%s` ]", parserConfig.getArgumentsSeparator()));
helper.outputArgumentsSynopsis(out, command.getArguments());
}
out.newline();
Expand Down

0 comments on commit 70cb17d

Please sign in to comment.