Revamp ggo help.
[paraslash.git] / ggo.c
1 /*
2 * Copyright (C) 2008-2013 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file ggo.c Function for printing help. */
8
9
10 #include "para.h"
11 #include "ggo.h"
12 #include "version.h"
13
14 /**
15 * Wrapper for printf() that exits on errors.
16 *
17 * \param fmt Usual format string.
18 */
19 __printf_1_2 int printf_or_die(const char *fmt, ...)
20 {
21 va_list argp;
22 int ret;
23
24 va_start(argp, fmt);
25 ret = vprintf(fmt, argp);
26 va_end(argp);
27 if (ret >= 0)
28 return ret;
29 exit(EXIT_FAILURE);
30 }
31
32 /**
33 * Print one of the two given help texts.
34 *
35 * \param help contains the help texts.
36 * \param flags What to print, see \ref ggo_print_help_flags.
37 */
38 void ggo_print_help(struct ggo_help *help, unsigned flags)
39 {
40 const char **p;
41
42 if (flags & GPH_PRINT_NAME_VERSION)
43 printf_or_die("%s\n", version_single_line(help->prefix));
44 if (help->usage && (flags & GPH_PRINT_USAGE))
45 printf_or_die("\n%s\n", help->usage);
46 if (help->description && (flags & GPH_PRINT_DESCRIPTION))
47 printf_or_die("\n%s\n", help->description);
48 printf_or_die("\n");
49 if (flags & GPH_DETAILED)
50 p = help->detailed_help;
51 else
52 p = help->short_help;
53 if (!p)
54 return;
55 for (; *p; p++)
56 printf_or_die("%s\n", *p);
57 }