tools: re-factor usage message function

* change order of long & short option names: letters first, then the long names
  Effect: nicely aligned short and long option names in the help text
* more space between option names and explanation
  Effect: better readability on long options
* print "Options:" header only if there is at least one non-hidden options
  Effect: nicer output when all options are hidden
* only show printable, non-space short options letters
  Effect: no control codes printed to terminal
* get rid of a temporary variable
This commit is contained in:
Peter Marschall 2012-05-26 18:01:37 +02:00 committed by Viktor Tarasov
parent 367ebd94d4
commit 93cb77079b
1 changed files with 20 additions and 16 deletions

View File

@ -199,27 +199,25 @@ void util_hex_dump_asc(FILE *f, const u8 *in, size_t count, int addr)
void util_print_usage_and_die(const char *app_name, const struct option options[],
const char *option_help[], const char *args)
{
int i = 0;
int i;
int header_shown = 0;
if (args)
printf("Usage: %s [OPTIONS] %s\nOptions:\n", app_name, args);
printf("Usage: %s [OPTIONS] %s\n", app_name, args);
else
printf("Usage: %s [OPTIONS]\nOptions:\n", app_name);
printf("Usage: %s [OPTIONS]\n", app_name);
while (options[i].name) {
char buf[40], tmp[5];
for (i = 0; options[i].name; i++) {
char buf[40];
const char *arg_str;
/* Skip "hidden" options */
if (option_help[i] == NULL) {
i++;
if (option_help[i] == NULL)
continue;
}
if (options[i].val > 0 && options[i].val < 128)
sprintf(tmp, ", -%c", options[i].val);
else
tmp[0] = 0;
if (!header_shown++)
printf("Options:\n");
switch (options[i].has_arg) {
case 1:
arg_str = " <arg>";
@ -231,14 +229,20 @@ void util_print_usage_and_die(const char *app_name, const struct option options[
arg_str = "";
break;
}
sprintf(buf, "--%s%s%s", options[i].name, tmp, arg_str);
if (strlen(buf) > 29) {
if (isascii(options[i].val) &&
isprint(options[i].val) && !isascii(options[i].val))
sprintf(buf, "-%c, --%s%s", options[i].val, options[i].name, arg_str);
else
sprintf(buf, " --%s%s", options[i].name, arg_str);
/* print the line - wrap if necessary */
if (strlen(buf) > 28) {
printf(" %s\n", buf);
buf[0] = '\0';
}
printf(" %-29s %s\n", buf, option_help[i]);
i++;
printf(" %-28s %s\n", buf, option_help[i]);
}
exit(2);
}