Skip to content

Commit

Permalink
[chip-tool] Add std::vector<CustomArgument *> arguments support
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jun 15, 2022
1 parent dea9d93 commit ada97cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,23 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
return true;
}

case ArgumentType::VectorCustom: {
auto vectorArgument = static_cast<std::vector<CustomArgument *> *>(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())
{
Expand Down Expand Up @@ -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<CustomArgument *> * value, const char * desc)
{
Argument arg;
arg.type = ArgumentType::VectorCustom;
arg.name = name;
arg.value = static_cast<void *>(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;
Expand Down Expand Up @@ -855,5 +884,14 @@ void Command::ResetArguments()
optionalArgument->Value().clear();
}
}
else if (type == ArgumentType::VectorCustom && flags != Argument::kOptional)
{
auto vectorArgument = static_cast<std::vector<CustomArgument *> *>(arg.value);
for (auto & customArgument : *vectorArgument)
{
delete customArgument;
}
vectorArgument->clear();
}
}
}
2 changes: 2 additions & 0 deletions examples/chip-tool/commands/common/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ enum ArgumentType
VectorBool,
Vector16,
Vector32,
VectorCustom,
};

struct Argument
Expand Down Expand Up @@ -179,6 +180,7 @@ class Command

size_t AddArgument(const char * name, int64_t min, uint64_t max, std::vector<uint16_t> * value, const char * desc = "");
size_t AddArgument(const char * name, int64_t min, uint64_t max, std::vector<uint32_t> * value, const char * desc = "");
size_t AddArgument(const char * name, std::vector<CustomArgument *> * value, const char * desc = "");
size_t AddArgument(const char * name, int64_t min, uint64_t max, chip::Optional<std::vector<bool>> * value,
const char * desc = "");
size_t AddArgument(const char * name, int64_t min, uint64_t max, chip::Optional<std::vector<uint32_t>> * value,
Expand Down

0 comments on commit ada97cd

Please sign in to comment.