support abbreviated options in getopt_long

This commit is contained in:
Rich Felker 2014-12-10 21:26:49 -05:00
parent 59f6232f85
commit cfd7b4acd5

View File

@ -56,23 +56,34 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring
((longonly && argv[optind][1]) || ((longonly && argv[optind][1]) ||
(argv[optind][1] == '-' && argv[optind][2]))) (argv[optind][1] == '-' && argv[optind][2])))
{ {
int i; int i, cnt, match;
for (i=0; longopts[i].name; i++) { char *opt;
for (cnt=i=0; longopts[i].name; i++) {
const char *name = longopts[i].name; const char *name = longopts[i].name;
char *opt = argv[optind]+1; opt = argv[optind]+1;
if (*opt == '-') opt++; if (*opt == '-') opt++;
for (; *name && *name == *opt; name++, opt++); for (; *name && *name == *opt; name++, opt++);
if (*name || (*opt && *opt != '=')) continue; if (*opt && *opt != '=') continue;
match = i;
if (!*name) {
cnt = 1;
break;
}
cnt++;
}
if (cnt==1) {
i = match;
optind++;
if (*opt == '=') { if (*opt == '=') {
if (!longopts[i].has_arg) continue; if (!longopts[i].has_arg) return '?';
optarg = opt+1; optarg = opt+1;
} else { } else {
if (longopts[i].has_arg == required_argument) { if (longopts[i].has_arg == required_argument) {
if (!(optarg = argv[++optind])) if (!(optarg = argv[optind]))
return ':'; return ':';
optind++;
} else optarg = NULL; } else optarg = NULL;
} }
optind++;
if (idx) *idx = i; if (idx) *idx = i;
if (longopts[i].flag) { if (longopts[i].flag) {
*longopts[i].flag = longopts[i].val; *longopts[i].flag = longopts[i].val;