Moved CmdLineProxy

This commit is contained in:
Erik Frojdh
2019-11-05 11:07:07 +01:00
parent b3587bcee5
commit 2f2e4da628
10 changed files with 68 additions and 42 deletions

View File

@ -0,0 +1,112 @@
#include "CmdLineParser.h"
#include "sls_detector_defs.h"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <sstream>
namespace sls {
void CmdLineParser::Print() {
std::cout << "\nCmdLineParser::Print()\n";
std::cout << "\tmulti_id: " << multi_id_
<< ", detector_id: " << detector_id_ << std::endl;
std::cout << "\texecutable: " << executable_ << '\n';
std::cout << "\tcommand: " << command_ << '\n';
std::cout << "\tn_arguments: " << n_arguments() << '\n';
std::cout << "\targuments: ";
for (const auto &argument : arguments_) {
std::cout << argument << " ";
}
std::cout << "\n\n";
};
void CmdLineParser::Parse(int argc, const char *const argv[]) {
executable_ = argv[0]; // first arg is calling binary
if (argc > 1) {
std::string s = argv[1];
for (int i = 2; i < argc; ++i) {
s += " ";
s += argv[i];
}
Parse(s);
}
}
void CmdLineParser::Parse(const std::string &s) {
std::istringstream iss(s);
auto it = std::istream_iterator<std::string>(iss);
arguments_ =
std::vector<std::string>(it, std::istream_iterator<std::string>());
auto old_size = arguments_.size();
arguments_.erase(std::remove_if(begin(arguments_), end(arguments_),
[](const std::string &item) {
if (item == "-h" || item == "--help")
return true;
return false;
}),
end(arguments_));
if (old_size - arguments_.size() > 0)
help_ = true;
if (!arguments_.empty()) {
command_ = arguments_[0];
arguments_.erase(begin(arguments_));
}
DecodeIdAndPosition(command_.c_str());
}
void CmdLineParser::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 *> CmdLineParser::argv() const {
std::vector<const char *> vec;
if (command_.empty() != true) {
vec.push_back(&command_.front());
}
for (auto &arg : arguments_) {
vec.push_back(&arg.front());
}
return vec;
}
std::string CmdLineParser::cli_line() const{
std::ostringstream os;
os << command_;
for (const auto & arg : arguments_)
os << " " << arg;
return os.str();
}
} // namespace sls

View File

@ -0,0 +1,51 @@
/*
This class parses command line input or string input to extract the
multi_id, detector_id, command and arguments. It's used in the command
line binaries (sls_detector_get/put) to parse commands and in the
slsDetectorShared library to parse input from config files.
This class is fully internal to the project and NO guarantees are given
on the stability of the interface or implementation. This is also the
reason that the header file is not exposed.
*/
#ifndef CMD_LINE_PARSER_H
#define CMD_LINE_PARSER_H
#include <string>
#include <vector>
namespace sls {
class CmdLineParser {
public:
void Parse(int argc, const char *const argv[]);
void Parse(const std::string &s);
void Print();
int multi_id() const noexcept { return multi_id_; };
int detector_id() const noexcept { return detector_id_; };
int n_arguments() const noexcept { return arguments_.size(); }
const std::string &command() const noexcept{ return command_; }
void setCommand(std::string cmd) { command_ = cmd; }
bool isHelp() const noexcept { return help_; }
const std::string &executable() const noexcept{ return executable_; }
const std::vector<std::string> &arguments() const noexcept{ return arguments_; };
std::vector<const char *> argv() const;
std::string cli_line() const;
private:
void DecodeIdAndPosition(const char *c);
int multi_id_ = 0;
int detector_id_ = -1;
bool help_{false};
std::string command_;
std::string executable_;
std::vector<std::string> arguments_;
};
} // namespace sls
#endif // CMD_LINE_PARSER_H

View File

@ -0,0 +1,23 @@
#pragma once
#include <iostream>
#include "CmdLineParser.h"
class multiSlsDetector;
class multiSlsDetectorClient {
public:
multiSlsDetectorClient(int argc, char *argv[], int action,
multiSlsDetector *myDetector = nullptr,
std::ostream &output = std::cout);
multiSlsDetectorClient(const std::string &args, int action,
multiSlsDetector *myDetector = nullptr,
std::ostream &output = std::cout);
private:
int action_;
sls::CmdLineParser parser;
multiSlsDetector *detPtr = nullptr;
std::ostream &os;
void runCommand();
};