diff --git a/src/common/app_parameters.cpp b/src/common/app_parameters.cpp index ea77955..83a392b 100644 --- a/src/common/app_parameters.cpp +++ b/src/common/app_parameters.cpp @@ -89,6 +89,9 @@ app_parameters::app_parameters(int in_argc, const char** in_argv) { // Flush any pending switch/value flush_value(); + + // Populate m_switches_set from m_switches + m_switches_set = std::unordered_set{ m_switches.begin(), m_switches.end() }; } std::string_view app_parameters::path() const { @@ -103,8 +106,8 @@ const std::vector& app_parameters::switches() const { return m_switches; } -std::unordered_set app_parameters::switches_set() const { - return { m_switches.begin(), m_switches.end() }; +const std::unordered_set& app_parameters::switches_set() const { + return m_switches_set; } const std::unordered_map& app_parameters::values() const { @@ -133,4 +136,19 @@ object app_parameters::as_object() const { }; } +bool app_parameters::hasSwitch(std::string_view in_switch) const { + return m_switches_set.find(in_switch) != m_switches_set.end(); +} + +std::string_view app_parameters::getValue(std::string_view in_key) const { + auto result = m_values.find(in_key); + + // Safety check + if (result == m_values.end()) { + return {}; + } + + return result->second; +} + } // namespace jessilib diff --git a/src/include/app_parameters.hpp b/src/include/app_parameters.hpp index f1925f0..21c9ebc 100644 --- a/src/include/app_parameters.hpp +++ b/src/include/app_parameters.hpp @@ -28,16 +28,20 @@ public: std::string_view path() const; const std::vector& arguments() const; const std::vector& switches() const; - std::unordered_set switches_set() const; + const std::unordered_set& switches_set() const; const std::unordered_map& values() const; jessilib::object as_object() const; + bool hasSwitch(std::string_view in_switch) const; + std::string_view getValue(std::string_view in_key) const; + operator jessilib::object() const { return as_object(); } private: std::string_view m_path; std::vector m_args; std::vector m_switches; + std::unordered_set m_switches_set; std::unordered_map m_values; }; diff --git a/src/test/app_parameters.cpp b/src/test/app_parameters.cpp index 70deb61..383bb9b 100644 --- a/src/test/app_parameters.cpp +++ b/src/test/app_parameters.cpp @@ -274,4 +274,8 @@ TEST(AppParametersTest, switch_and_value) { EXPECT_EQ(obj["Args"], expected_args); EXPECT_EQ(obj["Switches"], expected_switches); EXPECT_EQ(obj["Values"], expected_values); + + EXPECT_TRUE(parameters.hasSwitch("switch")); + EXPECT_FALSE(parameters.hasSwitch("switch2")); + EXPECT_EQ(parameters.getValue("key"), "value"); }