Skip to content

Commit 50eae82

Browse files
Add support for "true" and "false" for boolean args in chip-tool. (#18091)
* Add support for "true" and "false" for boolean args in chip-tool. This continues allowing 0 and 1 for now for backwards compat, as aliases for false and true. Other numeric values continue not being allowed. * Address review comments
1 parent f1e70f9 commit 50eae82

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

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

+48-14
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,27 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
182182
bool isHexNotation = strncmp(argValue, "0x", 2) == 0 || strncmp(argValue, "0X", 2) == 0;
183183

184184
Argument arg = mArgs.at(argIndex);
185+
186+
// We have two places where we handle uint8_t-typed args (actual int8u and
187+
// bool args), so declare the handler function here so it can be reused.
188+
auto uint8Handler = [&](uint8_t * value) {
189+
// stringstream treats uint8_t as char, which is not what we want here.
190+
uint16_t tmpValue;
191+
std::stringstream ss;
192+
isHexNotation ? (ss << std::hex << argValue) : (ss << argValue);
193+
ss >> tmpValue;
194+
if (chip::CanCastTo<uint8_t>(tmpValue))
195+
{
196+
*value = static_cast<uint8_t>(tmpValue);
197+
198+
uint64_t min = chip::CanCastTo<uint64_t>(arg.min) ? static_cast<uint64_t>(arg.min) : 0;
199+
uint64_t max = arg.max;
200+
return (!ss.fail() && ss.eof() && *value >= min && *value <= max);
201+
}
202+
203+
return false;
204+
};
205+
185206
switch (arg.type)
186207
{
187208
case ArgumentType::Complex: {
@@ -330,28 +351,41 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
330351
break;
331352
}
332353

333-
case ArgumentType::Bool:
334-
case ArgumentType::Number_uint8: {
335-
isValidArgument = HandleNullableOptional<uint8_t>(arg, argValue, [&](auto * value) {
336-
// stringstream treats uint8_t as char, which is not what we want here.
337-
uint16_t tmpValue;
338-
std::stringstream ss;
339-
isHexNotation ? ss << std::hex << argValue : ss << argValue;
340-
ss >> tmpValue;
341-
if (chip::CanCastTo<uint8_t>(tmpValue))
354+
case ArgumentType::Bool: {
355+
isValidArgument = HandleNullableOptional<bool>(arg, argValue, [&](auto * value) {
356+
// Start with checking for actual boolean values.
357+
if (strcasecmp(argValue, "true") == 0)
342358
{
343-
*value = static_cast<uint8_t>(tmpValue);
359+
*value = true;
360+
return true;
361+
}
344362

345-
uint64_t min = chip::CanCastTo<uint64_t>(arg.min) ? static_cast<uint64_t>(arg.min) : 0;
346-
uint64_t max = arg.max;
347-
return (!ss.fail() && ss.eof() && *value >= min && *value <= max);
363+
if (strcasecmp(argValue, "false") == 0)
364+
{
365+
*value = false;
366+
return true;
348367
}
349368

350-
return false;
369+
// For backwards compat, keep accepting 0 and 1 for now as synonyms
370+
// for false and true. Since we set our min to 0 and max to 1 for
371+
// booleans, calling uint8Handler does the right thing in terms of
372+
// only allowing those two values.
373+
uint8_t temp = 0;
374+
if (!uint8Handler(&temp))
375+
{
376+
return false;
377+
}
378+
*value = (temp == 1);
379+
return true;
351380
});
352381
break;
353382
}
354383

384+
case ArgumentType::Number_uint8: {
385+
isValidArgument = HandleNullableOptional<uint8_t>(arg, argValue, uint8Handler);
386+
break;
387+
}
388+
355389
case ArgumentType::Number_uint16: {
356390
isValidArgument = HandleNullableOptional<uint16_t>(arg, argValue, [&](auto * value) {
357391
std::stringstream ss;

0 commit comments

Comments
 (0)