mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-08 19:10:42 +02:00
using new CmdLineParser
This commit is contained in:
parent
db0807bf7b
commit
76da6a07aa
@ -805,9 +805,6 @@ int multiSlsDetector::execCommand(const std::string& cmd, int detPos) {
|
|||||||
int multiSlsDetector::readConfigurationFile(const std::string &fname) {
|
int multiSlsDetector::readConfigurationFile(const std::string &fname) {
|
||||||
freeSharedMemory();
|
freeSharedMemory();
|
||||||
setupMultiDetector();
|
setupMultiDetector();
|
||||||
|
|
||||||
char *args[100];
|
|
||||||
char myargs[100][1000];
|
|
||||||
FILE_LOG(logINFO) << "Loading configuration file: " << fname;
|
FILE_LOG(logINFO) << "Loading configuration file: " << fname;
|
||||||
|
|
||||||
std::ifstream input_file;
|
std::ifstream input_file;
|
||||||
@ -820,16 +817,7 @@ int multiSlsDetector::readConfigurationFile(const std::string &fname) {
|
|||||||
current_line.erase(current_line.find('#'));
|
current_line.erase(current_line.find('#'));
|
||||||
FILE_LOG(logDEBUG1) << "current_line after removing comments:\n\t" << current_line;
|
FILE_LOG(logDEBUG1) << "current_line after removing comments:\n\t" << current_line;
|
||||||
if (current_line.length() > 1) {
|
if (current_line.length() > 1) {
|
||||||
std::istringstream line_stream(current_line);
|
multiSlsDetectorClient(current_line, PUT_ACTION, this);
|
||||||
int n_arguments = 0;
|
|
||||||
std::string current_argument;
|
|
||||||
while (line_stream.good()) {
|
|
||||||
line_stream >> current_argument;
|
|
||||||
sls::strcpy_safe(myargs[n_arguments], current_argument.c_str());
|
|
||||||
args[n_arguments] = myargs[n_arguments];
|
|
||||||
++n_arguments;
|
|
||||||
}
|
|
||||||
multiSlsDetectorClient(n_arguments, args, PUT_ACTION, this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
input_file.close();
|
input_file.close();
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "CmdLineParser.h"
|
#include "CmdLineParser.h"
|
||||||
#include "container_utils.h"
|
#include "container_utils.h"
|
||||||
|
#include "string_utils.h"
|
||||||
#include "multiSlsDetector.h"
|
#include "multiSlsDetector.h"
|
||||||
#include "multiSlsDetectorCommand.h"
|
#include "multiSlsDetectorCommand.h"
|
||||||
#include "sls_detector_exceptions.h"
|
#include "sls_detector_exceptions.h"
|
||||||
@ -18,100 +19,84 @@ inline int dummyCallback(detectorData *d, int p, void *) {
|
|||||||
|
|
||||||
class multiSlsDetectorClient {
|
class multiSlsDetectorClient {
|
||||||
public:
|
public:
|
||||||
multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector = nullptr) {
|
multiSlsDetectorClient(int argc, char *argv[], int action, multiSlsDetector *myDetector = nullptr):
|
||||||
int id = -1, pos = -1, iv = 0;
|
action_(action),
|
||||||
bool verify = true, update = true;
|
detPtr(myDetector){
|
||||||
char cmd[100] = "";
|
parser.Parse(argc, argv);
|
||||||
|
runCommand();
|
||||||
|
|
||||||
if (action == slsDetectorDefs::PUT_ACTION && argc < 2) {
|
}
|
||||||
std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl;
|
multiSlsDetectorClient(const std::string& args, int action, multiSlsDetector *myDetector = nullptr):
|
||||||
|
action_(action),
|
||||||
|
detPtr(myDetector){
|
||||||
|
parser.Parse(args);
|
||||||
|
runCommand();
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
int action_;
|
||||||
|
CmdLineParser parser;
|
||||||
|
multiSlsDetector* detPtr = nullptr;
|
||||||
|
|
||||||
|
void runCommand(){
|
||||||
|
bool verify = true;
|
||||||
|
bool update = true;
|
||||||
|
if (action_ == slsDetectorDefs::PUT_ACTION && parser.n_arguments() == 0) {
|
||||||
|
std::cout << "Wrong usage - should be: " << parser.executable() << "[id-][pos:]channel arg" << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if (action == slsDetectorDefs::GET_ACTION && argc < 1) {
|
if (action_ == slsDetectorDefs::GET_ACTION && parser.command().empty()) {
|
||||||
std::cout << "Wrong usage - should be: " << argv[0] << "[id-][pos:]channel arg" << std::endl;
|
std::cout << "Wrong usage - should be: " << parser.executable() << "[id-][pos:]channel arg" << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (action == slsDetectorDefs::READOUT_ACTION) {
|
if (action_ == slsDetectorDefs::READOUT_ACTION && parser.detector_id() != -1) {
|
||||||
id = 0;
|
std::cout << "detector_id: " << parser.detector_id() << " ,readout of individual detectors is not allowed!" << std::endl;
|
||||||
pos = -1;
|
|
||||||
if (argc) { // multi id scanned
|
|
||||||
if (strchr(argv[0], '-')) {
|
|
||||||
iv = sscanf(argv[0], "%d-%s", &id, cmd); //%s needn't be there (if not 1:), so 1 or 2 arguments scanned
|
|
||||||
if (iv >= 1 && id >= 0) {
|
|
||||||
argv[0] = cmd;
|
|
||||||
std::cout << id << "-";
|
|
||||||
} else {
|
|
||||||
id = 0;
|
|
||||||
}
|
|
||||||
} // single id scanned
|
|
||||||
if (strchr(argv[0], ':')) {
|
|
||||||
iv = sscanf(argv[0], "%d:", &pos);
|
|
||||||
if (iv == 1 && pos >= 0) {
|
|
||||||
std::cout << "pos " << pos << " is not allowed for readout!" << std::endl;
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else { // multi id scanned
|
|
||||||
iv = sscanf(argv[0], "%d-%s", &id, cmd); // scan success
|
|
||||||
if (iv == 2 && id >= 0) {
|
|
||||||
argv[0] = cmd;
|
|
||||||
std::cout << id << "-";
|
|
||||||
} else {
|
|
||||||
id = 0;
|
|
||||||
} // sls pos scanned
|
|
||||||
iv = sscanf(argv[0], "%d:%s", &pos, cmd); // scan success
|
|
||||||
if (iv == 2 && pos >= 0) {
|
|
||||||
argv[0] = cmd;
|
|
||||||
std::cout << pos << ":";
|
|
||||||
}
|
|
||||||
if (iv != 2) {
|
|
||||||
pos = -1;
|
|
||||||
} // remove the %d- and %d:
|
|
||||||
if (strlen(cmd) == 0u) {
|
|
||||||
strcpy(cmd, argv[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// special commands
|
// special commands
|
||||||
std::string scmd = cmd; // free without calling multiSlsDetector constructor
|
if (parser.command() == "free") {
|
||||||
if (scmd == "free") {
|
multiSlsDetector::freeSharedMemory(parser.multi_id(), parser.detector_id());
|
||||||
multiSlsDetector::freeSharedMemory(id, pos);
|
|
||||||
return;
|
return;
|
||||||
} // get user details without verify sharedMultiSlsDetector version
|
} // get user details without verify sharedMultiSlsDetector version
|
||||||
else if ((scmd == "user") && (action == slsDetectorDefs::GET_ACTION)) {
|
else if ((parser.command() == "user") && (action_ == slsDetectorDefs::GET_ACTION)) {
|
||||||
verify = false;
|
verify = false;
|
||||||
update = false;
|
update = false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
//std::cout<<"id:"<<id<<" pos:"<<pos<<std::endl;
|
//std::cout<<"id:"<<id<<" pos:"<<pos<<std::endl;
|
||||||
// create multiSlsDetector class if required
|
// create multiSlsDetector class if required
|
||||||
std::unique_ptr<multiSlsDetector> localDet;
|
std::unique_ptr<multiSlsDetector> localDet;
|
||||||
if (myDetector == nullptr) {
|
if (detPtr == nullptr) {
|
||||||
try {
|
try {
|
||||||
localDet = sls::make_unique<multiSlsDetector>(id, verify, update);
|
localDet = sls::make_unique<multiSlsDetector>(parser.multi_id(), verify, update);
|
||||||
myDetector = localDet.get();
|
detPtr = localDet.get();
|
||||||
} catch (const SlsDetectorPackageExceptions &e) {
|
} catch (const SlsDetectorPackageExceptions &e) {
|
||||||
/*std::cout << e.GetMessage() << std::endl;*/
|
/*std::cout << e.GetMessage() << std::endl;*/
|
||||||
return;
|
return;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
std::cout << " caught exception" << std::endl;
|
std::cout << " caught exception\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pos >= myDetector->getNumberOfDetectors()) {
|
if (parser.detector_id() >= detPtr->getNumberOfDetectors()) {
|
||||||
std::cout << "position is out of bounds." << std::endl;
|
std::cout << "position is out of bounds.\n";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// call multi detector command line
|
// call multi detector command line
|
||||||
multiSlsDetectorCommand myCmd(myDetector);
|
multiSlsDetectorCommand myCmd(detPtr);
|
||||||
std::string answer = myCmd.executeLine(argc, argv, action, pos);
|
std::string answer = myCmd.executeLine(parser.n_arguments()+1, parser.argv().data(), action_, parser.detector_id());
|
||||||
|
|
||||||
if (action != slsDetectorDefs::READOUT_ACTION) {
|
if (parser.multi_id()!=0)
|
||||||
std::cout << argv[0] << " ";
|
std::cout << parser.multi_id() << '-';
|
||||||
|
if (parser.detector_id() != -1)
|
||||||
|
std::cout << parser.detector_id() << ':';
|
||||||
|
|
||||||
|
if (action_ != slsDetectorDefs::READOUT_ACTION) {
|
||||||
|
std::cout << parser.command() << " ";
|
||||||
}
|
}
|
||||||
std::cout << answer << std::endl;
|
std::cout << answer << std::endl;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ int main(int argc, char *argv[]) {
|
|||||||
int action = slsDetectorDefs::HELP_ACTION;
|
int action = slsDetectorDefs::HELP_ACTION;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (argc > 1)
|
// if (argc > 1)
|
||||||
argv++;
|
// argv++;
|
||||||
multiSlsDetectorClient(argc - 1, argv, action);
|
multiSlsDetectorClient(argc, argv, action);
|
||||||
}
|
}
|
||||||
|
@ -13,14 +13,18 @@ public:
|
|||||||
//getters
|
//getters
|
||||||
int multi_id() const { return multi_id_; };
|
int multi_id() const { return multi_id_; };
|
||||||
int detector_id() const { return detector_id_; };
|
int detector_id() const { return detector_id_; };
|
||||||
std::string command() const { return command_; }
|
int n_arguments() const {return arguments_.size();}
|
||||||
const std::vector<std::string>& arguments() { return arguments_; };
|
const std::string& command() const { return command_; }
|
||||||
|
const std::string& executable() const { return executable_;}
|
||||||
|
const std::vector<std::string>& arguments() const{ return arguments_; };
|
||||||
|
std::vector<char*> argv();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void DecodeIdAndPosition(const char* c);
|
void DecodeIdAndPosition(const char* c);
|
||||||
int multi_id_ = 0;
|
int multi_id_ = 0;
|
||||||
int detector_id_ = -1;
|
int detector_id_ = -1;
|
||||||
std::string command_;
|
std::string command_;
|
||||||
|
std::string executable_;
|
||||||
std::vector<std::string> arguments_;
|
std::vector<std::string> arguments_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -10,7 +10,9 @@ void CmdLineParser::Print()
|
|||||||
{
|
{
|
||||||
std::cout << "\nCmdLineParser::Print()\n";
|
std::cout << "\nCmdLineParser::Print()\n";
|
||||||
std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl;
|
std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl;
|
||||||
std::cout << "\tcommand: " << command_ << std::endl;
|
std::cout << "\texecutable: " << executable_ << '\n';
|
||||||
|
std::cout << "\tcommand: " << command_ << '\n';
|
||||||
|
std::cout << "\tn_arguments: " << n_arguments() << '\n';
|
||||||
std::cout << "\targuments: ";
|
std::cout << "\targuments: ";
|
||||||
for (size_t i = 0; i < arguments_.size(); ++i) {
|
for (size_t i = 0; i < arguments_.size(); ++i) {
|
||||||
std::cout << arguments_[i] << " ";
|
std::cout << arguments_[i] << " ";
|
||||||
@ -22,12 +24,13 @@ void CmdLineParser::Parse(int argc, char* argv[])
|
|||||||
{
|
{
|
||||||
//first element of argv is the command used to call the executable ->skipping
|
//first element of argv is the command used to call the executable ->skipping
|
||||||
//and if this is the only command skip all
|
//and if this is the only command skip all
|
||||||
|
executable_ = argv[0];
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
//second element is cmd string that needs to be decoded
|
//second element is cmd string that needs to be decoded
|
||||||
DecodeIdAndPosition(argv[1]);
|
DecodeIdAndPosition(argv[1]);
|
||||||
//The rest of the arguments goes into a vector for later processing
|
//The rest of the arguments goes into a vector for later processing
|
||||||
for (int i = 2; i < argc; ++i)
|
for (int i = 2; i < argc; ++i)
|
||||||
arguments_.push_back(std::string(argv[i]));
|
arguments_.emplace_back(std::string(argv[i]));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -65,3 +68,11 @@ void CmdLineParser::DecodeIdAndPosition(const char* c)
|
|||||||
command_ = c;
|
command_ = c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<char*> CmdLineParser::argv(){
|
||||||
|
std::vector<char*> vec;
|
||||||
|
vec.push_back(&command_.front());
|
||||||
|
for (auto& arg: arguments_)
|
||||||
|
vec.push_back(&arg.front());
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user