From 37004b4b321ed5a19496ee34c253f5839da8fcf4 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 14 Jun 2022 09:35:53 +0200 Subject: [PATCH] [chip-tool] Add std::vector arguments support --- .../chip-tool/commands/common/Command.cpp | 38 +++++++++++++++++++ examples/chip-tool/commands/common/Command.h | 2 + 2 files changed, 40 insertions(+) diff --git a/examples/chip-tool/commands/common/Command.cpp b/examples/chip-tool/commands/common/Command.cpp index f5828f2b138326..1931d88e39752f 100644 --- a/examples/chip-tool/commands/common/Command.cpp +++ b/examples/chip-tool/commands/common/Command.cpp @@ -305,6 +305,23 @@ bool Command::InitArgument(size_t argIndex, char * argValue) return true; } + case ArgumentType::VectorCustom: { + auto vectorArgument = static_cast *>(arg.value); + + std::stringstream ss(argValue); + while (ss.good()) + { + std::string valueAsString; + getline(ss, valueAsString, ','); + + CustomArgument * customArgument = new CustomArgument(); + vectorArgument->push_back(customArgument); + VerifyOrReturnError(CHIP_NO_ERROR == vectorArgument->back()->Parse(arg.name, valueAsString.c_str()), false); + } + + return true; + } + case ArgumentType::Attribute: { if (arg.isOptional() || arg.isNullable()) { @@ -695,6 +712,18 @@ size_t Command::AddArgument(const char * name, CustomArgument * value, const cha return AddArgumentToList(std::move(arg)); } +size_t Command::AddArgument(const char * name, std::vector * value, const char * desc) +{ + Argument arg; + arg.type = ArgumentType::VectorCustom; + arg.name = name; + arg.value = static_cast(value); + arg.flags = 0; + arg.desc = desc; + + return AddArgumentToList(std::move(arg)); +} + size_t Command::AddArgument(const char * name, float min, float max, float * out, const char * desc, uint8_t flags) { Argument arg; @@ -855,5 +884,14 @@ void Command::ResetArguments() optionalArgument->Value().clear(); } } + else if (type == ArgumentType::VectorCustom && flags != Argument::kOptional) + { + auto vectorArgument = static_cast *>(arg.value); + for (auto & customArgument : *vectorArgument) + { + delete customArgument; + } + vectorArgument->clear(); + } } } diff --git a/examples/chip-tool/commands/common/Command.h b/examples/chip-tool/commands/common/Command.h index b9a1c2bc1cb972..2a7411cf506854 100644 --- a/examples/chip-tool/commands/common/Command.h +++ b/examples/chip-tool/commands/common/Command.h @@ -73,6 +73,7 @@ enum ArgumentType VectorBool, Vector16, Vector32, + VectorCustom, }; struct Argument @@ -179,6 +180,7 @@ class Command size_t AddArgument(const char * name, int64_t min, uint64_t max, std::vector * value, const char * desc = ""); size_t AddArgument(const char * name, int64_t min, uint64_t max, std::vector * value, const char * desc = ""); + size_t AddArgument(const char * name, std::vector * value, const char * desc = ""); size_t AddArgument(const char * name, int64_t min, uint64_t max, chip::Optional> * value, const char * desc = ""); size_t AddArgument(const char * name, int64_t min, uint64_t max, chip::Optional> * value,