ChimeraTK-DeviceAccess  03.18.00
argumentParser.cc
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #include "argumentParser.h"
5 
6 #include <string>
7 
8 static const unsigned int DEFAULT_SERVER_PORT = 5001;
9 static const std::string DEFAULT_MAP_FILE = "./testFile.map";
10 static const unsigned int DEFAULT_PROTOCOL_VERSION = 0x0;
11 
12 /**********************************************************************************************************************/
13 struct Argument {
14  std::string _shortName;
15  std::string _longName;
16 
17  Argument(std::string shortName, std::string longName) : _shortName(shortName), _longName(longName) {}
18 };
19 std::string getArgumentValue(Argument& argument, char** argumentArray);
20 
21 /**********************************************************************************************************************/
22 
23 unsigned int getPortNumber(char** argumentArray) {
24  Argument portFlag("-p", "--port");
25  std::string portNumber = getArgumentValue(portFlag, argumentArray);
26 
27  try {
28  return std::stoul(portNumber);
29  }
30  catch(...) {
31  return DEFAULT_SERVER_PORT;
32  }
33 }
34 std::string getMapFileLocation(char** argumentArray) {
35  Argument mapFileLocationFlag("-m", "--mapfile");
36  std::string mapFileLocation = getArgumentValue(mapFileLocationFlag, argumentArray);
37 
38  if(mapFileLocation.empty()) {
39  return DEFAULT_MAP_FILE;
40  }
41  else {
42  return mapFileLocation;
43  }
44 }
45 
46 unsigned int getProtocolVersion(char** argumentArray) {
47  Argument protocolVersionFlag("-V", "--protocol-version");
48  std::string protocolVersion = getArgumentValue(protocolVersionFlag, argumentArray);
49 
50  try {
51  return std::stoul(protocolVersion);
52  }
53  catch(...) {
54  return DEFAULT_PROTOCOL_VERSION;
55  }
56 }
57 
58 std::string getArgumentValue(Argument& argument, char** argumentArray) {
59  if(*argumentArray == nullptr || *(argumentArray + 1) == nullptr) {
60  return std::string();
61  }
62  else if(argument._shortName == *argumentArray || argument._longName == *argumentArray) {
63  return std::string(*(argumentArray + 1));
64  }
65  return getArgumentValue(argument, argumentArray + 1);
66 }
getMapFileLocation
std::string getMapFileLocation(char **argumentArray)
Definition: argumentParser.cc:34
Argument::Argument
Argument(std::string shortName, std::string longName)
Definition: argumentParser.cc:17
getArgumentValue
std::string getArgumentValue(Argument &argument, char **argumentArray)
Definition: argumentParser.cc:58
argumentParser.h
getProtocolVersion
unsigned int getProtocolVersion(char **argumentArray)
Definition: argumentParser.cc:46
Argument
Definition: argumentParser.cc:13
getPortNumber
unsigned int getPortNumber(char **argumentArray)
Definition: argumentParser.cc:23
Argument::_shortName
std::string _shortName
Definition: argumentParser.cc:14
Argument::_longName
std::string _longName
Definition: argumentParser.cc:15