diff --git a/slsDetectorSoftware/CMakeLists.txt b/slsDetectorSoftware/CMakeLists.txt index 5ac91741f..6d202cbd0 100644 --- a/slsDetectorSoftware/CMakeLists.txt +++ b/slsDetectorSoftware/CMakeLists.txt @@ -7,6 +7,7 @@ set(SOURCES ${PROJECT_SOURCE_DIR}/slsSupportLib/src/ClientInterface.cpp ${PROJECT_SOURCE_DIR}/slsSupportLib/src/utilities.cpp ${PROJECT_SOURCE_DIR}/slsSupportLib/src/string_utils.cpp + ${PROJECT_SOURCE_DIR}/slsSupportLib/src/CmdLineParser.cpp ) set(HEADERS diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h index 28f9da73f..93b3fb9d0 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h @@ -2,6 +2,7 @@ #include #include +#include "CmdLineParser.h" #include "container_utils.h" #include "multiSlsDetector.h" #include "multiSlsDetectorCommand.h" diff --git a/slsSupportLib/include/CmdLineParser.h b/slsSupportLib/include/CmdLineParser.h new file mode 100644 index 000000000..c241d6666 --- /dev/null +++ b/slsSupportLib/include/CmdLineParser.h @@ -0,0 +1,27 @@ +#ifndef CMD_LINE_PARSER_H +#define CMD_LINE_PARSER_H +#include +#include +#include + +class CmdLineParser { +public: + void Parse(int argc, char* argv[]); + void Parse(std::string s); + void Print(); + + //getters + int multi_id() const { return multi_id_; }; + int detector_id() const { return detector_id_; }; + std::string command() const { return command_; } + const std::vector& arguments() { return arguments_; }; + +private: + void DecodeIdAndPosition(const char* c); + int multi_id_ = 0; + int detector_id_ = -1; + std::string command_; + std::vector arguments_; +}; + +#endif // CMD_LINE_PARSER_H \ No newline at end of file diff --git a/slsSupportLib/src/CmdLineParser.cpp b/slsSupportLib/src/CmdLineParser.cpp new file mode 100644 index 000000000..51a5a682a --- /dev/null +++ b/slsSupportLib/src/CmdLineParser.cpp @@ -0,0 +1,67 @@ + +#include "CmdLineParser.h" +#include +#include +#include +#include +#include +//printing function for debugging +void CmdLineParser::Print() +{ + std::cout << "\nCmdLineParser::Print()\n"; + std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl; + std::cout << "\tcommand: " << command_ << std::endl; + std::cout << "\targuments: "; + for (size_t i = 0; i < arguments_.size(); ++i) { + std::cout << arguments_[i] << " "; + } + std::cout << "\n\n"; +}; + +void CmdLineParser::Parse(int argc, char* argv[]) +{ + //first element of argv is the command used to call the executable ->skipping + //and if this is the only command skip all + if (argc > 1) { + //second element is cmd string that needs to be decoded + DecodeIdAndPosition(argv[1]); + //The rest of the arguments goes into a vector for later processing + for (int i = 2; i < argc; ++i) + arguments_.push_back(std::string(argv[i])); + } +}; + +void CmdLineParser::Parse(std::string s){ + std::istringstream iss(s); + auto it = std::istream_iterator(iss); + //read the first element and increment + command_ = *it++; + arguments_ = std::vector(it, std::istream_iterator());; + DecodeIdAndPosition(command_.c_str()); +} + +void CmdLineParser::DecodeIdAndPosition(const char* c) +{ + bool contains_id = std::strchr(c, '-'); + bool contains_pos = std::strchr(c, ':'); + char tmp[100]; + + if (contains_id && contains_pos) { + int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp); + if (r != 3) + throw(std::invalid_argument("Cannot decode client or detector id from: \"" + std::string(c) + "\"\n")); + command_ = tmp; + } else if (contains_id && !contains_pos) { + int r = sscanf(c, "%d-%s", &multi_id_, tmp); + if (r != 2) + throw(std::invalid_argument("Cannot decode client id from: \"" + std::string(c) + "\"\n")); + command_ = tmp; + } else if (!contains_id && contains_pos) { + int r = sscanf(c, "%d:%s", &detector_id_, tmp); + if (r != 2) + throw(std::invalid_argument("Cannot decode detector id from: \"" + std::string(c) + "\"\n")); + command_ = tmp; + } else { + command_ = c; + } +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4a4dcac4e..9b6ce4f83 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -18,7 +18,7 @@ if(USE_TESTS) ${LOCAL_TEST_DIR}/test-container_utils.cpp ${LOCAL_TEST_DIR}/test-string_utils.cpp ${LOCAL_TEST_DIR}/test-MySocketTCP.cpp - + ${LOCAL_TEST_DIR}/test-CmdLineParser.cpp #${LOCAL_TEST_DIR}/test-multiDetector.cpp ${LOCAL_TEST_DIR}/test.cpp # PARENT_SCOPE diff --git a/tests/src/test-CmdLineParser.cpp b/tests/src/test-CmdLineParser.cpp new file mode 100644 index 000000000..da469178b --- /dev/null +++ b/tests/src/test-CmdLineParser.cpp @@ -0,0 +1,278 @@ +#include "CmdLineParser.h" +#include "catch.hpp" +#include +#include +//tests to add +//help for all docs +//command for all depreciated commands + +TEST_CASE("Parse with no arguments results in no command and default id") +{ + //build up argc and argv + //first argument is the command used to call the binary + int argc = 1; + char* argv[argc]; + char a0[] = "call"; + argv[0] = a0; + + CmdLineParser p; + p.Parse(argc, argv); + + REQUIRE(p.detector_id() == -1); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Parse empty string") +{ + std::string s = ""; + CmdLineParser p; + p.Parse(s); + + REQUIRE(p.detector_id() == -1); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Parse a command without client id and detector id results in default") +{ + int argc = 2; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "vrf"; + argv[0] = a0; + argv[1] = a1; + + CmdLineParser p; + p.Parse(argc, argv); + + REQUIRE(p.detector_id() == -1); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Parse a string without client id and detector id results in default") +{ + std::string s = "vrf"; + CmdLineParser p; + p.Parse(s); + + REQUIRE(p.detector_id() == -1); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Parse a command with value but without client or detector id") +{ + int argc = 3; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "vrf"; + char a2[] = "3000"; + argv[0] = a0; + argv[1] = a1; + argv[2] = a2; + + CmdLineParser p; + p.Parse(argc, argv); + + REQUIRE(p.detector_id() == -1); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 1); + REQUIRE(p.arguments()[0] == std::string("3000")); +} +TEST_CASE("Parse a string with value but without client or detector id") +{ + std::string s = "vrf 3000\n"; + + CmdLineParser p; + p.Parse(s); + + REQUIRE(p.detector_id() == -1); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 1); + REQUIRE(p.arguments()[0] == std::string("3000")); +} + +TEST_CASE("Decodes position") +{ + int argc = 2; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "7:vrf"; + argv[0] = a0; + argv[1] = a1; + + CmdLineParser p; + p.Parse(argc, argv); + + REQUIRE(p.detector_id() == 7); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} +TEST_CASE("Decodes position from string") +{ + std::string s = "7:vrf\n"; + + CmdLineParser p; + p.Parse(s); + + REQUIRE(p.detector_id() == 7); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Decodes double digit position") +{ + int argc = 2; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "73:vcmp"; + argv[0] = a0; + argv[1] = a1; + + CmdLineParser p; + p.Parse(argc, argv); + + REQUIRE(p.detector_id() == 73); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vcmp")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Decodes double digit position from string") +{ + + std::string s = "73:vcmp"; + CmdLineParser p; + p.Parse(s); + + REQUIRE(p.detector_id() == 73); + REQUIRE(p.multi_id() == 0); + REQUIRE(p.command() == std::string("vcmp")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Decodes position and id") +{ + int argc = 2; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "5-8:vrf"; + argv[0] = a0; + argv[1] = a1; + + CmdLineParser p; + p.Parse(argc, argv); + + REQUIRE(p.detector_id() == 8); + REQUIRE(p.multi_id() == 5); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} +TEST_CASE("Decodes position and id from string") +{ + + std::string s = "5-8:vrf"; + + + CmdLineParser p; + p.Parse(s); + + REQUIRE(p.detector_id() == 8); + REQUIRE(p.multi_id() == 5); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Double digit id") +{ + int argc = 2; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "56-8:vrf"; + argv[0] = a0; + argv[1] = a1; + + CmdLineParser p; + p.Parse(argc, argv); + + REQUIRE(p.detector_id() == 8); + REQUIRE(p.multi_id() == 56); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Double digit id from string") +{ + std::string s = "56-8:vrf"; + + + CmdLineParser p; + p.Parse(s); + + REQUIRE(p.detector_id() == 8); + REQUIRE(p.multi_id() == 56); + REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.arguments().size() == 0); +} + +TEST_CASE("Calling with wrong id throws invalid_argument") +{ + + int argc = 2; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "asvldkn:vrf"; + argv[0] = a0; + argv[1] = a1; + + CmdLineParser p; + CHECK_THROWS(p.Parse(argc, argv)); +} +TEST_CASE("Calling with string with wrong id throws invalid_argument") +{ + std::string s = "asvldkn:vrf"; + CmdLineParser p; + CHECK_THROWS(p.Parse(s)); +} + + +TEST_CASE("Calling with wrong client throws invalid_argument") +{ + + int argc = 2; + char* argv[argc]; + char a0[] = "call"; + char a1[] = "lki-3:vrf"; + argv[0] = a0; + argv[1] = a1; + + CmdLineParser p; + CHECK_THROWS(p.Parse(argc, argv)); +} +TEST_CASE("Calling with string with wrong client throws invalid_argument") +{ + std::string s = "lki-3:vrf"; + CmdLineParser p; + CHECK_THROWS(p.Parse(s)); +} + +TEST_CASE("Parses string with two arguments"){ + std::string s = "trimen 3000 4000\n"; + CmdLineParser p; + p.Parse(s); + + REQUIRE("trimen" == p.command()); + REQUIRE("3000" == p.arguments()[0]); + REQUIRE("4000" == p.arguments()[1]); + REQUIRE(2 == p.arguments().size()); +} \ No newline at end of file