On 10/30/20 6:29 AM, Petr Machata wrote:
> diff --git a/lib/utils.c b/lib/utils.c
> index 9815e328c9e0..930877ae0f0d 100644
> --- a/lib/utils.c
> +++ b/lib/utils.c
> @@ -1735,3 +1735,31 @@ int do_batch(const char *name, bool force,
>
> return ret;
> }
> +
> +int parse_one_of(const char *msg, const char *realval, const char * const
> *list,
> + size_t len, int *p_err)
> +{
> + int i;
> +
> + for (i = 0; i < len; i++) {
> + if (list[i] && matches(realval, list[i]) == 0) {
> + *p_err = 0;
> + return i;
> + }
> + }
> +
> + fprintf(stderr, "Error: argument of \"%s\" must be one of ", msg);
> + for (i = 0; i < len; i++)
> + if (list[i])
> + fprintf(stderr, "\"%s\", ", list[i]);
> + fprintf(stderr, "not \"%s\"\n", realval);
> + *p_err = -EINVAL;
> + return 0;
> +}
> +
> +int parse_on_off(const char *msg, const char *realval, int *p_err)
> +{
> + static const char * const values_on_off[] = { "off", "on" };
> +
> + return parse_one_of(msg, realval, values_on_off,
> ARRAY_SIZE(values_on_off), p_err);
> +}
>
This has weird semantics to me. You have a buried array of strings and
returning the index of the one that matches. Let's use a 'bool' return
for parse_on_off that makes it clear that the string is 'off' = false or
'on' = true.