diff options
author | Gereon Kremer <nafur42@gmail.com> | 2021-09-18 02:13:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-18 00:13:16 +0000 |
commit | 57de807335402741c6371f66cbdd3d3f47863341 (patch) | |
tree | b22b4c896428b82a8c5754d752c59f454e61d1c4 /src/options/options_handler.cpp | |
parent | 4209fb71c97c06833ef320ad9c73590546c16fa2 (diff) |
Refactor tag suggestion mechanism (#7199)
This refactors the internal tag suggestion mechanism to no longer rely on C-style char arrays, but use a C++ vector of strings instead.
Diffstat (limited to 'src/options/options_handler.cpp')
-rw-r--r-- | src/options/options_handler.cpp | 51 |
1 files changed, 18 insertions, 33 deletions
diff --git a/src/options/options_handler.cpp b/src/options/options_handler.cpp index 1b6cff519..bf2def05b 100644 --- a/src/options/options_handler.cpp +++ b/src/options/options_handler.cpp @@ -372,12 +372,12 @@ void OptionsHandler::showConfiguration(const std::string& option, exit(0); } -static void printTags(unsigned ntags, char const* const* tags) +static void printTags(const std::vector<std::string>& tags) { std::cout << "available tags:"; - for (unsigned i = 0; i < ntags; ++ i) + for (const auto& t : tags) { - std::cout << " " << tags[i] << std::endl; + std::cout << " " << t << std::endl; } std::cout << std::endl; } @@ -393,8 +393,8 @@ void OptionsHandler::showDebugTags(const std::string& option, { throw OptionException("debug tags not available in non-tracing builds"); } - printTags(Configuration::getNumDebugTags(),Configuration::getDebugTags()); - exit(0); + printTags(Configuration::getDebugTags()); + std::exit(0); } void OptionsHandler::showTraceTags(const std::string& option, @@ -404,29 +404,17 @@ void OptionsHandler::showTraceTags(const std::string& option, { throw OptionException("trace tags not available in non-tracing build"); } - printTags(Configuration::getNumTraceTags(), Configuration::getTraceTags()); - exit(0); + printTags(Configuration::getTraceTags()); + std::exit(0); } -static std::string suggestTags(char const* const* validTags, +static std::string suggestTags(const std::vector<std::string>& validTags, std::string inputTag, - char const* const* additionalTags) + const std::vector<std::string>& additionalTags) { DidYouMean didYouMean; - - const char* opt; - for (size_t i = 0; (opt = validTags[i]) != nullptr; ++i) - { - didYouMean.addWord(validTags[i]); - } - if (additionalTags != nullptr) - { - for (size_t i = 0; (opt = additionalTags[i]) != nullptr; ++i) - { - didYouMean.addWord(additionalTags[i]); - } - } - + didYouMean.addWords(validTags); + didYouMean.addWords(additionalTags); return didYouMean.getMatchAsString(inputTag); } @@ -438,18 +426,17 @@ void OptionsHandler::enableTraceTag(const std::string& option, { throw OptionException("trace tags not available in non-tracing builds"); } - else if(!Configuration::isTraceTag(optarg.c_str())) + else if (!Configuration::isTraceTag(optarg)) { if (optarg == "help") { - printTags( - Configuration::getNumTraceTags(), Configuration::getTraceTags()); - exit(0); + printTags(Configuration::getTraceTags()); + std::exit(0); } throw OptionException( std::string("trace tag ") + optarg + std::string(" not available.") - + suggestTags(Configuration::getTraceTags(), optarg, nullptr)); + + suggestTags(Configuration::getTraceTags(), optarg, {})); } Trace.on(optarg); } @@ -467,14 +454,12 @@ void OptionsHandler::enableDebugTag(const std::string& option, throw OptionException("debug tags not available in non-tracing builds"); } - if (!Configuration::isDebugTag(optarg.c_str()) - && !Configuration::isTraceTag(optarg.c_str())) + if (!Configuration::isDebugTag(optarg) && !Configuration::isTraceTag(optarg)) { if (optarg == "help") { - printTags( - Configuration::getNumDebugTags(), Configuration::getDebugTags()); - exit(0); + printTags(Configuration::getDebugTags()); + std::exit(0); } throw OptionException(std::string("debug tag ") + optarg |