| 644 | } |
| 645 | |
| 646 | bool parse(const std::vector<std::string>& args) |
| 647 | { |
| 648 | std::unordered_map<std::string, unsigned> keywords; |
| 649 | for(auto&& arg : arguments) |
| 650 | { |
| 651 | for(auto&& flag : arg.flags) |
| 652 | keywords[flag] = arg.nargs + 1; |
| 653 | } |
| 654 | auto arg_map = generic_parse(args, [&](const std::string& x) { return keywords[x]; }); |
| 655 | std::list<const argument*> missing_arguments; |
| 656 | std::unordered_set<std::string> groups_used; |
| 657 | for(auto&& arg : arguments) |
| 658 | { |
| 659 | bool used = false; |
| 660 | auto flags = arg.flags; |
| 661 | if(flags.empty()) |
| 662 | flags = {""}; |
| 663 | for(auto&& flag : flags) |
| 664 | { |
| 665 | if(arg_map.count(flag) > 0) |
| 666 | { |
| 667 | if(run_action(arg, flag, arg_map[flag])) |
| 668 | return true; |
| 669 | used = true; |
| 670 | } |
| 671 | } |
| 672 | if(used and not arg.group.empty()) |
| 673 | groups_used.insert(arg.group); |
| 674 | if(arg.required and not used) |
| 675 | missing_arguments.push_back(&arg); |
| 676 | } |
| 677 | // Remove arguments from a group that is being used |
| 678 | missing_arguments.remove_if( |
| 679 | [&](const argument* arg) { return groups_used.count(arg->group); }); |
| 680 | if(not missing_arguments.empty()) |
| 681 | { |
| 682 | std::cout << color::fg_red << color::bold << "error: " << color::reset; |
| 683 | std::cout << "The following required arguments were not provided:" << std::endl; |
| 684 | std::cout << " " << color::fg_red |
| 685 | << join_strings(get_argument_usages(std::move(missing_arguments)), " ") |
| 686 | << color::reset << std::endl; |
| 687 | std::cout << std::endl; |
| 688 | auto required_usages = get_argument_usages(get_required_arguments()); |
| 689 | print_usage(required_usages); |
| 690 | print_try_help(); |
| 691 | return true; |
| 692 | } |
| 693 | for(auto&& action : actions) |
| 694 | action(*this); |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | void set_exe_name(const std::string& s) { exe_name = s; } |
| 699 |
nothing calls this directly
no test coverage detected