--- ChangeLog | 6 ++++++ lib/getopt.c | 25 +++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 7faac2b..b593588 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-12-14 James Youngman <[email protected]> + + getopt: Indicate the problem with ambiguous options clearly. + * lib/getopt.c (_getopt_internal_r): Print the first and second + possibility when there is an ambiguous match. + 2008-12-14 Bruno Haible <[email protected]> Update doc for POSIX:2008. diff --git a/lib/getopt.c b/lib/getopt.c index f1e6d1f..107538f 100644 --- a/lib/getopt.c +++ b/lib/getopt.c @@ -480,8 +480,8 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, char *nameend; const struct option *p; const struct option *pfound = NULL; + const struct option *pambig = NULL; int exact = 0; - int ambig = 0; int indfound = -1; int option_index; @@ -512,19 +512,25 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) - /* Second or later nonexact match found. */ - ambig = 1; + { + /* Second or later nonexact match found. We remember + it instead of breaking out of the loop in case + there is a later exact match to be found. */ + pambig = p; + } } - if (ambig && !exact) + if (pambig && !exact) { if (print_errors) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; - if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), - argv[0], argv[d->optind]) >= 0) + if (__asprintf (&buf, _("%s: option `%s' is ambiguous, " + "since for example it might mean --%s or --%s\n"), + argv[0], argv[d->optind], + pfound->name, pambig->name) >= 0) { _IO_flockfile (stderr); @@ -539,8 +545,11 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, free (buf); } #else - fprintf (stderr, _("%s: option `%s' is ambiguous\n"), - argv[0], argv[d->optind]); + fprintf (stderr, _("%s: option `%s' is ambiguous, " + "since for example it might mean --%s or --%s\n"), + argv[0], argv[d->optind], + pfound->name, pambig->name); + #endif } d->__nextchar += strlen (d->__nextchar); -- 1.5.6.5
