bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getopt: checking exlusive options?


From: Daniel Santos
Subject: Re: getopt: checking exlusive options?
Date: Tue, 23 Jul 2013 16:24:08 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130620 Thunderbird/17.0.6

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

[Prev in Thread] Current Thread [Next in Thread]