From d3b6f4c260580c6db7f0edcc175dc354db72eae1 Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:10:41 +0100 Subject: [PATCH] fix: Improve parsing of boolean action parameters --- .../cli/common/action/runner/ActionParameterHelper.java | 1 + .../fortify/cli/common/cli/util/SimpleOptionsParser.java | 9 +++++++-- .../com/fortify/cli/fod/actions/zip/setup-release.yaml | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/runner/ActionParameterHelper.java b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/runner/ActionParameterHelper.java index e9ea65a9d6..818e468193 100644 --- a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/runner/ActionParameterHelper.java +++ b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/action/runner/ActionParameterHelper.java @@ -39,6 +39,7 @@ private static final void addOptionDescriptor(List result, Ac .name(getOptionName(parameter.getName())) .aliases(getOptionAliases(parameter.getCliAliasesArray())) .description(parameter.getDescription()) + .bool(parameter.getType()!=null && parameter.getType().equals("boolean")) .build()); } diff --git a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/util/SimpleOptionsParser.java b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/util/SimpleOptionsParser.java index c0ddbaab2d..67b34d8480 100644 --- a/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/util/SimpleOptionsParser.java +++ b/fcli-core/fcli-common/src/main/java/com/fortify/cli/common/cli/util/SimpleOptionsParser.java @@ -45,6 +45,7 @@ public static interface IOptionDescriptor { String getName(); List getAliases(); String getDescription(); + boolean isBool(); default String getOptionNamesAndAliasesString(String delimiter) { var name = getName(); @@ -59,6 +60,7 @@ public static class OptionDescriptor implements IOptionDescriptor { private final String name; private final List aliases; private final String description; + private final boolean bool; } @Data @@ -107,13 +109,16 @@ private Map parseArgs(List validationErrors, String[] ar var argWithPossibleValue = argsDeque.pop(); var argElts = argWithPossibleValue.split("=", 2); var arg = argElts[0]; - if ( !optionsByNameAndAliases.containsKey(arg) ) { + var optionDescriptor = optionsByNameAndAliases.get(arg); + if ( optionDescriptor==null ) { validationErrors.add("Unknown command line option: "+arg); } else if ( argElts.length==2 ) { result.put(arg, argElts[1]); } else { var nextArg = argsDeque.peek(); - var value = optionsByNameAndAliases.containsKey(nextArg) ? null : argsDeque.pop(); + var value = nextArg==null || optionsByNameAndAliases.containsKey(nextArg) + ? (optionDescriptor.isBool() ? "true" : null) + : argsDeque.pop(); result.put(arg, value); } } diff --git a/fcli-core/fcli-fod/src/main/resources/com/fortify/cli/fod/actions/zip/setup-release.yaml b/fcli-core/fcli-fod/src/main/resources/com/fortify/cli/fod/actions/zip/setup-release.yaml index af52e9c260..c39342f76d 100644 --- a/fcli-core/fcli-fod/src/main/resources/com/fortify/cli/fod/actions/zip/setup-release.yaml +++ b/fcli-core/fcli-fod/src/main/resources/com/fortify/cli/fod/actions/zip/setup-release.yaml @@ -62,11 +62,13 @@ parameters: - group: sast_setup_opts required: false name: use-aviator - description: "See `fcli fod sast-scan setup`" + description: "See `fcli fod sast-scan setup`" + type: boolean - group: sast_setup_opts required: false name: oss description: "See `fcli fod sast-scan setup`" + type: boolean - name: attributes required: false cliAliases: attrs