| 114 | } |
| 115 | |
| 116 | inline void CommandLineParser::parse(int argc, char **argv) |
| 117 | { |
| 118 | const std::regex option_regex{"--((?:no-)?)([^=]+)(?:=(.*))?"}; |
| 119 | |
| 120 | const auto set_option = [&](const std::string &option, const std::string &name, const std::string &value) |
| 121 | { |
| 122 | if (_options.find(name) == _options.end()) |
| 123 | { |
| 124 | _unknown_options.push_back(option); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | const bool success = _options[name]->parse(value); |
| 129 | |
| 130 | if (!success) |
| 131 | { |
| 132 | _invalid_options.push_back(option); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | unsigned int positional_index = 0; |
| 137 | |
| 138 | for (int i = 1; i < argc; ++i) |
| 139 | { |
| 140 | std::string mixed_case_opt{argv[i]}; |
| 141 | int equal_sign = mixed_case_opt.find('='); |
| 142 | int pos = (equal_sign == -1) ? strlen(argv[i]) : equal_sign; |
| 143 | |
| 144 | const std::string option = |
| 145 | arm_compute::utility::tolower(mixed_case_opt.substr(0, pos)) + mixed_case_opt.substr(pos); |
| 146 | std::smatch option_matches; |
| 147 | |
| 148 | if (std::regex_match(option, option_matches, option_regex)) |
| 149 | { |
| 150 | // Boolean option |
| 151 | if (option_matches.str(3).empty()) |
| 152 | { |
| 153 | set_option(option, option_matches.str(2), option_matches.str(1).empty() ? "true" : "false"); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | // Can't have "no-" and a value |
| 158 | if (!option_matches.str(1).empty()) |
| 159 | { |
| 160 | _invalid_options.emplace_back(option); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | set_option(option, option_matches.str(2), option_matches.str(3)); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | if (positional_index >= _positional_options.size()) |
| 171 | { |
| 172 | _invalid_options.push_back(mixed_case_opt); |
| 173 | } |