Append the args of option to parsed data. Return true if there is any option called
| 412 | |
| 413 | // Append the args of option to parsed data. Return true if there is any option called |
| 414 | void |
| 415 | ArgParser::Command::append_option_data(Arguments &ret, AP_StrVec &args, int index) |
| 416 | { |
| 417 | std::map<std::string, unsigned> check_map; |
| 418 | for (unsigned i = index; i < args.size(); i++) { |
| 419 | // find matches of the arg |
| 420 | if (args[i][0] == '-' && args[i][1] == '-' && args[i].find('=') != std::string::npos) { |
| 421 | // deal with --args= |
| 422 | std::string option_name = args[i].substr(0, args[i].find_first_of('=')); |
| 423 | std::string value = args[i].substr(args[i].find_last_of('=') + 1); |
| 424 | if (value.empty()) { |
| 425 | help_message("missing argument for '" + option_name + "'"); |
| 426 | } |
| 427 | auto it = _option_list.find(option_name); |
| 428 | if (it != _option_list.end()) { |
| 429 | ArgParser::Option cur_option = it->second; |
| 430 | // handle environment variable |
| 431 | if (!cur_option.envvar.empty()) { |
| 432 | const char *const env = getenv(cur_option.envvar.c_str()); |
| 433 | ret.set_env(cur_option.key, nullptr != env ? env : ""); |
| 434 | } |
| 435 | ret.append_arg(cur_option.key, value); |
| 436 | check_map[cur_option.long_option] += 1; |
| 437 | args.erase(args.begin() + i); |
| 438 | i -= 1; |
| 439 | } |
| 440 | } else { |
| 441 | // output version message |
| 442 | if ((args[i] == "--version" || args[i] == "-V") && _option_list.find("--version") != _option_list.end()) { |
| 443 | version_message(); |
| 444 | } |
| 445 | // output help message |
| 446 | if ((args[i] == "--help" || args[i] == "-h") && _option_list.find("--help") != _option_list.end()) { |
| 447 | ArgParser::Command *command = this; |
| 448 | // find the correct level to output help message |
| 449 | for (unsigned i = 1; i < args.size(); i++) { |
| 450 | auto it = command->_subcommand_list.find(args[i]); |
| 451 | if (it == command->_subcommand_list.end()) { |
| 452 | break; |
| 453 | } |
| 454 | command = &it->second; |
| 455 | } |
| 456 | usage_return_code = 0; |
| 457 | command->help_message(); |
| 458 | } |
| 459 | // deal with normal --arg val1 val2 ... |
| 460 | auto long_it = _option_list.find(args[i]); |
| 461 | auto short_it = _option_map.find(args[i]); |
| 462 | // long option match or short option match |
| 463 | if (long_it != _option_list.end() || short_it != _option_map.end()) { |
| 464 | ArgParser::Option cur_option; |
| 465 | if (long_it != _option_list.end()) { |
| 466 | cur_option = long_it->second; |
| 467 | } else { |
| 468 | cur_option = _option_list.at(short_it->second); |
| 469 | } |
| 470 | // handle the arguments |
| 471 | std::string err = handle_args(ret, args, cur_option.key, cur_option.arg_num, i); |
nothing calls this directly
no test coverage detected