From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Cc: Sean Anderson <seanga2@gmail.com>, Simon Glass <sjg@chromium.org>,
Tom Rini <trini@konsulko.com>
Subject: [RFC PATCH 06/11] lib: getopt: Add getopt_pop() helper
Date: Fri, 15 May 2026 14:32:57 -0600 [thread overview]
Message-ID: <20260515203311.2555651-7-sjg@chromium.org> (raw)
In-Reply-To: <20260515203311.2555651-1-sjg@chromium.org>
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]);
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 */
--
2.43.0
next prev parent reply other threads:[~2026-05-15 20:34 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 ` Simon Glass [this message]
2026-05-15 21:40 ` [RFC PATCH 06/11] lib: getopt: Add getopt_pop() helper Sean Anderson
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=20260515203311.2555651-7-sjg@chromium.org \
--to=sjg@chromium.org \
--cc=seanga2@gmail.com \
--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;
as well as URLs for NNTP newsgroup(s).