| 425 | // |
| 426 | |
| 427 | static bool common_params_parse_ex(int argc, char ** argv, common_params_context & ctx_arg) { |
| 428 | common_params & params = ctx_arg.params; |
| 429 | |
| 430 | // setup log directly from params.verbosity: see tools/cli/cli.cpp |
| 431 | common_log_set_verbosity_thold(params.verbosity); |
| 432 | |
| 433 | std::unordered_map<std::string, std::pair<common_arg *, bool>> arg_to_options; |
| 434 | for (auto & opt : ctx_arg.options) { |
| 435 | for (const auto & arg : opt.args) { |
| 436 | arg_to_options[arg] = {&opt, /* is_positive */ true}; |
| 437 | } |
| 438 | for (const auto & arg : opt.args_neg) { |
| 439 | arg_to_options[arg] = {&opt, /* is_positive */ false}; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | // handle environment variables |
| 444 | for (auto & opt : ctx_arg.options) { |
| 445 | std::string value; |
| 446 | if (opt.get_value_from_env(value)) { |
| 447 | try { |
| 448 | if (opt.handler_void && is_truthy(value)) { |
| 449 | opt.handler_void(params); |
| 450 | } |
| 451 | if (opt.handler_int) { |
| 452 | opt.handler_int(params, std::stoi(value)); |
| 453 | } |
| 454 | if (opt.handler_bool) { |
| 455 | opt.handler_bool(params, parse_bool_value(value)); |
| 456 | } |
| 457 | if (opt.handler_string) { |
| 458 | opt.handler_string(params, value); |
| 459 | continue; |
| 460 | } |
| 461 | } catch (std::exception & e) { |
| 462 | throw std::invalid_argument(string_format( |
| 463 | "error while handling environment variable \"%s\": %s\n\n", opt.env, e.what())); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // handle command line arguments |
| 469 | auto check_arg = [&](int i) { |
| 470 | if (i+1 >= argc) { |
| 471 | throw std::invalid_argument("expected value for argument"); |
| 472 | } |
| 473 | }; |
| 474 | |
| 475 | auto parse_cli_args = [&]() { |
| 476 | std::set<std::string> seen_args; |
| 477 | |
| 478 | for (int i = 1; i < argc; i++) { |
| 479 | const std::string arg_prefix = "--"; |
| 480 | |
| 481 | std::string arg = argv[i]; |
| 482 | if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) { |
| 483 | std::replace(arg.begin(), arg.end(), '_', '-'); |
| 484 | } |
no test coverage detected