37 const std::regex option_regex{
"--((?:no-)?)([^=]+)(?:=(.*))?" };
39 const auto set_option = [&](
const std::string & option,
const std::string & name,
const std::string & value)
41 if(_options.find(name) == _options.end())
43 _unknown_options.push_back(option);
47 const bool success = _options[name]->parse(value);
51 _invalid_options.push_back(option);
55 unsigned int positional_index = 0;
57 for(
int i = 1; i < argc; ++i)
59 std::string mixed_case_opt{ argv[i] };
60 int equal_sign = mixed_case_opt.find(
'=');
61 int pos = (equal_sign == -1) ? strlen(argv[i]) : equal_sign;
63 const std::string option =
tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos);
64 std::smatch option_matches;
66 if(std::regex_match(option, option_matches, option_regex))
69 if(option_matches.str(3).empty())
71 set_option(option, option_matches.str(2), option_matches.str(1).empty() ?
"true" :
"false");
76 if(!option_matches.str(1).empty())
78 _invalid_options.emplace_back(option);
82 set_option(option, option_matches.str(2), option_matches.str(3));
88 if(positional_index >= _positional_options.size())
90 _invalid_options.push_back(mixed_case_opt);
94 _positional_options[positional_index]->parse(mixed_case_opt);
103 bool is_valid =
true;
105 for(
const auto &option : _options)
107 if(option.second->is_required() && !option.second->is_set())
110 std::cerr <<
"ERROR: Option '" << option.second->name() <<
"' is required but not given!\n";
114 for(
const auto &option : _positional_options)
116 if(option->is_required() && !option->is_set())
119 std::cerr <<
"ERROR: Option '" << option->name() <<
"' is required but not given!\n";
123 for(
const auto &option : _unknown_options)
125 std::cerr <<
"WARNING: Skipping unknown option '" << option <<
"'!\n";
128 for(
const auto &option : _invalid_options)
130 std::cerr <<
"WARNING: Skipping invalid option '" << option <<
"'!\n";
138 std::cout <<
"usage: " << program_name <<
" \n";
140 for(
const auto &option : _options)
142 std::cout << option.second->help() <<
"\n";
145 for(
const auto &option : _positional_options)
147 std::cout << option->name() <<
"\n";
std::string tolower(std::string string)
Convert string to lower case.
void parse(int argc, char **argv)
Parses the command line arguments and updates the options accordingly.
This file contains all available output stages for GEMMLowp on OpenCL.
bool validate() const
Validates the previously parsed command line arguments.
void print_help(const std::string &program_name) const
Prints a help message for all configured options.