diff --git a/include/ppx/command_line_parser.h b/include/ppx/command_line_parser.h index e8e571a6e..c3097a164 100644 --- a/include/ppx/command_line_parser.h +++ b/include/ppx/command_line_parser.h @@ -46,7 +46,7 @@ class CliOptions public: CliOptions() = default; - bool HasExtraOption(std::string_view option) const { return mAllOptions.contains(option); } + bool HasExtraOption(const std::string& option) const { return mAllOptions.contains(option); } // Returns the number of unique options and flags that were specified on the commandline, // not counting multiple appearances of the same flag such as: --assets-path a --assets-path b @@ -57,7 +57,7 @@ class CliOptions // Warning: If this is called instead of the vector overload for multiple-value flags, // only the last value will be returned. template - T GetOptionValueOrDefault(std::string_view optionName, const T& defaultValue) const + T GetOptionValueOrDefault(const std::string& optionName, const T& defaultValue) const { auto it = mAllOptions.find(optionName); if (it == mAllOptions.cend()) { @@ -70,7 +70,7 @@ class CliOptions // Same as above, but intended for list flags that are specified on the command line // with multiple instances of the same flag template - std::vector GetOptionValueOrDefault(std::string_view optionName, const std::vector& defaultValues) const + std::vector GetOptionValueOrDefault(const std::string& optionName, const std::vector& defaultValues) const { auto it = mAllOptions.find(optionName); if (it == mAllOptions.cend()) { @@ -86,14 +86,14 @@ class CliOptions // Same as above, but intended for resolution flags that are specified on command line // with x - std::pair GetOptionValueOrDefault(std::string_view optionName, const std::pair& defaultValue) const; + std::pair GetOptionValueOrDefault(const std::string& optionName, const std::pair& defaultValue) const; // (WILL BE DEPRECATED, USE KNOBS INSTEAD) // Get the parameter value after converting it into the desired integral, // floating-point, or boolean type. If the value fails to be converted, // return the specified default value. template - T GetExtraOptionValueOrDefault(std::string_view optionName, const T& defaultValue) const + T GetExtraOptionValueOrDefault(const std::string& optionName, const T& defaultValue) const { static_assert(std::is_integral_v || std::is_floating_point_v || std::is_same_v, "GetExtraOptionValueOrDefault must be called with an integral, floating-point, boolean, or std::string type"); @@ -103,7 +103,7 @@ class CliOptions private: // Adds new option if the option does not already exist // Otherwise, the new value is appended to the end of the vector of stored parameters for this option - void AddOption(std::string_view optionName, std::string_view valueStr); + void AddOption(std::string_view optionName, std::string_view value); // Same as above, but appends an array of values at the same key void AddOption(std::string_view optionName, const std::vector& valueArray); @@ -136,18 +136,8 @@ class CliOptions } private: - struct string_hash - { - using hash_type = std::hash; - using is_transparent = void; - - size_t operator()(const char* str) const { return hash_type{}(str); } - size_t operator()(std::string_view str) const { return hash_type{}(str); } - size_t operator()(std::string const& str) const { return hash_type{}(str); } - }; - // All flag names (string) and parameters (vector of strings) specified on the command line - std::unordered_map, string_hash, std::equal_to<>> mAllOptions; + std::unordered_map> mAllOptions; friend class CommandLineParser; }; diff --git a/src/ppx/command_line_parser.cpp b/src/ppx/command_line_parser.cpp index dc8adb202..8e68ed039 100644 --- a/src/ppx/command_line_parser.cpp +++ b/src/ppx/command_line_parser.cpp @@ -38,7 +38,7 @@ bool StartsWithDoubleDash(std::string_view s) namespace ppx { -std::pair CliOptions::GetOptionValueOrDefault(std::string_view optionName, const std::pair& defaultValue) const +std::pair CliOptions::GetOptionValueOrDefault(const std::string& optionName, const std::pair& defaultValue) const { auto it = mAllOptions.find(optionName); if (it == mAllOptions.cend()) { @@ -55,22 +55,25 @@ std::pair CliOptions::GetOptionValueOrDefault(std::string_view optionN return std::make_pair(N, M); } -void CliOptions::AddOption(std::string_view optionName, std::string_view valueStr) +void CliOptions::AddOption(std::string_view optionName, std::string_view value) { - auto it = mAllOptions.find(optionName); + std::string optionNameStr{optionName}; + std::string valueStr{value}; + auto it = mAllOptions.find(optionNameStr); if (it == mAllOptions.cend()) { - std::vector v{std::string(valueStr)}; - mAllOptions.emplace(std::string(optionName), v); + std::vector v{valueStr}; + mAllOptions.emplace(optionName, v); return; } - it->second.push_back(std::string(valueStr)); + it->second.push_back(valueStr); } void CliOptions::AddOption(std::string_view optionName, const std::vector& valueArray) { - auto it = mAllOptions.find(optionName); + std::string optionNameStr{optionName}; + auto it = mAllOptions.find(optionNameStr); if (it == mAllOptions.cend()) { - mAllOptions.emplace(std::string(optionName), valueArray); + mAllOptions.emplace(optionNameStr, valueArray); return; } auto storedValueArray = it->second;