--------------------------------------------------------------------------- ^FUNCTION: Options::parse_opt - parse an option ^SYNOPSIS: int Options::parse_opt(iter, optarg) ^PARAMETERS: OptIter & iter -- option iterator const char * & optarg -- where to store any option-argument ^DESCRIPTION: Parse the next option in iter (advancing as necessary). Make sure we update the nextchar pointer along t
| 822 | // It gets complicated -- follow the comments in the source. |
| 823 | // ^^------------------------------------------------------------------------- |
| 824 | int Options::parse_opt(OptIter &iter, const char *&optarg) |
| 825 | { |
| 826 | listopt = NULLSTR; // reset the list pointer |
| 827 | |
| 828 | if ((optvec == NULL) || (!*optvec)) |
| 829 | return static_cast<signed>(Options::OptRC::ENDOPTS); |
| 830 | |
| 831 | // Try to match a known option |
| 832 | OptionSpec optspec = match_opt(*(nextchar++), (optctrls & static_cast<signed>(Options::OptCtrl::ANYCASE))); |
| 833 | |
| 834 | // Check for an unknown option |
| 835 | if (optspec.isNULL()) |
| 836 | { |
| 837 | // See if this was a long-option in disguise |
| 838 | if (!(optctrls & static_cast<signed>(Options::OptCtrl::NOGUESSING))) |
| 839 | { |
| 840 | unsigned save_ctrls = optctrls; |
| 841 | const char *save_nextchar = nextchar; |
| 842 | nextchar -= 1; |
| 843 | optctrls |= |
| 844 | (static_cast<signed>(Options::OptCtrl::QUIET) | static_cast<signed>(Options::OptCtrl::NOGUESSING)); |
| 845 | int optchar = parse_longopt(iter, optarg); |
| 846 | optctrls = save_ctrls; |
| 847 | if (optchar > 0) |
| 848 | { |
| 849 | return optchar; |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | nextchar = save_nextchar; |
| 854 | } |
| 855 | } |
| 856 | if (!(optctrls & static_cast<signed>(Options::OptCtrl::QUIET))) |
| 857 | { |
| 858 | cerr << cmdname << ": unknown option -" << *(nextchar - 1) << "." << endl; |
| 859 | } |
| 860 | optarg = (nextchar - 1); // record the bad option in optarg |
| 861 | return static_cast<signed>(Options::OptRC::BADCHAR); |
| 862 | } |
| 863 | |
| 864 | // If no argument is taken, then leave now |
| 865 | if (optspec.isNoArg()) |
| 866 | { |
| 867 | optarg = NULLSTR; |
| 868 | return optspec.OptChar(); |
| 869 | } |
| 870 | |
| 871 | // Check for argument in this arg |
| 872 | if (*nextchar) |
| 873 | { |
| 874 | optarg = nextchar; // the argument is right here |
| 875 | nextchar = NULLSTR; // we've exhausted this arg |
| 876 | if (optspec.isList()) |
| 877 | listopt = optspec; // save the list-spec |
| 878 | return optspec.OptChar(); |
| 879 | } |
| 880 | |
| 881 | // Check for argument in next arg |