]> git.draconx.ca Git - dxcommon.git/blob - src/help.h
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / src / help.h
1 /*
2  * Copyright © 2021, 2023 Nick Bowler
3  *
4  * Helper functions for formatting --help program output.
5  *
6  * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
7  * This is free software: you are free to do what the fuck you want to.
8  * There is NO WARRANTY, to the extent permitted by law.
9  */
10
11 #ifndef DX_HELP_H_
12 #define DX_HELP_H_
13
14 struct option;
15
16 /*
17  * Print an option string describing the short option character (if any),
18  * the long option name, and the argument name (if applicable).  The argument
19  * name is localized (if NLS is enabled).  If the string width is more than
20  * l columns, a newline is printed and 0 is returned.  Otherwise, a newline
21  * is not printed and the string width (in columns) is returned.
22  */
23 int help_print_optstring(const struct option *opt, const char *argname, int l);
24
25 /*
26  * Print an option description with each line indented.  If opt is not NULL,
27  * then the string is first localized (if NLS is enabled) via pgettext_expr
28  * with the context set to opt->name.  The first line will be indented by
29  * i-w spaces (to account for the cursor being in some other column), all
30  * other lines are indented by i spaces.
31  *
32  * The output always ends with a newline, regardless of whether or not the
33  * input string ends with a newline.
34  */
35 void help_print_desc(const struct option *opt, const char *desc, int i, int w);
36
37 static inline void help_print_option(const struct option *opt,
38                                      const char *argname, const char *desc,
39                                      int w)
40 {
41         if (w < 2)
42                 w = 2;
43
44         help_print_desc(opt, desc, w,
45                         help_print_optstring(opt, argname, w-2));
46 }
47
48 #endif