Browse Source

Rework make_usage to print the usage message immediately

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
maint
Alex Riesen 17 years ago committed by Junio C Hamano
parent
commit
f389c808b6
  1. 47
      parse-options.c

47
parse-options.c

@ -1,6 +1,5 @@
#include "git-compat-util.h" #include "git-compat-util.h"
#include "parse-options.h" #include "parse-options.h"
#include "strbuf.h"


#define OPT_SHORT 1 #define OPT_SHORT 1
#define OPT_UNSET 2 #define OPT_UNSET 2
@ -172,59 +171,57 @@ int parse_options(int argc, const char **argv, const struct option *options,
void usage_with_options(const char * const *usagestr, void usage_with_options(const char * const *usagestr,
const struct option *opts) const struct option *opts)
{ {
struct strbuf sb; fprintf(stderr, "usage: %s\n", *usagestr++);

while (*usagestr && **usagestr)
strbuf_init(&sb, 4096); fprintf(stderr, " or: %s\n", *usagestr++);
strbuf_addstr(&sb, *usagestr); while (*usagestr)
strbuf_addch(&sb, '\n'); fprintf(stderr, " %s\n", *usagestr++);
while (*++usagestr)
strbuf_addf(&sb, " %s\n", *usagestr);


if (opts->type != OPTION_GROUP) if (opts->type != OPTION_GROUP)
strbuf_addch(&sb, '\n'); fputc('\n', stderr);


for (; opts->type != OPTION_END; opts++) { for (; opts->type != OPTION_END; opts++) {
size_t pos; size_t pos;
int pad; int pad;


if (opts->type == OPTION_GROUP) { if (opts->type == OPTION_GROUP) {
strbuf_addch(&sb, '\n'); fputc('\n', stderr);
if (*opts->help) if (*opts->help)
strbuf_addf(&sb, "%s\n", opts->help); fprintf(stderr, "%s\n", opts->help);
continue; continue;
} }


pos = sb.len; pos = fprintf(stderr, " ");
strbuf_addstr(&sb, " ");
if (opts->short_name) if (opts->short_name)
strbuf_addf(&sb, "-%c", opts->short_name); pos += fprintf(stderr, "-%c", opts->short_name);
if (opts->long_name && opts->short_name) if (opts->long_name && opts->short_name)
strbuf_addstr(&sb, ", "); pos += fprintf(stderr, ", ");
if (opts->long_name) if (opts->long_name)
strbuf_addf(&sb, "--%s", opts->long_name); pos += fprintf(stderr, "--%s", opts->long_name);


switch (opts->type) { switch (opts->type) {
case OPTION_INTEGER: case OPTION_INTEGER:
strbuf_addstr(&sb, " <n>"); pos += fprintf(stderr, " <n>");
break; break;
case OPTION_STRING: case OPTION_STRING:
if (opts->argh) if (opts->argh)
strbuf_addf(&sb, " <%s>", opts->argh); pos += fprintf(stderr, " <%s>", opts->argh);
else else
strbuf_addstr(&sb, " ..."); pos += fprintf(stderr, " ...");
break; break;
default: default:
break; break;
} }


pad = sb.len - pos; if (pos <= USAGE_OPTS_WIDTH)
if (pad <= USAGE_OPTS_WIDTH) pad = USAGE_OPTS_WIDTH - pos;
pad = USAGE_OPTS_WIDTH - pad;
else { else {
strbuf_addch(&sb, '\n'); fputc('\n', stderr);
pad = USAGE_OPTS_WIDTH; pad = USAGE_OPTS_WIDTH;
} }
strbuf_addf(&sb, "%*s%s\n", pad + USAGE_GAP, "", opts->help); fprintf(stderr, "%*s%s\n", pad + USAGE_GAP, "", opts->help);
} }
usage(sb.buf); fputc('\n', stderr);

exit(129);
} }

Loading…
Cancel
Save