Skip to content

Commit

Permalink
Added support for required arguments in help. Resolves usnistgov/osca…
Browse files Browse the repository at this point in the history
…l-cli#125.

Fixed suppression of messages for invalid commands. Resolves usnistgov/oscal-cli#79.
  • Loading branch information
david-waltermire committed Jul 4, 2023
1 parent afb5173 commit 2e60645
Showing 1 changed file with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
Expand Down Expand Up @@ -201,8 +202,9 @@ private static void handleNoColor() {
}

public static void handleQuiet() {
@SuppressWarnings("resource") LoggerContext ctx = (LoggerContext) LogManager.getContext(false); // NOPMD not
// closable here
@SuppressWarnings("resource")
LoggerContext ctx = (LoggerContext) LogManager.getContext(false); // NOPMD not
// closable here
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
Level oldLevel = loggerConfig.getLevel();
Expand All @@ -213,7 +215,8 @@ public static void handleQuiet() {
}

protected void showVersion() {
@SuppressWarnings("resource") PrintStream out = AnsiConsole.out(); // NOPMD - not owner
@SuppressWarnings("resource")
PrintStream out = AnsiConsole.out(); // NOPMD - not owner
getVersionInfos().stream().forEach((info) -> {
out.println(ansi()
.bold().a(info.getName()).boldOff()
Expand Down Expand Up @@ -399,7 +402,10 @@ protected ExitStatus invokeCommand(@NonNull CommandLine cmdLine) {
public ExitStatus handleInvalidCommand(
@NonNull String message) {
showHelp();
return ExitCode.INVALID_COMMAND.exitMessage(message);

ExitStatus retval = ExitCode.INVALID_COMMAND.exitMessage(message);
retval.generateMessage(false);
return retval;
}

/**
Expand Down Expand Up @@ -482,6 +488,7 @@ protected String buildHelpCliSyntax() {
.collect(Collectors.joining(" ", " ", "")));
}

// output calling commands
Command targetCommand = getTargetCommand();
if (targetCommand == null) {
builder.append(" <command>");
Expand All @@ -502,8 +509,24 @@ protected String buildHelpCliSyntax() {
}
}

// output required options
getOptionsList().stream()
.filter(option -> option.isRequired())
.forEach(option -> {
builder
.append(' ')
.append(OptionUtils.toArgument(option));
if (option.hasArg()) {
builder
.append('=')
.append(option.getArgName());
}
});

// output non-required option placeholder
builder.append(" [<options>]");

// output extra arguments
if (targetCommand != null) {
// handle extra arguments
for (ExtraArgument argument : targetCommand.getExtraArguments()) {
Expand Down Expand Up @@ -538,9 +561,10 @@ public void showHelp() {
AnsiPrintStream out = AnsiConsole.out();
int terminalWidth = Math.max(out.getTerminalWidth(), 40);

@SuppressWarnings("resource") PrintWriter writer = new PrintWriter(out, true, StandardCharsets.UTF_8); // NOPMD -
// not
// owned
@SuppressWarnings("resource")
PrintWriter writer = new PrintWriter(out, true, StandardCharsets.UTF_8); // NOPMD -
// not
// owned
formatter.printHelp(
writer,
terminalWidth,
Expand Down

0 comments on commit 2e60645

Please sign in to comment.