| 741 | |
| 742 | |
| 743 | RegisterParsedArgs::Ptr SingleCommand::parse_commandline(const std::string &arg0, |
| 744 | unsigned int skip, |
| 745 | int argc, |
| 746 | char **argv) |
| 747 | { |
| 748 | struct option *long_opts = init_getopt(); |
| 749 | |
| 750 | if (argv[1] && (argv[1] == alias_cmd) && !alias_remark.empty()) |
| 751 | { |
| 752 | std::cout << alias_remark << std::endl; |
| 753 | } |
| 754 | |
| 755 | RegisterParsedArgs::Ptr cmd_args; |
| 756 | cmd_args = RegisterParsedArgs::Create(arg0); |
| 757 | int c; |
| 758 | optind = 1 + skip; // Skip argv[0] which contains this command name |
| 759 | try |
| 760 | { |
| 761 | while (1) |
| 762 | { |
| 763 | int optidx = 0; |
| 764 | c = getopt_long(argc, argv, shortopts.c_str(), long_opts, &optidx); |
| 765 | if (-1 == c) // Are we done? |
| 766 | { |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | // If -h or --help is used, print the help screen and exit |
| 771 | if ('h' == c) |
| 772 | { |
| 773 | std::cout << gen_help(arg0) << std::endl; |
| 774 | goto exit; |
| 775 | } |
| 776 | |
| 777 | // If an unknown option is detected |
| 778 | if ('?' == c) |
| 779 | { |
| 780 | throw CommandException(command); |
| 781 | } |
| 782 | |
| 783 | // Check if this matches an option which has been defined |
| 784 | if (0 == c) |
| 785 | { |
| 786 | // No match on short option ... search on long option, |
| 787 | // based on optidx |
| 788 | |
| 789 | if (opt_version_added |
| 790 | && (0 == strncmp("version", long_opts[optidx].name, 7))) |
| 791 | { |
| 792 | std::cout << get_version(arg0) << std::endl; |
| 793 | goto exit; |
| 794 | } |
| 795 | |
| 796 | for (auto &o : options) |
| 797 | { |
| 798 | if (o->check_long_option(long_opts[optidx].name)) |
| 799 | { |
| 800 | cmd_args->register_option(o->get_option_name(), optarg); |
nothing calls this directly
no test coverage detected