]> git.ipfire.org Git - thirdparty/git.git/commitdiff
parse-options API: add a usage_msg_optf()
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Tue, 28 Dec 2021 13:28:43 +0000 (14:28 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Dec 2021 21:05:28 +0000 (13:05 -0800)
Add a usage_msg_optf() as a shorthand for the sort of
usage_msg_opt(xstrfmt(...)) used in builtin/stash.c. I'll make more
use of this function in builtin/cat-file.c shortly.

The disconnect between the "..." and "fmt" is a bit unusual, but it
works just fine and this keeps it consistent with usage_msg_opt(),
i.e. a caller of it can be moved to usage_msg_optf() and not have to
have its arguments re-arranged.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stash.c
parse-options.c
parse-options.h

index 18c812bbe032cc24e57bd3083822bb4753dfa1ca..c9a09047a6ecf150fab309b2754c92d301be3f6c 100644 (file)
@@ -1811,8 +1811,8 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
        else if (!strcmp(argv[0], "save"))
                return !!save_stash(argc, argv, prefix);
        else if (*argv[0] != '-')
-               usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
-                             git_stash_usage, options);
+               usage_msg_optf(_("unknown subcommand: %s"),
+                              git_stash_usage, options, argv[0]);
 
        /* Assume 'stash push' */
        strvec_push(&args, "push");
index fc5b43ff0b2566abd1d4b5aa198cb21aa01f2045..5a319dda7d5466d1f93b64d256bbd889938756ca 100644 (file)
@@ -1078,3 +1078,16 @@ void NORETURN usage_msg_opt(const char *msg,
        fprintf(stderr, "fatal: %s\n\n", msg);
        usage_with_options(usagestr, options);
 }
+
+void NORETURN usage_msg_optf(const char * const fmt,
+                            const char * const *usagestr,
+                            const struct option *options, ...)
+{
+       struct strbuf msg = STRBUF_INIT;
+       va_list ap;
+       va_start(ap, options);
+       strbuf_vaddf(&msg, fmt, ap);
+       va_end(ap);
+
+       usage_msg_opt(msg.buf, usagestr, options);
+}
index 275fb440818d539e91e44a9f5824dd5edcf65420..4a9fa8a84d7c30ff120fb7107c944778431d5957 100644 (file)
@@ -225,6 +225,16 @@ NORETURN void usage_msg_opt(const char *msg,
                            const char * const *usagestr,
                            const struct option *options);
 
+/**
+ * usage_msg_optf() is like usage_msg_opt() except that the first
+ * argument is a format string, and optional format arguments follow
+ * after the 3rd option.
+ */
+__attribute__((format (printf,1,4)))
+void NORETURN usage_msg_optf(const char *fmt,
+                            const char * const *usagestr,
+                            const struct option *options, ...);
+
 /*
  * Use these assertions for callbacks that expect to be called with NONEG and
  * NOARG respectively, and do not otherwise handle the "unset" and "arg"