]> git.draconx.ca Git - dxcommon.git/blob - src/help.h
Add common option formatting routines.
[dxcommon.git] / src / help.h
1 /*
2  * Copyright © 2021 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.  The string is first
27  * localized (if NLS is enabled).  The first line will be indented by i-w
28  * spaces (to account for the cursor being in some other column), all other
29  * lines are indented by i spaces.
30  */
31 void help_print_desc(const struct option *opt, const char *desc, int i, int w);
32
33 static inline void help_print_option(const struct option *opt,
34                                      const char *argname, const char *desc,
35                                      int w)
36 {
37         if (w < 2)
38                 w = 2;
39
40         help_print_desc(opt, desc, w,
41                         help_print_optstring(opt, argname, w-2));
42 }
43
44 #endif