From 089e47aa370e7295be1080b33ee3e9e6ed05c414 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 2 Feb 2023 10:32:30 -0500 Subject: [PATCH] Improve error reporting in chip-tool when the wrong quotes are used. In interactive mode, arguments are delimited by single quotes. If our argument init fails, and we have arguments that include mismatched double quotes, there's a good chance the wrong quotes were used in the command, and we should log that. --- .../chip-tool/commands/common/Commands.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples/chip-tool/commands/common/Commands.cpp b/examples/chip-tool/commands/common/Commands.cpp index fb675ee96acb4d..8b25de818d459c 100644 --- a/examples/chip-tool/commands/common/Commands.cpp +++ b/examples/chip-tool/commands/common/Commands.cpp @@ -217,6 +217,34 @@ CHIP_ERROR Commands::RunCommand(int argc, char ** argv, bool interactive) int argumentsPosition = isGlobalCommand ? 4 : 3; if (!command->InitArguments(argc - argumentsPosition, &argv[argumentsPosition])) { + if (interactive) + { + // Check for arguments with a starting '"' but no ending '"': those + // would indicate that people are using double-quoting, not single + // quoting, on arguments with spaces. + for (int curArg = argumentsPosition; curArg < argc - argumentsPosition; ++curArg) + { + char * arg = argv[curArg]; + if (!arg) + { + continue; + } + + auto len = strlen(arg); + if (len == 0) + { + continue; + } + + if (arg[0] == '"' && arg[len - 1] != '"') + { + ChipLogError(chipTool, + "Mismatched '\"' detected in argument: '%s'. Use single quotes to delimit arguments with spaces " + "in them: 'x y', not \"x y\".", + arg); + } + } + } ShowCommand(argv[0], argv[1], command); return CHIP_ERROR_INVALID_ARGUMENT; }