]> git.draconx.ca Git - dxcommon.git/blobdiff - src/help.h
Add common option formatting routines.
[dxcommon.git] / src / help.h
diff --git a/src/help.h b/src/help.h
new file mode 100644 (file)
index 0000000..9eb2654
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright © 2021 Nick Bowler
+ *
+ * Helper functions for formatting --help program output.
+ *
+ * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+ * This is free software: you are free to do what the fuck you want to.
+ * There is NO WARRANTY, to the extent permitted by law.
+ */
+
+#ifndef DX_HELP_H_
+#define DX_HELP_H_
+
+struct option;
+
+/*
+ * Print an option string describing the short option character (if any),
+ * the long option name, and the argument name (if applicable).  The argument
+ * name is localized (if NLS is enabled).  If the string width is more than
+ * l columns, a newline is printed and 0 is returned.  Otherwise, a newline
+ * is not printed and the string width (in columns) is returned.
+ */
+int help_print_optstring(const struct option *opt, const char *argname, int l);
+
+/*
+ * Print an option description with each line indented.  The string is first
+ * localized (if NLS is enabled).  The first line will be indented by i-w
+ * spaces (to account for the cursor being in some other column), all other
+ * lines are indented by i spaces.
+ */
+void help_print_desc(const struct option *opt, const char *desc, int i, int w);
+
+static inline void help_print_option(const struct option *opt,
+                                     const char *argname, const char *desc,
+                                    int w)
+{
+       if (w < 2)
+               w = 2;
+
+       help_print_desc(opt, desc, w,
+                       help_print_optstring(opt, argname, w-2));
+}
+
+#endif