* Parses all the command line options provided by the user and stores in a map. */
| 32 | * Parses all the command line options provided by the user and stores in a map. |
| 33 | */ |
| 34 | int ParseOptions(std::map<std::string, std::string>& options, std::map<std::string, std::string>& acceptedOptions, |
| 35 | char *argv[], int argc) |
| 36 | { |
| 37 | for (int i = 1; i < argc; ++i) |
| 38 | { |
| 39 | std::string currentOption = std::string(argv[i]); |
| 40 | auto it = acceptedOptions.find(currentOption); |
| 41 | if (it != acceptedOptions.end()) |
| 42 | { |
| 43 | if (i + 1 < argc && std::string(argv[i + 1]).rfind("--", 0) != 0) |
| 44 | { |
| 45 | std::string value = argv[++i]; |
| 46 | options.insert({it->first, value}); |
| 47 | } |
| 48 | else if (std::string(argv[i]) == "HELP") |
| 49 | { |
| 50 | std::cout << "Available options" << std::endl; |
| 51 | for (auto & acceptedOption : acceptedOptions) |
| 52 | { |
| 53 | std::cout << acceptedOption.first << " : " << acceptedOption.second << std::endl; |
| 54 | } |
| 55 | return 2; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | std::cerr << std::string(argv[i]) << " option requires one argument." << std::endl; |
| 60 | return 1; |
| 61 | } |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | std::cerr << "Unrecognised option: " << std::string(argv[i]) << std::endl; |
| 66 | return 1; |
| 67 | } |
| 68 | } |
| 69 | return 0; |
| 70 | } |
no test coverage detected