added -h --help option to display help

This commit is contained in:
Erik Frojdh
2019-06-21 17:27:47 +02:00
parent 2d3f5a03ca
commit e80ce1b4c4
5 changed files with 86 additions and 21 deletions

View File

@ -1,5 +1,6 @@
#include "CmdLineParser.h"
#include "sls_detector_defs.h"
#include <cstdio>
#include <cstring>
#include <iostream>
@ -20,22 +21,37 @@ void CmdLineParser::Print() {
std::cout << "\n\n";
};
void CmdLineParser::Parse(int argc, const char * const argv[]) {
executable_ = argv[0]; //first arg is calling binary
void CmdLineParser::Parse(int argc, const char *const argv[]) {
executable_ = argv[0]; // first arg is calling binary
if (argc > 1) {
DecodeIdAndPosition(argv[1]);
std::string s = argv[1];
for (int i = 2; i < argc; ++i) {
arguments_.emplace_back(argv[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);
command_ = *it++;
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());
}
@ -47,7 +63,7 @@ void CmdLineParser::DecodeIdAndPosition(const char *c) {
if (contains_id && contains_pos) {
int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp);
if (r != 3) {
throw(std::invalid_argument(
throw(sls::RuntimeError(
"Cannot decode client or detector id from: \"" +
std::string(c) + "\"\n"));
}
@ -55,15 +71,15 @@ void CmdLineParser::DecodeIdAndPosition(const char *c) {
} 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"));
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(std::invalid_argument("Cannot decode detector id from: \"" +
std::string(c) + "\"\n"));
throw(sls::RuntimeError("Cannot decode detector id from: \"" +
std::string(c) + "\"\n"));
}
command_ = tmp;
} else {