Skip to content

Commit ff8fe37

Browse files
authored
[chip-tool] InitArguments is off by one for global commands (#24299)
1 parent c982db7 commit ff8fe37

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

examples/chip-tool/commands/common/Command.cpp

+23-38
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,6 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
331331
return true;
332332
}
333333

334-
case ArgumentType::Attribute: {
335-
if (arg.isOptional() || arg.isNullable())
336-
{
337-
isValidArgument = false;
338-
}
339-
else
340-
{
341-
char * value = reinterpret_cast<char *>(arg.value);
342-
isValidArgument = (strcmp(argValue, value) == 0);
343-
}
344-
break;
345-
}
346-
347334
case ArgumentType::String: {
348335
isValidArgument = HandleNullableOptional<char *>(arg, argValue, [&](auto * value) {
349336
*value = argValue;
@@ -603,16 +590,14 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
603590
return isValidArgument;
604591
}
605592

606-
size_t Command::AddArgument(const char * name, const char * value, const char * desc, uint8_t flags)
593+
void Command::AddArgument(const char * name, const char * value, const char * desc)
607594
{
608-
Argument arg;
609-
arg.type = ArgumentType::Attribute;
595+
ReadOnlyGlobalCommandArgument arg;
610596
arg.name = name;
611-
arg.value = const_cast<void *>(reinterpret_cast<const void *>(value));
612-
arg.flags = flags;
597+
arg.value = value;
613598
arg.desc = desc;
614599

615-
return AddArgumentToList(std::move(arg));
600+
mReadOnlyGlobalCommandArgument.SetValue(arg);
616601
}
617602

618603
size_t Command::AddArgument(const char * name, char ** value, const char * desc, uint8_t flags)
@@ -832,31 +817,36 @@ const char * Command::GetArgumentDescription(size_t index) const
832817
return nullptr;
833818
}
834819

820+
const char * Command::GetReadOnlyGlobalCommandArgument() const
821+
{
822+
if (GetAttribute())
823+
{
824+
return GetAttribute();
825+
}
826+
827+
if (GetEvent())
828+
{
829+
return GetEvent();
830+
}
831+
832+
return nullptr;
833+
}
834+
835835
const char * Command::GetAttribute() const
836836
{
837-
size_t argsCount = mArgs.size();
838-
for (size_t i = 0; i < argsCount; i++)
837+
if (mReadOnlyGlobalCommandArgument.HasValue())
839838
{
840-
Argument arg = mArgs.at(i);
841-
if (arg.type == ArgumentType::Attribute)
842-
{
843-
return reinterpret_cast<const char *>(arg.value);
844-
}
839+
return mReadOnlyGlobalCommandArgument.Value().value;
845840
}
846841

847842
return nullptr;
848843
}
849844

850845
const char * Command::GetEvent() const
851846
{
852-
size_t argsCount = mArgs.size();
853-
for (size_t i = 0; i < argsCount; i++)
847+
if (mReadOnlyGlobalCommandArgument.HasValue())
854848
{
855-
Argument arg = mArgs.at(i);
856-
if (arg.type == ArgumentType::Attribute)
857-
{
858-
return reinterpret_cast<const char *>(arg.value);
859-
}
849+
return mReadOnlyGlobalCommandArgument.Value().value;
860850
}
861851

862852
return nullptr;
@@ -943,11 +933,6 @@ void Command::ResetArguments()
943933
VerifyOrDie(false);
944934
break;
945935
}
946-
case ArgumentType::Attribute: {
947-
// No optional Attribute arguments so far.
948-
VerifyOrDie(false);
949-
break;
950-
}
951936
case ArgumentType::String: {
952937
ResetOptionalArg<char *>(arg);
953938
break;

examples/chip-tool/commands/common/Command.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ enum ArgumentType
6565
String,
6666
CharString,
6767
OctetString,
68-
Attribute,
6968
Address,
7069
Complex,
7170
Custom,
@@ -95,6 +94,13 @@ struct Argument
9594
bool isNullable() const { return flags & kNullable; }
9695
};
9796

97+
struct ReadOnlyGlobalCommandArgument
98+
{
99+
const char * name;
100+
const char * value;
101+
const char * desc;
102+
};
103+
98104
class Command
99105
{
100106
public:
@@ -109,6 +115,7 @@ class Command
109115

110116
const char * GetName(void) const { return mName; }
111117
const char * GetHelpText() const { return mHelpText; }
118+
const char * GetReadOnlyGlobalCommandArgument(void) const;
112119
const char * GetAttribute(void) const;
113120
const char * GetEvent(void) const;
114121
const char * GetArgumentName(size_t index) const;
@@ -117,7 +124,7 @@ class Command
117124
size_t GetArgumentsCount(void) const { return mArgs.size(); }
118125

119126
bool InitArguments(int argc, char ** argv);
120-
size_t AddArgument(const char * name, const char * value, const char * desc = "", uint8_t flags = 0);
127+
void AddArgument(const char * name, const char * value, const char * desc = "");
121128
/**
122129
* @brief
123130
* Add a char string command argument
@@ -274,5 +281,7 @@ class Command
274281
const char * mName = nullptr;
275282
const char * mHelpText = nullptr;
276283
bool mIsInteractive = false;
284+
285+
chip::Optional<ReadOnlyGlobalCommandArgument> mReadOnlyGlobalCommandArgument;
277286
std::vector<Argument> mArgs;
278287
};

examples/chip-tool/commands/common/Commands.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ CHIP_ERROR Commands::RunCommand(int argc, char ** argv, bool interactive)
9393
return CHIP_ERROR_INVALID_ARGUMENT;
9494
}
9595

96-
if (!IsGlobalCommand(argv[2]))
96+
bool isGlobalCommand = IsGlobalCommand(argv[2]);
97+
if (!isGlobalCommand)
9798
{
9899
command = GetCommand(cluster->second, argv[2]);
99100
if (command == nullptr)
@@ -138,7 +139,8 @@ CHIP_ERROR Commands::RunCommand(int argc, char ** argv, bool interactive)
138139
}
139140
}
140141

141-
if (!command->InitArguments(argc - 3, &argv[3]))
142+
int argumentsPosition = isGlobalCommand ? 4 : 3;
143+
if (!command->InitArguments(argc - argumentsPosition, &argv[argumentsPosition]))
142144
{
143145
ShowCommand(argv[0], argv[1], command);
144146
return CHIP_ERROR_INVALID_ARGUMENT;
@@ -321,6 +323,12 @@ void Commands::ShowCommand(std::string executable, std::string clusterName, Comm
321323
std::string description;
322324
arguments += command->GetName();
323325

326+
if (command->GetReadOnlyGlobalCommandArgument())
327+
{
328+
arguments += ' ';
329+
arguments += command->GetReadOnlyGlobalCommandArgument();
330+
}
331+
324332
size_t argumentsCount = command->GetArgumentsCount();
325333
for (size_t i = 0; i < argumentsCount; i++)
326334
{

0 commit comments

Comments
 (0)