diff --git a/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp b/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp index f73a7aed..b04bf35a 100644 --- a/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp +++ b/cpp_utils/include/cpp_utils/user_interface/CommandReader.hpp @@ -94,6 +94,9 @@ class CommandReader void read_command_callback_( std::string command_read); + std::vector join_quoted_strings( + const std::vector& input); + //! Builder to transform string into a command enum value. EnumBuilder builder_; diff --git a/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp b/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp index 443ac811..6ed2f514 100644 --- a/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp +++ b/cpp_utils/include/cpp_utils/user_interface/impl/CommandReader.ipp @@ -45,13 +45,45 @@ bool CommandReader::read_next_command( std::string full_command = commands_read_.consume(); // Divide command - command.arguments = utils::split_string(full_command, " "); - + command.arguments = join_quoted_strings(utils::split_string(full_command, " ")); // Check if command exists // The args are already set in command, and the enum value will be set string_to_enumeration return builder_.string_to_enumeration(command.arguments[0], command.command); } +template +std::vector CommandReader::join_quoted_strings( + const std::vector& input) +{ + std::vector result; + + for (size_t i = 0; i < input.size(); ++i) + { + // Check if string starts with a quote + if (!input[i].empty() && input[i].front() == '"') + { + std::string joined = input[i]; + + // Keep joining until we find a string ending with a quote + while (i + 1 < input.size() && + (joined.empty() || joined.back() != '"')) + { + joined += " " + input[++i]; + } + + result.push_back(joined.substr(1, joined.size() - 2)); + } + else + { + result.push_back(input[i]); + } + } + + return result; +} + + + template void CommandReader::read_command_callback_( std::string command_read)