Browse Source

Added `hasSwitch()` and `getValue()` methods to `app_parameters`

master
Jessica James 5 years ago
parent
commit
5f25583c23
  1. 22
      src/common/app_parameters.cpp
  2. 6
      src/include/app_parameters.hpp
  3. 4
      src/test/app_parameters.cpp

22
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 any pending switch/value
flush_value(); flush_value();
// Populate m_switches_set from m_switches
m_switches_set = std::unordered_set<std::string_view>{ m_switches.begin(), m_switches.end() };
} }
std::string_view app_parameters::path() const { std::string_view app_parameters::path() const {
@ -103,8 +106,8 @@ const std::vector<std::string_view>& app_parameters::switches() const {
return m_switches; return m_switches;
} }
std::unordered_set<std::string_view> app_parameters::switches_set() const { const std::unordered_set<std::string_view>& app_parameters::switches_set() const {
return { m_switches.begin(), m_switches.end() }; return m_switches_set;
} }
const std::unordered_map<std::string_view, std::string>& app_parameters::values() const { const std::unordered_map<std::string_view, std::string>& 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 } // namespace jessilib

6
src/include/app_parameters.hpp

@ -28,16 +28,20 @@ public:
std::string_view path() const; std::string_view path() const;
const std::vector<std::string_view>& arguments() const; const std::vector<std::string_view>& arguments() const;
const std::vector<std::string_view>& switches() const; const std::vector<std::string_view>& switches() const;
std::unordered_set<std::string_view> switches_set() const; const std::unordered_set<std::string_view>& switches_set() const;
const std::unordered_map<std::string_view, std::string>& values() const; const std::unordered_map<std::string_view, std::string>& values() const;
jessilib::object as_object() 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(); } operator jessilib::object() const { return as_object(); }
private: private:
std::string_view m_path; std::string_view m_path;
std::vector<std::string_view> m_args; std::vector<std::string_view> m_args;
std::vector<std::string_view> m_switches; std::vector<std::string_view> m_switches;
std::unordered_set<std::string_view> m_switches_set;
std::unordered_map<std::string_view, std::string> m_values; std::unordered_map<std::string_view, std::string> m_values;
}; };

4
src/test/app_parameters.cpp

@ -274,4 +274,8 @@ TEST(AppParametersTest, switch_and_value) {
EXPECT_EQ(obj["Args"], expected_args); EXPECT_EQ(obj["Args"], expected_args);
EXPECT_EQ(obj["Switches"], expected_switches); EXPECT_EQ(obj["Switches"], expected_switches);
EXPECT_EQ(obj["Values"], expected_values); EXPECT_EQ(obj["Values"], expected_values);
EXPECT_TRUE(parameters.hasSwitch("switch"));
EXPECT_FALSE(parameters.hasSwitch("switch2"));
EXPECT_EQ(parameters.getValue("key"), "value");
} }

Loading…
Cancel
Save