| 118 | } |
| 119 | |
| 120 | Arguments Arguments::parse(int argc, char* argv[]) { |
| 121 | Arguments args; |
| 122 | for (int argIdx = 1; argIdx < argc; ++argIdx) { |
| 123 | std::string key{argv[argIdx]}; |
| 124 | if (getFlag(key)) { |
| 125 | args.set(key); |
| 126 | continue; |
| 127 | } |
| 128 | if (!getArg(key)) { |
| 129 | throw CommandException("Unrecognized option: \"" + key + "\""); |
| 130 | } |
| 131 | if (argIdx == argc - 1) { |
| 132 | throw CommandException("No value specified for key \"" + key + "\""); |
| 133 | } |
| 134 | ++argIdx; |
| 135 | std::string value{argv[argIdx]}; |
| 136 | args.set(key, value); |
| 137 | } |
| 138 | if (args.isSet("-h")) { |
| 139 | std::cout << getHelp(); |
| 140 | std::exit(0); |
| 141 | } |
| 142 | for (const auto& arg : registered_args_) { |
| 143 | if (arg.required && !args.get(arg)) { |
| 144 | throw CommandException("Missing required option " + utils::StringUtils::join("|", arg.names)); |
| 145 | } |
| 146 | } |
| 147 | return args; |
| 148 | } |
| 149 | |
| 150 | utils::optional<Flag> Arguments::getFlag(const std::string &name) { |
| 151 | for (const auto& flag : registered_flags_) { |
nothing calls this directly
no test coverage detected