[PATCH 3/5] parameter: allow enum params to be set by numeric index
Ahmad Fatoum
a.fatoum at barebox.org
Mon Apr 13 03:06:58 PDT 2026
When no name matches the input string, fall back to parsing it as a
decimal index into the names array for backwards compatibility.
This lets scripts and users write e.g. "0" or "1" in addition to the
symbolic name, which can be useful if a boolean is turned into an enum
to allow for more options.
As the index isn't stable, we emit a warning to point out that this is
just to simplify migration.
Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
lib/parameter.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/lib/parameter.c b/lib/parameter.c
index 5c8b86d0dff3..f4e62cc1ae87 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -589,8 +589,17 @@ static int param_enum_set(struct bobject *bobj, struct param_d *p,
if (pe->names[i] && !strcmp(val, pe->names[i]))
break;
- if (i == pe->num_names)
- return -EINVAL;
+ if (i == pe->num_names) {
+ char *endp;
+ unsigned long idx = simple_strtoul(val, &endp, 0);
+
+ if (*endp || idx >= pe->num_names || !pe->names[idx])
+ return -EINVAL;
+
+ pr_warn("setting %s.%s by index is not stable, use \"%s\" instead\n",
+ bobj->name, p->name, pe->names[idx]);
+ i = idx;
+ }
*pe->value = i;
--
2.47.3
More information about the barebox
mailing list