Bison has been using for years a trick with argmatch: exploit the support for partial matches to have a simple documentation system:
$ bison --trace=help bison: invalid argument ‘help’ for ‘--trace’ Valid arguments are: - ‘none - no traces’ - ‘scan - grammar scanner traces’ - ‘parse - grammar parser traces’ - ‘parse-stats - grammar parser stats’ - ‘automaton - construction of the automaton’ - ‘bitsets - use of bitsets’ - ‘closure - input/output of closure’ - ‘grammar - reading, reducing the grammar’ - ‘resource - memory consumption (where available)’ - ‘sets - grammar sets: firsts, nullable etc.’ - ‘muscles - m4 definitions passed to the skeleton’ - ‘tools - m4 invocation’ - ‘m4 - m4 traces’ - ‘skeleton - skeleton postprocessing’ - ‘time - time consumption’ - ‘ielr - IELR conversion’ - ‘all - all of the above’ This is a low profile feature, meant for Bison developers and contributors, not end users, so i18n is not an issue here. However, I just introduced the parse-stats category, and as a consequence "--trace=parse" no longer matches the corresponding item, since "parse" is a prefix for both "parse - grammar parser traces" and "parse-stats - grammar parser stats". I don't think people are using arguments with spaces, so I propose to acknowledge this use of argmatch by accepting as a perfect match the first option, since "parse" is a prefix followed by a space. WDYT? commit 7fc1d6af473b6572bc643e0481ac0e04f9150cd1 Author: Akim Demaille <akim.demai...@gmail.com> Date: Thu Apr 18 22:48:29 2019 +0200 argmatch: accept perfect matches in documented arglists * lib/argmatch.c (argmatch): Consider effective arguments that are prefix of one of the arglist followed by a space to be perfect matches. diff --git a/ChangeLog b/ChangeLog index 7d6cb0b0a..0ce1de17d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2019-04-18 Akim Demaille <a...@lrde.epita.fr> + + argmatch: accept perfect matches in documented arglists. + * lib/argmatch.c (argmatch): Consider effective arguments that are + prefix of one of the arglist followed by a space to be perfect + matches. + 2019-04-18 Akim Demaille <a...@lrde.epita.fr> argmatch: use void* for raw memory pointers diff --git a/lib/argmatch.c b/lib/argmatch.c index 7666ef414..79512125c 100644 --- a/lib/argmatch.c +++ b/lib/argmatch.c @@ -78,7 +78,17 @@ argmatch_exit_fn argmatch_die = __argmatch_die; synonyms, i.e., for "yes", "yop" -> 0 "no", "nope" -> 1 - "y" is a valid argument, for 0, and "n" for 1. */ + "y" is a valid argument, for 0, and "n" for 1. + + Accept arguments with a documentation string, such as: + "foo - a first feature" -> 0 + "bar - a second feature" -> 1 + "barest - a better second feature" -> 2 + In this case, "ba" is ambiguous, but accept "bar" as a perfect + match of 1, even though technically it is also a prefix for 2. + This is because "bar" is followed by a space in the argument list + ("bar - a second feature"). + */ ptrdiff_t argmatch (const char *arg, const char *const *arglist, @@ -96,7 +106,8 @@ argmatch (const char *arg, const char *const *arglist, { if (!strncmp (arglist[i], arg, arglen)) { - if (strlen (arglist[i]) == arglen) + if (strlen (arglist[i]) == arglen + || (arglen < strlen (arglist[i]) && arglist[i][arglen] == ' ')) /* Exact match found. */ return i; else if (matchind == -1)