2 * Copyright © 2021-2022 Nick Bowler
4 * Test helper application to verify help_print_optstring operation.
6 * Command-line arguments are collected to create the options to be formatted.
7 * There are six basic forms:
10 * --long-option-with-argument arg
11 * --long-option-with-optional-argument [arg]
12 * --short-option -s arg
13 * --short-option-with-argument -s arg
14 * --short-option-with-optional-argument -s [arg]
16 * Adding an argument of '&' to any form will set the "flag" member of
17 * the option structure to a non-null value.
19 * Based on these arguments, a suitable 'struct option' is constructed and
20 * passed to help_print_optstring. Then a tab character is printed, followed
21 * by the return value of help_print_optsring (via printf %d). Then further
22 * arguments are considered to output more options.
24 * Initially, the "l" value passed to help_print_optstring is 20. This can be
25 * changed at any point by a numeric command-line argument, which will set a
26 * new value that applies to all subsequent calls until it is changed again.
28 * License WTFPL2: Do What The Fuck You Want To Public License, version 2.
29 * This is free software: you are free to do what the fuck you want to.
30 * There is NO WARRANTY, to the extent permitted by law.
43 int arg_to_int(const char *s)
49 val = strtol(s, &end, 0);
51 tap_bail_out("%s: numeric argument expected", s);
52 else if (val < INT_MIN || val > INT_MAX || errno == ERANGE)
53 tap_bail_out("%s: %s", s, strerror(ERANGE));
55 tap_bail_out("%s: %s", s, strerror(errno));
60 void print_opt(struct option *opt, const char *argname, int w)
62 w = help_print_optstring(opt, argname, w);
66 int main(int argc, char **argv)
68 struct option opt = {0};
69 const char *argname = 0;
72 for (i = 1; i < argc; i++) {
73 if (argv[i][0] == '-' && argv[i][1] == '-') {
75 print_opt(&opt, argname, w);
76 opt.val = UCHAR_MAX+1;
80 } else if (argv[i][0] == '-') {
82 } else if (argv[i][0] == '[') {
86 if ((c = strchr(argname, ']')))
89 } else if (argv[i][0] == '&') {
92 } else if (argv[i][0] >= '0' && argv[i][0] <= '9') {
93 w = arg_to_int(argv[i]);
101 print_opt(&opt, argname, w);