Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error reporting in chip-tool when the wrong quotes are used. #24821

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,35 @@ std::vector<std::string> GetArgumentsFromJson(Command * command, Json::Value & v
return args;
};

// 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.
static void DetectAndLogMismatchedDoubleQuotes(int argc, char ** argv)
{
for (int curArg = 0; curArg < argc; ++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);
}
}
}

} // namespace

void Commands::Register(const char * clusterName, commands_list commandsList)
Expand Down Expand Up @@ -217,6 +246,10 @@ CHIP_ERROR Commands::RunCommand(int argc, char ** argv, bool interactive)
int argumentsPosition = isGlobalCommand ? 4 : 3;
if (!command->InitArguments(argc - argumentsPosition, &argv[argumentsPosition]))
{
if (interactive)
{
DetectAndLogMismatchedDoubleQuotes(argc - argumentsPosition, &argv[argumentsPosition]);
}
ShowCommand(argv[0], argv[1], command);
return CHIP_ERROR_INVALID_ARGUMENT;
}
Expand Down