Skip to content

Commit

Permalink
fix: Improve synopsis order (fixes #133)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Sep 27, 2024
1 parent 4ff86f4 commit 78b530c
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.fortify.cli.common.variable.FcliVariableHelper;

import picocli.CommandLine;
import picocli.CommandLine.Help.Ansi.Text;
import picocli.CommandLine.Model.ArgGroupSpec;
import picocli.CommandLine.Model.CommandSpec;

public final class DefaultFortifyCLIRunner implements IFortifyCLIRunner {
// TODO See https://github.com/remkop/picocli/issues/2066
Expand All @@ -36,6 +39,7 @@ private CommandLine createCommandLine() {
// See comments in I18nParameterExceptionHandler for more detail.
//cl.setParameterExceptionHandler(new I18nParameterExceptionHandler(cl.getParameterExceptionHandler()));
cl.setDefaultValueProvider(FortifyCLIDefaultValueProvider.getInstance());
cl.setHelpFactory((commandSpec, colorScheme)->new FcliHelp(commandSpec, colorScheme));
return cl;
}

Expand Down Expand Up @@ -70,4 +74,34 @@ public int run(List<String> args) {
public void close() {
GenericUnirestFactory.shutdown();
}

private static final class FcliHelp extends CommandLine.Help {
public FcliHelp(CommandSpec commandSpec, ColorScheme colorScheme) {
super(commandSpec, colorScheme);
}

public FcliHelp(Object command, Ansi ansi) {
super(command, ansi);
}

public FcliHelp(Object command) {
super(command);
}

protected String makeSynopsisFromParts(int synopsisHeadingLength, Text optionText, Text groupsText, Text endOfOptionsText, Text positionalParamText, Text commandText) {
boolean positionalsOnly = true;
for (ArgGroupSpec group : commandSpec().argGroups()) {
if (group.validate()) { // non-validating groups are not shown in the synopsis
positionalsOnly &= group.allOptionsNested().isEmpty();
}
}
Text text;
if (positionalsOnly) { // show end-of-options delimiter before the (all-positional params) groups
text = positionalParamText.concat(optionText).concat(endOfOptionsText).concat(groupsText).concat(commandText);
} else {
text = positionalParamText.concat(optionText).concat(groupsText).concat(endOfOptionsText).concat(commandText);
}
return insertSynopsisCommandName(synopsisHeadingLength, text);
}
}
}

0 comments on commit 78b530c

Please sign in to comment.