diff options
Diffstat (limited to 'utils/command_line')
-rw-r--r-- | utils/command_line/CommandLineParser.h | 57 | ||||
-rw-r--r-- | utils/command_line/EnumListOption.h | 25 | ||||
-rw-r--r-- | utils/command_line/EnumOption.h | 17 | ||||
-rw-r--r-- | utils/command_line/ListOption.h | 13 | ||||
-rw-r--r-- | utils/command_line/Option.h | 9 | ||||
-rw-r--r-- | utils/command_line/SimpleOption.h | 7 | ||||
-rw-r--r-- | utils/command_line/ToggleOption.h | 12 |
7 files changed, 68 insertions, 72 deletions
diff --git a/utils/command_line/CommandLineParser.h b/utils/command_line/CommandLineParser.h index e8fabc4251..57796bce73 100644 --- a/utils/command_line/CommandLineParser.h +++ b/utils/command_line/CommandLineParser.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020 Arm Limited. + * Copyright (c) 2017-2020, 2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -24,13 +24,13 @@ #ifndef ARM_COMPUTE_UTILS_COMMANDLINEPARSER #define ARM_COMPUTE_UTILS_COMMANDLINEPARSER -#include "Option.h" #include "arm_compute/core/utils/misc/Utility.h" +#include "Option.h" +#include <cstring> #include <iostream> #include <map> #include <memory> -#include <memory> #include <regex> #include <string> #include <utility> @@ -55,7 +55,7 @@ public: * @return Pointer to the option. The option is owned by the parser. */ template <typename T, typename... As> - T *add_option(const std::string &name, As &&... args); + T *add_option(const std::string &name, As &&...args); /** Function to add a new positional argument to the parser. * @@ -64,7 +64,7 @@ public: * @return Pointer to the option. The option is owned by the parser. */ template <typename T, typename... As> - T *add_positional_option(As &&... args); + T *add_positional_option(As &&...args); /** Parses the command line arguments and updates the options accordingly. * @@ -100,14 +100,14 @@ private: }; template <typename T, typename... As> -inline T *CommandLineParser::add_option(const std::string &name, As &&... args) +inline T *CommandLineParser::add_option(const std::string &name, As &&...args) { auto result = _options.emplace(name, std::make_unique<T>(name, std::forward<As>(args)...)); return static_cast<T *>(result.first->second.get()); } template <typename T, typename... As> -inline T *CommandLineParser::add_positional_option(As &&... args) +inline T *CommandLineParser::add_positional_option(As &&...args) { _positional_options.emplace_back(std::make_unique<T>(std::forward<As>(args)...)); return static_cast<T *>(_positional_options.back().get()); @@ -115,11 +115,11 @@ inline T *CommandLineParser::add_positional_option(As &&... args) inline void CommandLineParser::parse(int argc, char **argv) { - const std::regex option_regex{ "--((?:no-)?)([^=]+)(?:=(.*))?" }; + const std::regex option_regex{"--((?:no-)?)([^=]+)(?:=(.*))?"}; - const auto set_option = [&](const std::string & option, const std::string & name, const std::string & value) + const auto set_option = [&](const std::string &option, const std::string &name, const std::string &value) { - if(_options.find(name) == _options.end()) + if (_options.find(name) == _options.end()) { _unknown_options.push_back(option); return; @@ -127,7 +127,7 @@ inline void CommandLineParser::parse(int argc, char **argv) const bool success = _options[name]->parse(value); - if(!success) + if (!success) { _invalid_options.push_back(option); } @@ -135,26 +135,27 @@ inline void CommandLineParser::parse(int argc, char **argv) unsigned int positional_index = 0; - for(int i = 1; i < argc; ++i) + for (int i = 1; i < argc; ++i) { - std::string mixed_case_opt{ argv[i] }; + std::string mixed_case_opt{argv[i]}; int equal_sign = mixed_case_opt.find('='); int pos = (equal_sign == -1) ? strlen(argv[i]) : equal_sign; - const std::string option = arm_compute::utility::tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos); - std::smatch option_matches; + const std::string option = + arm_compute::utility::tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos); + std::smatch option_matches; - if(std::regex_match(option, option_matches, option_regex)) + if (std::regex_match(option, option_matches, option_regex)) { // Boolean option - if(option_matches.str(3).empty()) + if (option_matches.str(3).empty()) { set_option(option, option_matches.str(2), option_matches.str(1).empty() ? "true" : "false"); } else { // Can't have "no-" and a value - if(!option_matches.str(1).empty()) + if (!option_matches.str(1).empty()) { _invalid_options.emplace_back(option); } @@ -166,7 +167,7 @@ inline void CommandLineParser::parse(int argc, char **argv) } else { - if(positional_index >= _positional_options.size()) + if (positional_index >= _positional_options.size()) { _invalid_options.push_back(mixed_case_opt); } @@ -183,30 +184,30 @@ inline bool CommandLineParser::validate() const { bool is_valid = true; - for(const auto &option : _options) + for (const auto &option : _options) { - if(option.second->is_required() && !option.second->is_set()) + if (option.second->is_required() && !option.second->is_set()) { is_valid = false; std::cerr << "ERROR: Option '" << option.second->name() << "' is required but not given!\n"; } } - for(const auto &option : _positional_options) + for (const auto &option : _positional_options) { - if(option->is_required() && !option->is_set()) + if (option->is_required() && !option->is_set()) { is_valid = false; std::cerr << "ERROR: Option '" << option->name() << "' is required but not given!\n"; } } - for(const auto &option : _unknown_options) + for (const auto &option : _unknown_options) { std::cerr << "WARNING: Skipping unknown option '" << option << "'!\n"; } - for(const auto &option : _invalid_options) + for (const auto &option : _invalid_options) { std::cerr << "WARNING: Skipping invalid option '" << option << "'!\n"; } @@ -218,19 +219,19 @@ inline void CommandLineParser::print_help(const std::string &program_name) const { std::cout << "usage: " << program_name << " \n"; - for(const auto &option : _options) + for (const auto &option : _options) { std::cout << option.second->help() << "\n"; } - for(const auto &option : _positional_options) + for (const auto &option : _positional_options) { std::string help_to_print; // Extract help sub-string const std::string help_str = option->help(); const size_t help_pos = help_str.find(" - "); - if(help_pos != std::string::npos) + if (help_pos != std::string::npos) { help_to_print = help_str.substr(help_pos); } diff --git a/utils/command_line/EnumListOption.h b/utils/command_line/EnumListOption.h index f4ee283528..6c4146fa75 100644 --- a/utils/command_line/EnumListOption.h +++ b/utils/command_line/EnumListOption.h @@ -25,7 +25,6 @@ #define ARM_COMPUTE_UTILS_ENUMLISTOPTION #include "Option.h" - #include <initializer_list> #include <set> #include <sstream> @@ -57,7 +56,7 @@ public: */ EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values); - bool parse(std::string value) override; + bool parse(std::string value) override; std::string help() const override; /** Get the values of the option. @@ -73,13 +72,17 @@ private: template <typename T> inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values) - : Option{ std::move(name) }, _allowed_values{ std::move(allowed_values) } + : Option{std::move(name)}, _allowed_values{std::move(allowed_values)} { } template <typename T> -inline EnumListOption<T>::EnumListOption(std::string name, std::set<T> allowed_values, std::initializer_list<T> &&default_values) - : Option{ std::move(name), false, true }, _values{ std::forward<std::initializer_list<T>>(default_values) }, _allowed_values{ std::move(allowed_values) } +inline EnumListOption<T>::EnumListOption(std::string name, + std::set<T> allowed_values, + std::initializer_list<T> &&default_values) + : Option{std::move(name), false, true}, + _values{std::forward<std::initializer_list<T>>(default_values)}, + _allowed_values{std::move(allowed_values)} { } @@ -90,10 +93,10 @@ bool EnumListOption<T>::parse(std::string value) _values.clear(); _is_set = true; - std::stringstream stream{ value }; + std::stringstream stream{value}; std::string item; - while(!std::getline(stream, item, ',').fail()) + while (!std::getline(stream, item, ',').fail()) { try { @@ -102,9 +105,9 @@ bool EnumListOption<T>::parse(std::string value) item_stream >> typed_value; - if(!item_stream.fail()) + if (!item_stream.fail()) { - if(_allowed_values.count(typed_value) == 0) + if (_allowed_values.count(typed_value) == 0) { _is_set = false; continue; @@ -115,7 +118,7 @@ bool EnumListOption<T>::parse(std::string value) _is_set = _is_set && !item_stream.fail(); } - catch(const std::invalid_argument &) + catch (const std::invalid_argument &) { _is_set = false; } @@ -130,7 +133,7 @@ std::string EnumListOption<T>::help() const std::stringstream msg; msg << "--" + name() + "={"; - for(const auto &value : _allowed_values) + for (const auto &value : _allowed_values) { msg << value << ","; } diff --git a/utils/command_line/EnumOption.h b/utils/command_line/EnumOption.h index 6bcfe5f14e..eb43b6c54e 100644 --- a/utils/command_line/EnumOption.h +++ b/utils/command_line/EnumOption.h @@ -25,7 +25,6 @@ #define ARM_COMPUTE_UTILS_ENUMOPTION #include "SimpleOption.h" - #include <set> #include <sstream> #include <stdexcept> @@ -55,7 +54,7 @@ public: */ EnumOption(std::string name, std::set<T> allowed_values, T default_value); - bool parse(std::string value) override; + bool parse(std::string value) override; std::string help() const override; /** Get the selected value. @@ -70,13 +69,13 @@ private: template <typename T> inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values) - : SimpleOption<T>{ std::move(name) }, _allowed_values{ std::move(allowed_values) } + : SimpleOption<T>{std::move(name)}, _allowed_values{std::move(allowed_values)} { } template <typename T> inline EnumOption<T>::EnumOption(std::string name, std::set<T> allowed_values, T default_value) - : SimpleOption<T>{ std::move(name), std::move(default_value) }, _allowed_values{ std::move(allowed_values) } + : SimpleOption<T>{std::move(name), std::move(default_value)}, _allowed_values{std::move(allowed_values)} { } @@ -85,14 +84,14 @@ bool EnumOption<T>::parse(std::string value) { try { - std::stringstream stream{ value }; + std::stringstream stream{value}; T typed_value{}; stream >> typed_value; - if(!stream.fail()) + if (!stream.fail()) { - if(_allowed_values.count(typed_value) == 0) + if (_allowed_values.count(typed_value) == 0) { return false; } @@ -104,7 +103,7 @@ bool EnumOption<T>::parse(std::string value) return false; } - catch(const std::invalid_argument &) + catch (const std::invalid_argument &) { return false; } @@ -116,7 +115,7 @@ std::string EnumOption<T>::help() const std::stringstream msg; msg << "--" + this->name() + "={"; - for(const auto &value : _allowed_values) + for (const auto &value : _allowed_values) { msg << value << ","; } diff --git a/utils/command_line/ListOption.h b/utils/command_line/ListOption.h index b290191e08..f318e1646a 100644 --- a/utils/command_line/ListOption.h +++ b/utils/command_line/ListOption.h @@ -25,7 +25,6 @@ #define ARM_COMPUTE_UTILS_LISTOPTION #include "Option.h" - #include <initializer_list> #include <sstream> #include <stdexcept> @@ -50,7 +49,7 @@ public: */ ListOption(std::string name, std::initializer_list<T> &&default_values); - bool parse(std::string value) override; + bool parse(std::string value) override; std::string help() const override; /** Get the list of option values. @@ -65,7 +64,7 @@ private: template <typename T> inline ListOption<T>::ListOption(std::string name, std::initializer_list<T> &&default_values) - : Option{ std::move(name), false, true }, _values{ std::forward<std::initializer_list<T>>(default_values) } + : Option{std::move(name), false, true}, _values{std::forward<std::initializer_list<T>>(default_values)} { } @@ -76,17 +75,17 @@ bool ListOption<T>::parse(std::string value) try { - std::stringstream stream{ value }; + std::stringstream stream{value}; std::string item; - while(!std::getline(stream, item, ',').fail()) + while (!std::getline(stream, item, ',').fail()) { std::stringstream item_stream(item); T typed_value{}; item_stream >> typed_value; - if(!item_stream.fail()) + if (!item_stream.fail()) { _values.emplace_back(typed_value); } @@ -96,7 +95,7 @@ bool ListOption<T>::parse(std::string value) return _is_set; } - catch(const std::invalid_argument &) + catch (const std::invalid_argument &) { return false; } diff --git a/utils/command_line/Option.h b/utils/command_line/Option.h index c845e5499f..b4288538b0 100644 --- a/utils/command_line/Option.h +++ b/utils/command_line/Option.h @@ -97,18 +97,17 @@ public: protected: std::string _name; - bool _is_required{ false }; - bool _is_set{ false }; + bool _is_required{false}; + bool _is_set{false}; std::string _help{}; }; -inline Option::Option(std::string name) - : _name{ std::move(name) } +inline Option::Option(std::string name) : _name{std::move(name)} { } inline Option::Option(std::string name, bool is_required, bool is_set) - : _name{ std::move(name) }, _is_required{ is_required }, _is_set{ is_set } + : _name{std::move(name)}, _is_required{is_required}, _is_set{is_set} { } diff --git a/utils/command_line/SimpleOption.h b/utils/command_line/SimpleOption.h index d76797375d..f6329c1790 100644 --- a/utils/command_line/SimpleOption.h +++ b/utils/command_line/SimpleOption.h @@ -25,7 +25,6 @@ #define ARM_COMPUTE_UTILS_SIMPLEOPTION #include "Option.h" - #include <sstream> #include <stdexcept> #include <string> @@ -74,7 +73,7 @@ protected: template <typename T> inline SimpleOption<T>::SimpleOption(std::string name, T default_value) - : Option{ std::move(name), false, true }, _value{ std::move(default_value) } + : Option{std::move(name), false, true}, _value{std::move(default_value)} { } @@ -83,12 +82,12 @@ bool SimpleOption<T>::parse(std::string value) { try { - std::stringstream stream{ std::move(value) }; + std::stringstream stream{std::move(value)}; stream >> _value; _is_set = !stream.fail(); return _is_set; } - catch(const std::invalid_argument &) + catch (const std::invalid_argument &) { return false; } diff --git a/utils/command_line/ToggleOption.h b/utils/command_line/ToggleOption.h index d3c68663b5..694b7bb9e6 100644 --- a/utils/command_line/ToggleOption.h +++ b/utils/command_line/ToggleOption.h @@ -25,7 +25,6 @@ #define ARM_COMPUTE_UTILS_TOGGLEOPTION #include "SimpleOption.h" - #include <string> namespace arm_compute @@ -45,26 +44,23 @@ public: */ ToggleOption(std::string name, bool default_value); - bool parse(std::string value) override; + bool parse(std::string value) override; std::string help() const override; }; inline ToggleOption::ToggleOption(std::string name, bool default_value) - : SimpleOption<bool> -{ - std::move(name), default_value -} + : SimpleOption<bool>{std::move(name), default_value} { } inline bool ToggleOption::parse(std::string value) { - if(value == "true") + if (value == "true") { _value = true; _is_set = true; } - else if(value == "false") + else if (value == "false") { _value = false; _is_set = true; |