This commit is contained in:
Erik Frojdh
2021-09-13 21:26:17 +02:00
parent 9d4d667df1
commit abf56ad643
4 changed files with 153 additions and 60 deletions

View File

@ -36,10 +36,27 @@ void CmdParser::Parse(int argc, const char *const argv[]) {
}
}
void CmdParser::Parse(const std::string &s) {
void CmdParser::Parse(std::string s) {
//taking s by value we can modify it.
Reset();
//Are we looking at -h --help?
auto pos = s.find_first_not_of("0123456789:- ");
if (pos!=0){
auto pre = s.substr(0, pos);
pre.erase(std::remove(pre.begin(), pre.end(), ' '), pre.end());
s.erase(0, pos);
DecodeIdAndPosition(pre.c_str());
}
//Load string
std::istringstream iss(s);
auto it = std::istream_iterator<std::string>(iss);
command_ = *it++; //First arg is the comand to run
arguments_ =
std::vector<std::string>(it, std::istream_iterator<std::string>());
auto old_size = arguments_.size();
@ -51,51 +68,89 @@ void CmdParser::Parse(const std::string &s) {
end(arguments_));
if (old_size - arguments_.size() > 0)
help_ = true;
if (!arguments_.empty()) {
command_ = arguments_[0];
arguments_.erase(begin(arguments_));
}
// if (!arguments_.empty()) {
// command_ = arguments_[0];
// arguments_.erase(begin(arguments_));
// }
//allow comma sep
for (auto& arg : arguments_){
if (arg.back() == ',')
arg.pop_back();
}
DecodeIdAndPosition(command_.c_str());
// DecodeIdAndPosition(command_.c_str());
}
void CmdParser::DecodeIdAndPosition(const char *c) {
bool contains_id = std::strchr(c, '-') != nullptr;
bool contains_pos = std::strchr(c, ':') != nullptr;
char tmp[100];
// if (!isdigit(c[0])){
// // The first char is not a digit which means we have a command.
// // or at least a candidate, calling could still fail
// command_ = c;
// return;
// }
if (contains_id && contains_pos) {
int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp);
if (r != 3) {
int r = sscanf(c, "%d-%d:", &multi_id_, &detector_id_);
if (r != 2) {
throw(sls::RuntimeError(
"Cannot decode client or detector id from: \"" +
std::string(c) + "\"\n"));
}
command_ = tmp;
// command_ = tmp;
} else if (contains_id && !contains_pos) {
int r = sscanf(c, "%d-%s", &multi_id_, tmp);
if (r != 2) {
int r = sscanf(c, "%d-", &multi_id_);
if (r != 1) {
throw(sls::RuntimeError("Cannot decode client id from: \"" +
std::string(c) + "\"\n"));
}
command_ = tmp;
// command_ = tmp;
} else if (!contains_id && contains_pos) {
int r = sscanf(c, "%d:%s", &detector_id_, tmp);
if (r != 2) {
int r = sscanf(c, "%d:", &detector_id_);
if (r != 1) {
throw(sls::RuntimeError("Cannot decode detector id from: \"" +
std::string(c) + "\"\n"));
}
command_ = tmp;
// command_ = tmp;
} else {
command_ = c;
// command_ = c;
}
}
// void CmdParser::DecodeIdAndPosition(const char *c) {
// bool contains_id = std::strchr(c, '-') != nullptr;
// bool contains_pos = std::strchr(c, ':') != nullptr;
// char tmp[100];
// if (contains_id && contains_pos) {
// int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp);
// if (r != 3) {
// throw(sls::RuntimeError(
// "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(sls::RuntimeError("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(sls::RuntimeError("Cannot decode detector id from: \"" +
// std::string(c) + "\"\n"));
// }
// command_ = tmp;
// } else {
// command_ = c;
// }
// }
std::vector<const char *> CmdParser::argv() const {
std::vector<const char *> vec;
if (!command_.empty()) {

View File

@ -22,7 +22,7 @@ namespace sls {
class CmdParser {
public:
void Parse(int argc, const char *const argv[]);
void Parse(const std::string &s);
void Parse(std::string s);
void Print();
int multi_id() const noexcept { return multi_id_; };