+#include <limits.h>
+#include <assert.h>
+#include <getopt.h>
+
+#ifndef _
+# define _(x) (x)
+#endif
+
+static inline int
+print_option_start(const struct option *opt, const char *argname)
+{
+ int w;
+
+ if (!argname)
+ argname = _("ARG");
+
+ if (opt->val >= CHAR_MIN && opt->val <= CHAR_MAX) {
+ switch (opt->has_arg) {
+ case 0:
+ w = printf(_(" -%c, --%s"), opt->val, opt->name);
+ break;
+ case 1:
+ w = printf(_(" -%c, --%s=%s"),
+ opt->val, opt->name, argname);
+ break;
+ case 2:
+ w = printf(_(" -%c, --%s[=%s]"),
+ opt->val, opt->name, argname);
+ break;
+ default:
+ assert(0);
+ }
+ } else {
+ switch (opt->has_arg) {
+ case 0:
+ w = printf(_(" --%s"), opt->name);
+ break;
+ case 1:
+ w = printf(_(" --%s=%s"), opt->name, argname);
+ break;
+ case 2:
+ w = printf(_(" --%s[=%s]"), opt->name, argname);
+ break;
+ default:
+ assert(0);
+ }
+ }
+
+ if (w > 18) {
+ putchar('\n');
+ w = 0;
+ }
+
+ return w;
+}