options: get rid of "on_error" parameter to FOREACH_OPTION

I am really not a fan of full code lines passed to macros as parameters.
Let's get rid of the 3rd parameter of FOREACH_OPTION() hence:

1. Let's return errors just as a regular value (though a negative one),
   that can be handled via a OPTION_ERROR case statement for the switch.
   This normalizes handling of the error, just like any other event
   returned by the option parser.

2. In order to avoid exploding the amount of boilerplate in each use
   (that just propagates the error on OPTION_ERROR), let's then
   introduce an explicit FOREACH_OPTION_OR_RETURN(), that returns from
   the calling function on its own (and makes that clear in the name).

Together this cleans up, normalizes the logic and shortens the code.
This commit is contained in:
Lennart Poettering
2026-04-30 11:12:33 +01:00
committed by Luca Boccassi
parent 7671b43cb8
commit 99ce7a0770
111 changed files with 123 additions and 118 deletions
+1 -1
View File
@@ -423,7 +423,7 @@ static int parse_argv(int argc, char *argv[]) {
OptionParser opts = { argc, argv };
int r;
FOREACH_OPTION(c, &opts, /* on_error= */ return c)
FOREACH_OPTION_OR_RETURN(c, &opts)
switch (c) {
OPTION_COMMON_HELP: