[RFC PATCH v2 13/17] lib: getopt: Drop printed error messages and the silent variant
Simon Glass
sjg at chromium.org
Wed May 20 01:31:39 CEST 2026
Inside __getopt() two printf() calls report 'invalid option' and
'option requires an argument'. Every U-Boot caller that uses getopt()
already detects '?' and ':' return values and prints its own usage
via CMD_RET_USAGE, so the messages are redundant. They also bring
in two format strings and the printf() argument plumbing.
Drop both printf() calls and the bool silent parameter that gates
them. Rename __getopt() to getopt() (the function is now small
enough not to need an inline trampoline), and turn getopt_silent()
into a static-inline alias of getopt() so the existing unit tests
keep working without change.
Saves around 60-120 bytes per arch (116 on sandbox, see following
buildman delta on firefly-rk3399).
Signed-off-by: Simon Glass <sjg at chromium.org>
---
(no changes since v1)
include/getopt.h | 21 ++++++++++-----------
lib/getopt.c | 6 +-----
2 files changed, 11 insertions(+), 16 deletions(-)
diff --git a/include/getopt.h b/include/getopt.h
index 52aabcc19ac..5ecd60d4d1b 100644
--- a/include/getopt.h
+++ b/include/getopt.h
@@ -61,8 +61,6 @@ struct getopt_state {
void getopt_init_state(struct getopt_state *gs, int argc,
char *const argv[]);
-int __getopt(struct getopt_state *gs, const char *optstring, bool silent);
-
/**
* getopt() - Parse short command-line options
* @gs: Internal state and out-of-band return arguments. This must be
@@ -89,6 +87,10 @@ int __getopt(struct getopt_state *gs, const char *optstring, bool silent);
* getopt() instead stops at the first non-option (POSIX ``getopt``
* behaviour). An ``--`` argument terminates option scanning in either mode.
*
+ * getopt() does not print any error messages; the caller is expected to
+ * detect '?' and ':' and report via its own usage string (typically
+ * CMD_RET_USAGE).
+ *
* An example invocation of getopt() might look like::
*
* char *argv[] = { "program", "-cbx", "-a", "foo", "bar", 0 };
@@ -122,21 +124,18 @@ int __getopt(struct getopt_state *gs, const char *optstring, bool silent);
* @gs.index is always set to the index of the next unparsed argument in
* @gs.argv.
*/
-static inline int getopt(struct getopt_state *gs, const char *optstring)
-{
- return __getopt(gs, optstring, false);
-}
+int getopt(struct getopt_state *gs, const char *optstring);
/**
- * getopt_silent() - Parse short command-line options silently
- * @gs: State
- * @optstring: Option specification
+ * getopt_silent() - Compatibility alias for getopt()
*
- * Same as getopt(), except no error messages are printed.
+ * getopt() no longer prints error messages, so this is identical to
+ * getopt(). Kept for callers (notably the unit tests) that named it
+ * explicitly.
*/
static inline int getopt_silent(struct getopt_state *gs, const char *optstring)
{
- return __getopt(gs, optstring, true);
+ return getopt(gs, optstring);
}
/**
diff --git a/lib/getopt.c b/lib/getopt.c
index 0179ae4d5ea..5e907477e57 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -27,7 +27,7 @@ void getopt_init_state(struct getopt_state *gs, int argc, char *const argv[])
gs->nonopts = 0;
}
-int __getopt(struct getopt_state *gs, const char *optstring, bool silent)
+int getopt(struct getopt_state *gs, const char *optstring)
{
char curopt; /* current option character */
const char *curoptp; /* pointer to the current option in optstring */
@@ -92,8 +92,6 @@ int __getopt(struct getopt_state *gs, const char *optstring, bool silent)
curoptp = strchr(optstring, curopt);
if (!curoptp) {
- if (!silent)
- printf("%s: invalid option -- %c\n", argv[0], curopt);
gs->opt = curopt;
gs->arg_index++;
return '?';
@@ -148,8 +146,6 @@ int __getopt(struct getopt_state *gs, const char *optstring, bool silent)
gs->arg_index = 1;
if (gs->index + gs->nonopts >= argc || argv[gs->index][0] == '-') {
- if (!silent)
- printf("option requires an argument -- %c\n", curopt);
gs->opt = curopt;
return ':';
}
--
2.43.0
More information about the U-Boot
mailing list