From 4ddd6323f9f0f7ee4558610544bdb9c4e6993b8f Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Thu, 14 Mar 2024 10:20:43 -0400 Subject: [PATCH] Fixed issue causing CLI errors when using --help or --version, while not providing a required command parameter. Resolves #293. (#327) --- .../cli/processor/CLIProcessor.java | 64 ++++++++++++------- .../nist/secauto/metaschema/cli/CLITest.java | 4 +- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java index 56dfd84b7..44a921fdd 100644 --- a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java +++ b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java @@ -344,36 +344,52 @@ protected Options toOptions() { public ExitStatus processCommand() { CommandLineParser parser = new DefaultParser(); CommandLine cmdLine; - try { - cmdLine = parser.parse(toOptions(), getExtraArgs().toArray(new String[0])); - } catch (ParseException ex) { - String msg = ex.getMessage(); - assert msg != null; - return handleInvalidCommand(msg); - } - if (cmdLine.hasOption(NO_COLOR_OPTION)) { - handleNoColor(); - } - - if (cmdLine.hasOption(QUIET_OPTION)) { - handleQuiet(); - } + // this uses a two phase approach where: + // phase 1: checks if help or version are used + // phase 2: executes the command + // phase 1 ExitStatus retval = null; - if (cmdLine.hasOption(VERSION_OPTION)) { - showVersion(); - retval = ExitCode.OK.exit(); - } else if (cmdLine.hasOption(HELP_OPTION)) { - showHelp(); - retval = ExitCode.OK.exit(); - // } else { - // retval = handleInvalidCommand(commandResult, options, - // "Invalid command arguments: " + - // cmdLine.getArgList().stream().collect(Collectors.joining(" "))); + { + try { + Options phase1Options = new Options(); + phase1Options.addOption(HELP_OPTION); + phase1Options.addOption(VERSION_OPTION); + + cmdLine = parser.parse(phase1Options, getExtraArgs().toArray(new String[0]), true); + } catch (ParseException ex) { + String msg = ex.getMessage(); + assert msg != null; + return handleInvalidCommand(msg); + } + + if (cmdLine.hasOption(VERSION_OPTION)) { + showVersion(); + retval = ExitCode.OK.exit(); + } else if (cmdLine.hasOption(HELP_OPTION)) { + showHelp(); + retval = ExitCode.OK.exit(); + } } if (retval == null) { + // phase 2 + try { + cmdLine = parser.parse(toOptions(), getExtraArgs().toArray(new String[0])); + } catch (ParseException ex) { + String msg = ex.getMessage(); + assert msg != null; + return handleInvalidCommand(msg); + } + + if (cmdLine.hasOption(NO_COLOR_OPTION)) { + handleNoColor(); + } + + if (cmdLine.hasOption(QUIET_OPTION)) { + handleQuiet(); + } retval = invokeCommand(cmdLine); } diff --git a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java index d67c991a5..97c44158f 100644 --- a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java +++ b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java @@ -70,10 +70,10 @@ private static Stream providesValues() { { add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, NO_EXCEPTION_CLASS)); add(Arguments.of(new String[] { "-h" }, ExitCode.OK, NO_EXCEPTION_CLASS)); - add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.INVALID_COMMAND, + add(Arguments.of(new String[] { "generate-schema", "--help" }, ExitCode.OK, NO_EXCEPTION_CLASS)); add(Arguments.of(new String[] { "validate", "--help" }, ExitCode.OK, NO_EXCEPTION_CLASS)); - add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.INVALID_COMMAND, + add(Arguments.of(new String[] { "validate-content", "--help" }, ExitCode.OK, NO_EXCEPTION_CLASS)); add(Arguments.of( new String[] { "validate",