U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Anderson <seanga2@gmail.com>
To: Simon Glass <sjg@chromium.org>, u-boot@lists.denx.de
Cc: Tom Rini <trini@konsulko.com>
Subject: Re: [RFC PATCH 06/11] lib: getopt: Add getopt_pop() helper
Date: Fri, 15 May 2026 17:40:20 -0400	[thread overview]
Message-ID: <2ca02f73-20b9-22fe-5ec3-97c56c0f11bf@gmail.com> (raw)
In-Reply-To: <20260515203311.2555651-7-sjg@chromium.org>

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.

> 
> 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 <sjg@chromium.org>
> ---
> 
>   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 */


  reply	other threads:[~2026-05-15 21:40 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15 20:32 [RFC PATCH 00/11] Tidy command option parsing and use it a bit Simon Glass
2026-05-15 20:32 ` [RFC PATCH 01/11] lib: string: Add strlower() Simon Glass
2026-05-15 20:32 ` [RFC PATCH 02/11] cmd: ini: Use strlower() to normalise case Simon Glass
2026-05-15 20:32 ` [RFC PATCH 03/11] fs: fat: " Simon Glass
2026-05-15 20:32 ` [RFC PATCH 04/11] boot: pxe_utils: Use strlower() in get_string() Simon Glass
2026-05-15 20:32 ` [RFC PATCH 05/11] lib: getopt: Permute by default with inline reorder Simon Glass
2026-05-15 21:37   ` Sean Anderson
2026-05-20 20:42     ` Simon Glass
2026-05-15 20:32 ` [RFC PATCH 06/11] lib: getopt: Add getopt_pop() helper Simon Glass
2026-05-15 21:40   ` Sean Anderson [this message]
2026-05-20 20:41     ` Simon Glass
2026-05-15 20:32 ` [RFC PATCH 07/11] cmd: echo: Use getopt() with '+' prefix for option parsing Simon Glass
2026-05-15 21:58   ` Sean Anderson
2026-05-20 20:41     ` Simon Glass
2026-05-15 20:32 ` [RFC PATCH 08/11] cmd: hash: Use getopt() " Simon Glass
2026-05-15 20:33 ` [RFC PATCH 09/11] cmd: nvedit: Use getopt() in env grep Simon Glass
2026-05-15 20:33 ` [RFC PATCH 10/11] cmd: nvedit: Use getopt() in env export and env import Simon Glass
2026-05-15 20:33 ` [RFC PATCH 11/11] doc: commands: Recommend getopt() for option parsing Simon Glass
2026-05-15 21:43 ` [RFC PATCH 00/11] Tidy command option parsing and use it a bit Tom Rini
2026-05-15 21:59   ` Sean Anderson
2026-05-15 22:06     ` Sean Anderson
2026-05-15 22:21       ` Tom Rini
2026-05-15 22:27         ` Sean Anderson
2026-05-20 20:40           ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2ca02f73-20b9-22fe-5ec3-97c56c0f11bf@gmail.com \
    --to=seanga2@gmail.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox