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:
committed by
Luca Boccassi
parent
7671b43cb8
commit
99ce7a0770
@@ -216,13 +216,15 @@ int option_parse(
|
||||
const Option options_end[],
|
||||
OptionParser *state);
|
||||
|
||||
/* Iterate over options. */
|
||||
#define FOREACH_OPTION(c, state, on_error) \
|
||||
/* Iterate over options. Don't forget to handle errors (negative c)! */
|
||||
#define FOREACH_OPTION(c, state) \
|
||||
for (int c; (c = option_parse(ALIGN_PTR(__start_SYSTEMD_OPTIONS), __stop_SYSTEMD_OPTIONS, state)) != 0; )
|
||||
|
||||
#define FOREACH_OPTION_OR_RETURN(c, state) \
|
||||
for (int c; (c = option_parse(ALIGN_PTR(__start_SYSTEMD_OPTIONS), __stop_SYSTEMD_OPTIONS, state)) != 0; ) \
|
||||
if (c < 0) { \
|
||||
on_error; \
|
||||
break; \
|
||||
} else
|
||||
if (c < 0) \
|
||||
return c; \
|
||||
else
|
||||
|
||||
/* Those helpers are used *during* option parsing and allow looking at or taking the next item in
|
||||
* the argv array, either an option or a positional parameter. */
|
||||
|
||||
Reference in New Issue
Block a user