On Sat, Sep 09, 2023 at 12:28:20AM +0200, René Scharfe wrote: > >> OPTARG would need a new macro to allow specifying the default value. Or > >> is there a variadic macro trick that we could use? > > > > Hmm, I had just assumed OPTARG was a lost cause (or we would need an > > "OPTARG" variant of each macro; yuck). > > Only for OPT_INTEGER and OPT_STRING AFAICS. True, my use of BOOL was obviously dumb, as it wouldn't have arguments. But in theory anything that takes an argument could have an OPTARG variant. So that would include special stuff like OPT_EXPIRY_DATE, OPT_FILENAME, and so on. Though I would not be surprised if we currently only use it for string/integer. > It's true that a macro that accepts a variable number of arguments would > accept accidental extra arguments of the right type, but I don't see how > it would ignore excessive ones. The macro itself wouldn't notice, but I guess the generated code would probably complain about getting "(foo,bar)" as the initializer, if you really sent to many. But I was more worried about an error where you accidentally give an extra argument. Right now that's an error, but would it be quietly shifted into the OPTARG slot? > > You'd want some semantic check between what's in flags (i.e., is the > > OPTARG flag set), but I think that's beyond what the compiler itself can > > do (you could probably write a coccinelle rule for it, though). > > Actually I'd want the macro to set that flag for me. For a dedicated OPT_STRING_OPTARG(), I'd agree. For OPT_STRING() that uses varargs, I'm more on the fence (because of the cross-checking above; now we are getting into "accidentally adding a parameter is quietly accepted" territory). I dunno. Maybe saving some keystrokes is worth it, but having to say both OPTARG _and_ provide the extra argument makes things less subtle. > I was thinking more about something like the solutions discussed at > https://stackoverflow.com/questions/47674663/variable-arguments-inside-a-macro. > It allows selecting variants based on argument count. > [...] > So OPT_INTEGER(s, l, v, h) would be the same as before. Add an argument > and it becomes current OPT_INTEGER_F, add another one and it acts as > your _OPTARG_F variant. Ah, yeah, I've seen something like this before. I do think it would work as you're suggesting. I'm just not sure if being verbose and explicit is better than trying to be clever here. -Peff