mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-23 06:50:02 +02:00
renamed CmdLineParser to CmdParser
This commit is contained in:
parent
e996068328
commit
a28fa66e54
@ -4,7 +4,7 @@ set(SOURCES
|
|||||||
src/slsDetector.cpp
|
src/slsDetector.cpp
|
||||||
src/Detector.cpp
|
src/Detector.cpp
|
||||||
src/CmdProxy.cpp
|
src/CmdProxy.cpp
|
||||||
src/CmdLineParser.cpp
|
src/CmdParser.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
set(HEADERS
|
set(HEADERS
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
|
|
||||||
#include "CmdLineParser.h"
|
/*
|
||||||
|
This file is used to generate the command line binaries
|
||||||
|
(sls_detector_get/put/acquire/help). By defines in CMake
|
||||||
|
we get the different files.
|
||||||
|
|
||||||
|
*/
|
||||||
|
#include "CmdParser.h"
|
||||||
#include "CmdProxy.h"
|
#include "CmdProxy.h"
|
||||||
#include "Detector.h"
|
#include "Detector.h"
|
||||||
#include "sls_detector_defs.h"
|
#include "sls_detector_defs.h"
|
||||||
@ -35,7 +41,7 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sls::CmdLineParser parser;
|
sls::CmdParser parser;
|
||||||
parser.Parse(argc, argv);
|
parser.Parse(argc, argv);
|
||||||
|
|
||||||
// If we called sls_detector_acquire, add the acquire command
|
// If we called sls_detector_acquire, add the acquire command
|
||||||
@ -57,11 +63,11 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//How big should this try block be?
|
// How big should this try block be?
|
||||||
sls::Detector det(parser.multi_id());
|
sls::Detector det(parser.multi_id());
|
||||||
sls::CmdProxy proxy(&det);
|
sls::CmdProxy proxy(&det);
|
||||||
proxy.Call(parser.command(), parser.arguments(),
|
proxy.Call(parser.command(), parser.arguments(), parser.detector_id(),
|
||||||
parser.detector_id(), action);
|
action);
|
||||||
} catch (const sls::RuntimeError &e) {
|
} catch (const sls::RuntimeError &e) {
|
||||||
// OK to catch and do nothing since this will print the error message
|
// OK to catch and do nothing since this will print the error message
|
||||||
// and command line app will anyway exit
|
// and command line app will anyway exit
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
#include "CmdLineParser.h"
|
#include "CmdParser.h"
|
||||||
#include "sls_detector_defs.h"
|
#include "sls_detector_defs.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -9,8 +9,8 @@
|
|||||||
|
|
||||||
namespace sls {
|
namespace sls {
|
||||||
|
|
||||||
void CmdLineParser::Print() {
|
void CmdParser::Print() {
|
||||||
std::cout << "\nCmdLineParser::Print()\n";
|
std::cout << "\nCmdParser::Print()\n";
|
||||||
std::cout << "\tmulti_id: " << multi_id_
|
std::cout << "\tmulti_id: " << multi_id_
|
||||||
<< ", detector_id: " << detector_id_ << std::endl;
|
<< ", detector_id: " << detector_id_ << std::endl;
|
||||||
std::cout << "\texecutable: " << executable_ << '\n';
|
std::cout << "\texecutable: " << executable_ << '\n';
|
||||||
@ -23,7 +23,7 @@ void CmdLineParser::Print() {
|
|||||||
std::cout << "\n\n";
|
std::cout << "\n\n";
|
||||||
};
|
};
|
||||||
|
|
||||||
void CmdLineParser::Parse(int argc, const char *const argv[]) {
|
void CmdParser::Parse(int argc, const char *const argv[]) {
|
||||||
Reset();
|
Reset();
|
||||||
executable_ = argv[0]; // first arg is calling binary
|
executable_ = argv[0]; // first arg is calling binary
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
@ -36,7 +36,7 @@ void CmdLineParser::Parse(int argc, const char *const argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdLineParser::Parse(const std::string &s) {
|
void CmdParser::Parse(const std::string &s) {
|
||||||
Reset();
|
Reset();
|
||||||
std::istringstream iss(s);
|
std::istringstream iss(s);
|
||||||
auto it = std::istream_iterator<std::string>(iss);
|
auto it = std::istream_iterator<std::string>(iss);
|
||||||
@ -59,7 +59,7 @@ void CmdLineParser::Parse(const std::string &s) {
|
|||||||
DecodeIdAndPosition(command_.c_str());
|
DecodeIdAndPosition(command_.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdLineParser::DecodeIdAndPosition(const char *c) {
|
void CmdParser::DecodeIdAndPosition(const char *c) {
|
||||||
bool contains_id = std::strchr(c, '-') != nullptr;
|
bool contains_id = std::strchr(c, '-') != nullptr;
|
||||||
bool contains_pos = std::strchr(c, ':') != nullptr;
|
bool contains_pos = std::strchr(c, ':') != nullptr;
|
||||||
char tmp[100];
|
char tmp[100];
|
||||||
@ -91,7 +91,7 @@ void CmdLineParser::DecodeIdAndPosition(const char *c) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<const char *> CmdLineParser::argv() const {
|
std::vector<const char *> CmdParser::argv() const {
|
||||||
std::vector<const char *> vec;
|
std::vector<const char *> vec;
|
||||||
if (command_.empty() != true) {
|
if (command_.empty() != true) {
|
||||||
vec.push_back(&command_.front());
|
vec.push_back(&command_.front());
|
||||||
@ -103,7 +103,7 @@ std::vector<const char *> CmdLineParser::argv() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string CmdLineParser::cli_line() const{
|
std::string CmdParser::cli_line() const{
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << command_;
|
os << command_;
|
||||||
for (const auto & arg : arguments_)
|
for (const auto & arg : arguments_)
|
||||||
@ -111,7 +111,7 @@ std::string CmdLineParser::cli_line() const{
|
|||||||
return os.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmdLineParser::Reset(){
|
void CmdParser::Reset(){
|
||||||
multi_id_ = 0;
|
multi_id_ = 0;
|
||||||
detector_id_ = -1;
|
detector_id_ = -1;
|
||||||
help_ = false;
|
help_ = false;
|
@ -19,7 +19,7 @@ reason that the header file is not exposed.
|
|||||||
|
|
||||||
namespace sls {
|
namespace sls {
|
||||||
|
|
||||||
class CmdLineParser {
|
class CmdParser {
|
||||||
public:
|
public:
|
||||||
void Parse(int argc, const char *const argv[]);
|
void Parse(int argc, const char *const argv[]);
|
||||||
void Parse(const std::string &s);
|
void Parse(const std::string &s);
|
@ -1,5 +1,5 @@
|
|||||||
#include "Detector.h"
|
#include "Detector.h"
|
||||||
#include "CmdLineParser.h"
|
#include "CmdParser.h"
|
||||||
#include "CmdProxy.h"
|
#include "CmdProxy.h"
|
||||||
#include "container_utils.h"
|
#include "container_utils.h"
|
||||||
#include "detectorData.h"
|
#include "detectorData.h"
|
||||||
@ -58,7 +58,7 @@ void Detector::loadConfig(const std::string &fname) {
|
|||||||
|
|
||||||
void Detector::loadParameters(const std::string &fname) {
|
void Detector::loadParameters(const std::string &fname) {
|
||||||
CmdProxy proxy(this);
|
CmdProxy proxy(this);
|
||||||
CmdLineParser parser;
|
CmdParser parser;
|
||||||
std::ifstream input_file;
|
std::ifstream input_file;
|
||||||
input_file.open(fname.c_str(), std::ios_base::in);
|
input_file.open(fname.c_str(), std::ios_base::in);
|
||||||
if (!input_file.is_open()) {
|
if (!input_file.is_open()) {
|
||||||
|
@ -6,7 +6,7 @@ target_sources(tests PRIVATE
|
|||||||
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdProxy-eiger.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdProxy-eiger.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdProxy-jungfrau.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdProxy-jungfrau.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/test-Result.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/test-Result.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdLineParser.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdParser.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(tests PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src>")
|
target_include_directories(tests PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../src>")
|
@ -1,4 +1,4 @@
|
|||||||
#include "CmdLineParser.h"
|
#include "CmdParser.h"
|
||||||
#include "catch.hpp"
|
#include "catch.hpp"
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -9,11 +9,11 @@
|
|||||||
// command for all depreciated commands
|
// command for all depreciated commands
|
||||||
|
|
||||||
using vs = std::vector<std::string>;
|
using vs = std::vector<std::string>;
|
||||||
using sls::CmdLineParser;
|
using sls::CmdParser;
|
||||||
|
|
||||||
SCENARIO("Construction", "[support]") {
|
SCENARIO("Construction", "[support]") {
|
||||||
GIVEN("A default constructed CmdLineParser") {
|
GIVEN("A default constructed CmdParser") {
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
THEN("The state of the object is valid") {
|
THEN("The state of the object is valid") {
|
||||||
REQUIRE(p.detector_id() == -1);
|
REQUIRE(p.detector_id() == -1);
|
||||||
REQUIRE(p.multi_id() == 0);
|
REQUIRE(p.multi_id() == 0);
|
||||||
@ -26,8 +26,8 @@ SCENARIO("Construction", "[support]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SCENARIO("Parsing a string with the command line parser", "[support]") {
|
SCENARIO("Parsing a string with the command line parser", "[support]") {
|
||||||
GIVEN("A CmdLineParser") {
|
GIVEN("A CmdParser") {
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
WHEN("Parsing an empty string") {
|
WHEN("Parsing an empty string") {
|
||||||
std::string s;
|
std::string s;
|
||||||
p.Parse(s);
|
p.Parse(s);
|
||||||
@ -121,7 +121,7 @@ SCENARIO("Parsing a string with the command line parser", "[support]") {
|
|||||||
|
|
||||||
SCENARIO("Parsing strings with -h or --help", "[support]") {
|
SCENARIO("Parsing strings with -h or --help", "[support]") {
|
||||||
GIVEN("A parser") {
|
GIVEN("A parser") {
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
WHEN("Parsing a string with a command and help ") {
|
WHEN("Parsing a string with a command and help ") {
|
||||||
std::string s = "-h list";
|
std::string s = "-h list";
|
||||||
|
|
||||||
@ -159,7 +159,7 @@ SCENARIO("Parsing strings with -h or --help", "[support]") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parsing consecutive strings resets not found det id"){
|
TEST_CASE("Parsing consecutive strings resets not found det id"){
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse("1:exptime 0.5");
|
p.Parse("1:exptime 0.5");
|
||||||
REQUIRE(p.detector_id() == 1);
|
REQUIRE(p.detector_id() == 1);
|
||||||
p.Parse("exptime 0.5");
|
p.Parse("exptime 0.5");
|
||||||
@ -170,7 +170,7 @@ TEST_CASE("Parsing consecutive strings resets not found det id"){
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Parsing consecutive strings resets not found multi id"){
|
TEST_CASE("Parsing consecutive strings resets not found multi id"){
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse("1-1:exptime 0.5");
|
p.Parse("1-1:exptime 0.5");
|
||||||
REQUIRE(p.multi_id() == 1);
|
REQUIRE(p.multi_id() == 1);
|
||||||
p.Parse("1:exptime 0.5");
|
p.Parse("1:exptime 0.5");
|
||||||
@ -183,7 +183,7 @@ TEST_CASE("Parse with no arguments results in no command and default id",
|
|||||||
// first argument is the command used to call the binary
|
// first argument is the command used to call the binary
|
||||||
int argc = 1;
|
int argc = 1;
|
||||||
const char *const argv[]{"call"};
|
const char *const argv[]{"call"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse(argc, argv);
|
p.Parse(argc, argv);
|
||||||
|
|
||||||
REQUIRE(p.detector_id() == -1);
|
REQUIRE(p.detector_id() == -1);
|
||||||
@ -197,7 +197,7 @@ TEST_CASE(
|
|||||||
"[support]") {
|
"[support]") {
|
||||||
int argc = 2;
|
int argc = 2;
|
||||||
const char *const argv[]{"caller", "vrf"};
|
const char *const argv[]{"caller", "vrf"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse(argc, argv);
|
p.Parse(argc, argv);
|
||||||
|
|
||||||
REQUIRE(p.detector_id() == -1);
|
REQUIRE(p.detector_id() == -1);
|
||||||
@ -210,7 +210,7 @@ TEST_CASE("Parse a command with value but without client or detector id",
|
|||||||
"[support]") {
|
"[support]") {
|
||||||
int argc = 3;
|
int argc = 3;
|
||||||
const char *const argv[]{"caller", "vrf", "3000"};
|
const char *const argv[]{"caller", "vrf", "3000"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse(argc, argv);
|
p.Parse(argc, argv);
|
||||||
|
|
||||||
REQUIRE(p.detector_id() == -1);
|
REQUIRE(p.detector_id() == -1);
|
||||||
@ -224,7 +224,7 @@ TEST_CASE("Decodes position") {
|
|||||||
int argc = 2;
|
int argc = 2;
|
||||||
const char *const argv[]{"caller", "7:vrf"};
|
const char *const argv[]{"caller", "7:vrf"};
|
||||||
|
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse(argc, argv);
|
p.Parse(argc, argv);
|
||||||
|
|
||||||
REQUIRE(p.detector_id() == 7);
|
REQUIRE(p.detector_id() == 7);
|
||||||
@ -236,7 +236,7 @@ TEST_CASE("Decodes position") {
|
|||||||
TEST_CASE("Decodes double digit position", "[support]") {
|
TEST_CASE("Decodes double digit position", "[support]") {
|
||||||
int argc = 2;
|
int argc = 2;
|
||||||
const char *const argv[]{"caller", "73:vcmp"};
|
const char *const argv[]{"caller", "73:vcmp"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse(argc, argv);
|
p.Parse(argc, argv);
|
||||||
|
|
||||||
REQUIRE(p.detector_id() == 73);
|
REQUIRE(p.detector_id() == 73);
|
||||||
@ -248,7 +248,7 @@ TEST_CASE("Decodes double digit position", "[support]") {
|
|||||||
TEST_CASE("Decodes position and id", "[support]") {
|
TEST_CASE("Decodes position and id", "[support]") {
|
||||||
int argc = 2;
|
int argc = 2;
|
||||||
const char *const argv[]{"caller", "5-8:vrf"};
|
const char *const argv[]{"caller", "5-8:vrf"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse(argc, argv);
|
p.Parse(argc, argv);
|
||||||
|
|
||||||
REQUIRE(p.detector_id() == 8);
|
REQUIRE(p.detector_id() == 8);
|
||||||
@ -260,7 +260,7 @@ TEST_CASE("Decodes position and id", "[support]") {
|
|||||||
TEST_CASE("Double digit id", "[support]") {
|
TEST_CASE("Double digit id", "[support]") {
|
||||||
int argc = 2;
|
int argc = 2;
|
||||||
const char *const argv[]{"caller", "56-8:vrf"};
|
const char *const argv[]{"caller", "56-8:vrf"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
p.Parse(argc, argv);
|
p.Parse(argc, argv);
|
||||||
REQUIRE(p.detector_id() == 8);
|
REQUIRE(p.detector_id() == 8);
|
||||||
REQUIRE(p.multi_id() == 56);
|
REQUIRE(p.multi_id() == 56);
|
||||||
@ -271,19 +271,19 @@ TEST_CASE("Double digit id", "[support]") {
|
|||||||
TEST_CASE("Calling with wrong id throws invalid_argument", "[support]") {
|
TEST_CASE("Calling with wrong id throws invalid_argument", "[support]") {
|
||||||
int argc = 2;
|
int argc = 2;
|
||||||
const char *const argv[]{"caller", "asvldkn:vrf"};
|
const char *const argv[]{"caller", "asvldkn:vrf"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
CHECK_THROWS(p.Parse(argc, argv));
|
CHECK_THROWS(p.Parse(argc, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Calling with wrong client throws invalid_argument", "[support]") {
|
TEST_CASE("Calling with wrong client throws invalid_argument", "[support]") {
|
||||||
int argc = 2;
|
int argc = 2;
|
||||||
const char *const argv[]{"caller", "lki-3:vrf"};
|
const char *const argv[]{"caller", "lki-3:vrf"};
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
CHECK_THROWS(p.Parse(argc, argv));
|
CHECK_THROWS(p.Parse(argc, argv));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Build up argv", "[support]") {
|
TEST_CASE("Build up argv", "[support]") {
|
||||||
CmdLineParser p;
|
CmdParser p;
|
||||||
REQUIRE(p.argv().empty());
|
REQUIRE(p.argv().empty());
|
||||||
REQUIRE(p.argv().data() == nullptr);
|
REQUIRE(p.argv().data() == nullptr);
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user