change conversion from string to arithmetic

This commit is contained in:
ackimixs
2024-05-20 22:06:48 +02:00
parent 4a196a34ce
commit 7fa4fabc86
4 changed files with 62 additions and 13 deletions

View File

@@ -2,7 +2,9 @@
#include <map>
#include <optional>
#include <sstream>
#include <string>
#include <iostream>
class CLParser {
@@ -18,29 +20,38 @@ public:
[[nodiscard]] std::string getOption(const std::string& option, const std::string& defaultValue) const;
template <typename T>
[[nodiscard]] typename std::enable_if<std::is_arithmetic<T>::value, T>::type getOption(const std::string& option, T defaultValue) const {
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T> getOption(const std::string& option, T defaultValue) const {
if (!hasOption(option)) {
return defaultValue;
}
try {
return static_cast<T>(std::stod(_options.at(option)));
} catch (std::exception& e) {
T value;
std::istringstream iss(_options.at(option));
iss >> value;
if (iss.fail() || !iss.eof()) {
std::cout << "Failed convert" << std::endl;
return defaultValue;
}
return value;
}
template <typename T>
[[nodiscard]] std::optional<typename std::enable_if<std::is_arithmetic<T>::value, T>::type> getOption(const std::string& option) const {
[[nodiscard]] std::optional<std::enable_if_t<std::is_arithmetic_v<T>, T>> getOption(const std::string& option) const {
if (!hasOption(option)) {
return std::nullopt;
}
try {
return static_cast<T>(std::stod(_options.at(option)));
} catch (std::exception& e) {
T value;
std::istringstream iss(_options.at(option));
iss >> value;
if (iss.fail() || !iss.eof()) {
return std::nullopt;
}
return value;
}
[[nodiscard]] bool hasPositionalArgument(int index) const;
@@ -48,16 +59,20 @@ public:
[[nodiscard]] std::string getPositionalArgument(int index) const;
template <typename T>
[[nodiscard]] typename std::enable_if<std::is_arithmetic<T>::value, T>::type getPositionalArgument(int index) const {
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T> getPositionalArgument(int index) const {
if (!hasPositionalArgument(index)) {
return T();
}
try {
return static_cast<T>(std::stod(_argv[index]));
} catch (std::exception& e) {
T value;
std::istringstream iss(_argv[index]);
iss >> value;
if (iss.fail() || !iss.eof()) {
return T();
}
return value;
}
[[nodiscard]] int positionalArgumentsCount() const;

View File

@@ -10,11 +10,19 @@ find_package(Modelec COMPONENTS
REQUIRED)
# Define the executable
add_executable(client main.cpp)
add_executable(client client.example.cpp)
add_executable(parser parser.example.cpp)
# Link the shared library to the executable
target_link_libraries(client
Modelec::CLParser
Modelec::TCPClient
Modelec::Utils
)
target_link_libraries(parser
Modelec::CLParser
Modelec::TCPClient
Modelec::Utils
)

View File

@@ -0,0 +1,26 @@
#include <Modelec/CLParser.h>
#include <iostream>
struct B {
std::string a;
};
int main(int argc, char* argv[]) {
CLParser parser(argc, argv);
int port = parser.getOption("port", 12);
std::cout << "Port : " << port << std::endl;
auto test = parser.getOption<unsigned long long>("long");
if (test.has_value()) {
std::cout << test.value() << std::endl;
}
else {
std::cout << "missing long argument" << std::endl;
}
return 0;
}