adding cmdline parser

This commit is contained in:
Erik Frojdh
2019-01-16 16:50:19 +01:00
parent b19b2a044c
commit db0807bf7b
6 changed files with 375 additions and 1 deletions

View File

@ -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

View File

@ -2,6 +2,7 @@
#include <iostream>
#include <string>
#include "CmdLineParser.h"
#include "container_utils.h"
#include "multiSlsDetector.h"
#include "multiSlsDetectorCommand.h"

View File

@ -0,0 +1,27 @@
#ifndef CMD_LINE_PARSER_H
#define CMD_LINE_PARSER_H
#include <stdexcept>
#include <string>
#include <vector>
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<std::string>& arguments() { return arguments_; };
private:
void DecodeIdAndPosition(const char* c);
int multi_id_ = 0;
int detector_id_ = -1;
std::string command_;
std::vector<std::string> arguments_;
};
#endif // CMD_LINE_PARSER_H

View File

@ -0,0 +1,67 @@
#include "CmdLineParser.h"
#include <cstdio>
#include <cstring>
#include <sstream>
#include <iterator>
#include <iostream>
//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<std::string>(iss);
//read the first element and increment
command_ = *it++;
arguments_ = std::vector<std::string>(it, std::istream_iterator<std::string>());;
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;
}
}

View File

@ -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

View File

@ -0,0 +1,278 @@
#include "CmdLineParser.h"
#include "catch.hpp"
#include <exception>
#include <string>
//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());
}