tree-wide: change option_parse() to return option and arg via internal state
It was requested to make the 'c', 'opt', and 'arg' params the same, i.e. defined through the FOREACH_OPTION macro. But we can't do that easily, because 'c' was defined in the for loop definition, and we can only define variables of the same type in that way. Also, in some cases we need only 'c', in other cases with need 'c' and 'arg, in some cases 'c' and 'opt', and in other cases all three. We'd need to either conditionalize or mark those variables with _unused_ to deal with compiler warnings. But a different approach works quite nicely: add state.opt and state.arg to show the current option and it's argument. (The short names are picked on purpose to reduce verbosity since those are used a lot.)
This commit is contained in:
@@ -2505,10 +2505,9 @@ static int parse_argv(int argc, char *argv[], char ***ret_args) {
|
||||
assert(argv);
|
||||
assert(ret_args);
|
||||
|
||||
OptionParser state = { argc, argv };
|
||||
const char *arg;
|
||||
OptionParser opts = { argc, argv };
|
||||
|
||||
FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
|
||||
FOREACH_OPTION(c, &opts, /* on_error= */ return c)
|
||||
switch (c) {
|
||||
|
||||
OPTION_COMMON_HELP:
|
||||
@@ -2518,7 +2517,7 @@ static int parse_argv(int argc, char *argv[], char ***ret_args) {
|
||||
return version();
|
||||
}
|
||||
|
||||
*ret_args = option_parser_get_args(&state);
|
||||
*ret_args = option_parser_get_args(&opts);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user