Hi Sean, On Fri, 15 May 2026 at 15:40, Sean Anderson <[email protected]> wrote: > > On 5/15/26 16:32, Simon Glass wrote: > > Callers that consume positional arguments after parsing routinely write > > the same shape: > > > > algo = gs.args[gs.index]; > > /* advance past algo */ > > return hash_command(algo, ..., gs.nonopts - 1, &gs.args[gs.index + 1]); > > I think it's better to be explicit.
Do you mean that you don't want a function for this? My goal here is to try to reduce code size by turning common operations (here, getting the next arg, later perhaps getting the next arg as hex) into functions so we can share code. More generally, we should start to think of arg parsing at a higher-level, so that perhaps one day (which a fuller arg description for each command) we might end up with something which parses the args in common code, thus allowing commands to just use the args. > > > > Wrap that in a small inline helper that returns the next positional, > > advances gs.index, decrements gs.nonopts, and returns NULL when no > > positionals remain. The same helper doubles as an iterator: > > > > while ((arg = getopt_pop(&gs))) > > process(arg); > > > > stddef.h is now included from getopt.h for the NULL macro. > > > > Signed-off-by: Simon Glass <[email protected]> > > --- > > > > include/getopt.h | 23 +++++++++++++++++++++++ > > 1 file changed, 23 insertions(+) > > > > diff --git a/include/getopt.h b/include/getopt.h > > index 5a9e7802e65..013431807ff 100644 > > --- a/include/getopt.h > > +++ b/include/getopt.h > > @@ -10,6 +10,7 @@ > > #define __GETOPT_H > > > > #include <stdbool.h> > > +#include <stddef.h> > > #include <linux/kconfig.h> > > > > /** > > @@ -126,4 +127,26 @@ static inline int getopt_silent(struct getopt_state > > *gs, > > return __getopt(gs, optstring, true); > > } > > > > +/** > > + * getopt_pop() - Take the next remaining positional argument > > + * @gs: State, after getopt() has returned -1 > > + * > > + * Returns the first parked non-option (``gs->args[gs->index]``), > > + * advances the index, and decrements ``gs->nonopts``. Returns NULL > > + * when no positional arguments remain. > > + * > > + * Useful for consuming positionals one at a time after parsing:: > > + * > > + * algo = getopt_pop(&gs); > > + * return hash_command(algo, ..., gs.nonopts, &gs.args[gs.index]); > > + */ > > +static inline char *getopt_pop(struct getopt_state *gs) > > +{ > > + if (gs->index >= gs->argc) > > + return NULL; > > + if (gs->nonopts > 0) > > + gs->nonopts--; > > + return gs->args[gs->index++]; > > +} > > + > > #endif /* __GETOPT_H */ > Regards, Simon

