435 {
437 _program_name =
argv[0];
438 _program_name.erase(0, _program_name.find_last_of("/\\") + 1);
439 if (argc >= 2)
440 {
441 if (!std::strcmp(argv[1], "--help") || !std::strcmp(argv[1], "-h"))
442 {
443 std::cout << *this;
444 std::exit(0);
445 }
446 else
447 {
448 for (const auto &arg : _arg_map)
449 {
450 const auto &func =
arg.second->_func;
451 if (func && !std::strcmp(argv[1],
arg.first.c_str()))
452 {
453 func();
454 std::exit(0);
455 }
456 }
457 }
458 }
459
460
461
462
463 size_t parg_num = _positional_arg_vec.size();
464
465 size_t required_oarg_num = 0;
466 for (auto arg : _optional_arg_vec)
467 {
468 if (
arg._is_required)
469 required_oarg_num++;
470 }
471
472 for (
int c = 1; c <
argc;)
473 {
474 std::string arg_name{
argv[c++]};
475 auto arg = _arg_map.find(arg_name);
476
477 if (arg == _arg_map.end())
478 {
479 if (parg_num)
480 {
481 auto it = _positional_arg_vec.begin();
482 std::advance(it, _positional_arg_vec.size() - parg_num);
483 (*it)._values.clear();
484 (*it)._values.emplace_back(arg_name);
485 parg_num--;
486 }
487 else
488 throw std::runtime_error("Invalid argument. "
489 "You've given more positional argument than necessary.");
490 }
491 else
492 {
493
494 if (
arg->second->_is_required)
495 required_oarg_num--;
496 arg->second->_values.clear();
497 for (uint32_t n = 0; n <
arg->second->_nargs; n++)
498 {
499 if (c >= argc)
500 throw std::runtime_error("Invalid argument. "
501 "You must have missed some argument.");
502 arg->second->_values.emplace_back(argv[c++]);
503 }
504
505 if (
arg->second->_is_accumulated)
506 {
507 arg->second->_accum_values.emplace_back(
arg->second->_values);
508 }
509 if (
arg->second->_nargs == 0)
510 {
511
512 arg->second->_values.emplace_back(
"1");
513 }
514 }
515 }
516 if (parg_num || required_oarg_num)
517 throw std::runtime_error("Invalid argument. "
518 "You must have missed some argument.");
519 }
void validate_arguments(void)