From a5539a7de4e2f596d475a9809e7da1b54d65df6d Mon Sep 17 00:00:00 2001 From: mertcaliskan Date: Sat, 5 Jan 2019 23:52:28 +0300 Subject: [PATCH] PAYARA-3352-If-multiple-invalid-options-are-specified-for-asadmin-only-first-is-reported --- .../admin/cli/LocalStrings.properties | 2 ++ .../com/sun/enterprise/admin/cli/Parser.java | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/LocalStrings.properties b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/LocalStrings.properties index a6745f88f1b..977d1165b2d 100644 --- a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/LocalStrings.properties +++ b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/LocalStrings.properties @@ -37,6 +37,7 @@ # only if the new code is made subject to such option by the copyright # holder. # +# Portions Copyright [2019] Payara Foundation and/or affiliates CommandSuccessful=Command {0} executed successfully. CommandSuccessfulWithWarnings=Command {0} completed with warnings. @@ -202,6 +203,7 @@ DeprecatedSyntax=Deprecated syntax, instead use: parser.noValueAllowed=Option may not have value: {0} parser.illegalNo="--no" illegal with non-boolean option: {0} parser.invalidOption=Invalid option: {0} +parser.invalidOptions=Invalid options: {0} parser.nonbooleanNotAllowed=Non-boolean option: {0}, not allowed in argument: {1} parser.missingValue=Missing value for option: {0} parser.invalidFileEx=Invalid file for option: --{0}: {1} diff --git a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Parser.java b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Parser.java index 829b75427ba..22a0e4fe4ab 100644 --- a/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Parser.java +++ b/nucleus/admin/cli/src/main/java/com/sun/enterprise/admin/cli/Parser.java @@ -37,12 +37,14 @@ * only if the new code is made subject to such option by the copyright * holder. */ -// Portions Copyright [2018] Payara Foundation and/or affiliates +// Portions Copyright [2018-2019] Payara Foundation and/or affiliates package com.sun.enterprise.admin.cli; import java.io.*; import java.util.*; + +import com.sun.enterprise.util.StringUtils; import org.glassfish.api.admin.*; import org.glassfish.api.admin.CommandModel.ParamModel; import com.sun.enterprise.admin.util.CommandModelData.ParamModelData; @@ -56,7 +58,10 @@ public class Parser { /** MultiMap of options and values from command-line */ private ParameterMap optionsMap = new ParameterMap(); - + + /** Set of invalid options' names from command-line */ + private Set invalidOptions = new HashSet<>(); + /** Array of operands from command-line */ private List operands = new ArrayList(); @@ -176,7 +181,7 @@ private void parseCommandLine(final String[] argv, final int start) opt = lookupShortOption(arg.charAt(i)); if (opt == null) { if (!ignoreUnknown) { - throw new CommandValidationException(strings.get("parser.invalidOption", Character.toString(arg.charAt(i)))); + invalidOptions.add(arg); } // unknown option, skip all the rest operands.add(arg); @@ -202,7 +207,7 @@ private void parseCommandLine(final String[] argv, final int start) // is it a known option? if (opt == null) { if (!ignoreUnknown){ - throw new CommandValidationException(strings.get("parser.invalidOption", arg)); + invalidOptions.add(arg); } // unknown option, skip it operands.add(arg); @@ -240,7 +245,16 @@ private void parseCommandLine(final String[] argv, final int start) } } } - setOption(opt, value); + if (invalidOptions.isEmpty()) { + setOption(opt, value); + } + } + + if (invalidOptions.size() == 1) { + throw new CommandValidationException(strings.get("parser.invalidOption", invalidOptions.iterator().next())); + } + else if (invalidOptions.size() > 1) { + throw new CommandValidationException(strings.get("parser.invalidOptions", StringUtils.cat(" ", invalidOptions.toArray(new String[0])))); } }