On 07/23/2013 10:55 AM, Bernhard Voelker wrote:
Is there already something available in gnulib like
err_exclusive_options() in util-linux [1] to automatically
catch mutual exclusiveness of options inside the getopt loop?

After spending an inordinate amount of time experimenting with gcc's compile-time constant support, I'm bet that its possible to write an inline function that will generate a compile-time error for this! Basically, something like this:

extern int _getopt (int ___argc, char *const *___argv, const char *__shortopts) __THROW;

static inline void __validate_opts(const char *__shortopts) {
    /* insert magic here */
}

static inline int getopt (int ___argc, char *const *___argv, const char *__shortopts) {
    if (__builtin_constant_p(*__shortopts))
        __validate_opts(__shortopts);
    return _getopt (___argc, ___argv, __shortopts);
}

When __validate_opts() is called, we know that they have passed a compile-time constant value, so we can process the shortopts w/o worrying about a run-time overhead. I would have to experiment further to figure the rest out and then see what the oldest version of gcc is that would treat it all as compile-time.

Daniel

Reply via email to