diff --git a/components/CLParser/include/Modelec/CLParser.h b/components/CLParser/include/Modelec/CLParser.h index 7e8c8b9..b1be5e1 100644 --- a/components/CLParser/include/Modelec/CLParser.h +++ b/components/CLParser/include/Modelec/CLParser.h @@ -2,7 +2,9 @@ #include #include +#include #include +#include class CLParser { @@ -18,29 +20,38 @@ public: [[nodiscard]] std::string getOption(const std::string& option, const std::string& defaultValue) const; template - [[nodiscard]] typename std::enable_if::value, T>::type getOption(const std::string& option, T defaultValue) const { + [[nodiscard]] std::enable_if_t, T> getOption(const std::string& option, T defaultValue) const { if (!hasOption(option)) { return defaultValue; } - try { - return static_cast(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 - [[nodiscard]] std::optional::value, T>::type> getOption(const std::string& option) const { + [[nodiscard]] std::optional, T>> getOption(const std::string& option) const { if (!hasOption(option)) { return std::nullopt; } - try { - return static_cast(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 - [[nodiscard]] typename std::enable_if::value, T>::type getPositionalArgument(int index) const { + [[nodiscard]] std::enable_if_t, T> getPositionalArgument(int index) const { if (!hasPositionalArgument(index)) { return T(); } - try { - return static_cast(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; diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 084055c..f84d512 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -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 ) \ No newline at end of file diff --git a/example/main.cpp b/example/client.example.cpp similarity index 100% rename from example/main.cpp rename to example/client.example.cpp diff --git a/example/parser.example.cpp b/example/parser.example.cpp new file mode 100644 index 0000000..c663289 --- /dev/null +++ b/example/parser.example.cpp @@ -0,0 +1,26 @@ +#include +#include + +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("long"); + + if (test.has_value()) { + std::cout << test.value() << std::endl; + } + else { + std::cout << "missing long argument" << std::endl; + } + + return 0; +} \ No newline at end of file