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 <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
68c69f90c8
commit
fa476be8f0
@ -1811,8 +1811,8 @@ int cmd_stash(int argc, const char **argv, const char *prefix)
|
|||||||
else if (!strcmp(argv[0], "save"))
|
else if (!strcmp(argv[0], "save"))
|
||||||
return !!save_stash(argc, argv, prefix);
|
return !!save_stash(argc, argv, prefix);
|
||||||
else if (*argv[0] != '-')
|
else if (*argv[0] != '-')
|
||||||
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
|
usage_msg_optf(_("unknown subcommand: %s"),
|
||||||
git_stash_usage, options);
|
git_stash_usage, options, argv[0]);
|
||||||
|
|
||||||
/* Assume 'stash push' */
|
/* Assume 'stash push' */
|
||||||
strvec_push(&args, "push");
|
strvec_push(&args, "push");
|
||||||
|
@ -1078,3 +1078,16 @@ void NORETURN usage_msg_opt(const char *msg,
|
|||||||
fprintf(stderr, "fatal: %s\n\n", msg);
|
fprintf(stderr, "fatal: %s\n\n", msg);
|
||||||
usage_with_options(usagestr, options);
|
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);
|
||||||
|
}
|
||||||
|
@ -225,6 +225,16 @@ NORETURN void usage_msg_opt(const char *msg,
|
|||||||
const char * const *usagestr,
|
const char * const *usagestr,
|
||||||
const struct option *options);
|
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
|
* Use these assertions for callbacks that expect to be called with NONEG and
|
||||||
* NOARG respectively, and do not otherwise handle the "unset" and "arg"
|
* NOARG respectively, and do not otherwise handle the "unset" and "arg"
|
||||||
|
Reference in New Issue
Block a user