Skip to content

Commit fa476be

Browse files
avargitster
authored andcommitted
parse-options API: add a usage_msg_optf()
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68c69f9 commit fa476be

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

builtin/stash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,8 +1811,8 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
18111811
else if (!strcmp(argv[0], "save"))
18121812
return !!save_stash(argc, argv, prefix);
18131813
else if (*argv[0] != '-')
1814-
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
1815-
git_stash_usage, options);
1814+
usage_msg_optf(_("unknown subcommand: %s"),
1815+
git_stash_usage, options, argv[0]);
18161816

18171817
/* Assume 'stash push' */
18181818
strvec_push(&args, "push");

parse-options.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,3 +1078,16 @@ void NORETURN usage_msg_opt(const char *msg,
10781078
fprintf(stderr, "fatal: %s\n\n", msg);
10791079
usage_with_options(usagestr, options);
10801080
}
1081+
1082+
void NORETURN usage_msg_optf(const char * const fmt,
1083+
const char * const *usagestr,
1084+
const struct option *options, ...)
1085+
{
1086+
struct strbuf msg = STRBUF_INIT;
1087+
va_list ap;
1088+
va_start(ap, options);
1089+
strbuf_vaddf(&msg, fmt, ap);
1090+
va_end(ap);
1091+
1092+
usage_msg_opt(msg.buf, usagestr, options);
1093+
}

parse-options.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ NORETURN void usage_msg_opt(const char *msg,
225225
const char * const *usagestr,
226226
const struct option *options);
227227

228+
/**
229+
* usage_msg_optf() is like usage_msg_opt() except that the first
230+
* argument is a format string, and optional format arguments follow
231+
* after the 3rd option.
232+
*/
233+
__attribute__((format (printf,1,4)))
234+
void NORETURN usage_msg_optf(const char *fmt,
235+
const char * const *usagestr,
236+
const struct option *options, ...);
237+
228238
/*
229239
* Use these assertions for callbacks that expect to be called with NONEG and
230240
* NOARG respectively, and do not otherwise handle the "unset" and "arg"

0 commit comments

Comments
 (0)