Mercurial > hg > dmlib
annotate src/dmargs.c @ 2400:01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 12 Jan 2020 19:25:52 +0200 |
parents | c801995cbb13 |
children | 263093248f26 |
rev | line source |
---|---|
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
1 /* |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
2 * Simple commandline argument processing |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
2361 | 4 * (C) Copyright 2002-2020 Tecnic Software productions (TNSP) |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
5 * |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
7 */ |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
8 /// @file |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
9 /// @brief Simple commandline argument processing functions |
0 | 10 #include "dmargs.h" |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
11 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
12 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
13 /** |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
14 * Parse and optionally handle the given long or short option argument. |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
15 * @param currArg current argument string |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
16 * @param argIndex pointer to index of current argument in argv[] |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
17 * @param argc number of arguments |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
18 * @param argv argument string array |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
19 * @param opts options list array |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
20 * @param nopts number of elements in options list array |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
21 * @param handle_option function pointer to callback that handles option arguments |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
22 * @param doProcess if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked. |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
23 * @param isLong TRUE if the option is a --long-format one |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
24 */ |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
25 static BOOL dmArgsProcessOpt( |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
26 char *currArg, int *argIndex, |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
27 int argc, char *argv[], |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
28 const DMOptArg opts[], int nopts, |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
29 BOOL (*handle_option)(int id, char *, char *), |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
30 BOOL doProcess, BOOL isLong) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
31 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
32 const DMOptArg *opt = NULL; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
33 char *optArg = NULL; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
34 int optIndex; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
35 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
36 for (optIndex = 0; optIndex < nopts; optIndex++) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
37 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
38 const DMOptArg *node = &opts[optIndex]; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
39 if (isLong && node->o_long != NULL) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
40 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
41 if (strcmp(currArg, node->o_long) == 0) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
42 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
43 opt = node; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
44 optArg = NULL; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
45 break; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
46 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
47 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
48 size_t len = strlen(node->o_long); |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
49 if (strncmp(currArg, node->o_long, len) == 0 && |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
50 currArg[len] == '=') |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
51 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
52 opt = node; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
53 optArg = (&currArg[len+1] != 0) ? &currArg[len+1] : NULL; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
54 break; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
55 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
56 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
57 else |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
58 if (!isLong && node->o_short != 0) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
59 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
60 if (*currArg == node->o_short) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
61 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
62 opt = node; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
63 optArg = (currArg[1] != 0) ? &currArg[1] : NULL; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
64 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
65 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
66 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
67 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
68 if (opt != NULL) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
69 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
70 // Check for the possible option argument |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
71 if ((opt->flags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
72 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
73 if (*argIndex < argc) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
74 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
75 (*argIndex)++; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
76 optArg = argv[*argIndex]; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
77 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
78 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
79 if (optArg == NULL) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
80 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
81 dmErrorMsg("Option '%s%s' requires an argument.\n", |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
82 isLong ? "--" : "-", |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
83 currArg); |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
84 return FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
85 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
86 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
87 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
88 // Option was given succesfully, try to process it |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
89 if (doProcess && !handle_option(opt->id, optArg, currArg)) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
90 return FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
91 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
92 else |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
93 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
94 dmErrorMsg("Unknown %s option '%s%s'\n", |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
95 isLong ? "long" : "short", |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
96 isLong ? "--" : "-", |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
97 currArg); |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
98 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
99 return FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
100 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
101 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
102 return TRUE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
103 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
104 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
105 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
106 /** |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
107 * Process given array of commandline arguments, handling short |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
108 * and long options by calling the respective callback functions. |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
109 * |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
110 * @param argc number of arguments |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
111 * @param argv argument list |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
112 * @param opts supported option list array |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
113 * @param nopts number of elements in the option list array |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
114 * @param handle_option callback function |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
115 * @param handle_other callback function |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
116 * @param flags processing flags |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
117 * @return return TRUE if all is well |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
118 */ |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
119 BOOL dmArgsProcess(int argc, char *argv[], |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
120 const DMOptArg *opts, const int nopts, |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
121 BOOL(*handle_option)(int id, char *, char *), |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
122 BOOL(*handle_other)(char *), const int flags) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
123 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
124 int argIndex, handleFlags = flags & OPTH_ONLY_MASK; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
125 BOOL optionsOK = TRUE, endOfOptions = FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
126 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
127 for (argIndex = 1; argIndex < argc; argIndex++) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
128 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
129 char *str = argv[argIndex]; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
130 if (*str == '-' && !endOfOptions) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
131 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
132 // Should we process options? |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
133 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
134 BOOL isLong; |
0 | 135 |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
136 str++; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
137 if (*str == '-') |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
138 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
139 // Check for "--", which ends the options-list |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
140 str++; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
141 if (*str == 0) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
142 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
143 endOfOptions = TRUE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
144 continue; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
145 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
146 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
147 // We have a long option |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
148 isLong = TRUE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
149 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
150 else |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
151 isLong = FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
152 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
153 if (!dmArgsProcessOpt(str, &argIndex, argc, argv, |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
154 opts, nopts, handle_option, doProcess, isLong)) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
155 optionsOK = FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
156 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
157 else |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
158 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
159 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
160 // Was not option argument |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
161 if (handle_other == NULL || |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
162 (handle_other != NULL && !handle_other(str))) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
163 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
164 dmErrorMsg("Invalid argument '%s'\n", str); |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
165 optionsOK = FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
166 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
167 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
168 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
169 // Check if we bail out on invalid argument |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
170 if (!optionsOK && (flags & OPTH_BAILOUT)) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
171 return FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
172 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
173 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
174 return optionsOK; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
175 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
176 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
177 |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
178 void dmPrintPad(FILE *fh, int count, const char och) |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
179 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
180 while (count--) |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
181 fputc(och, fh); |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
182 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
183 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
184 |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
185 /** |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
186 * Print given string indented in such a way that it is automatically |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
187 * line-wrapped as necessary, taking hard linefeeds into account as well. |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
188 * @param fh stdio file handle to output to |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
189 * @param str string to output |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
190 * @param spad starting pad/indent of the first line |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
191 * @param rpad how much to pad the other lines |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
192 * @param width total line width to wrap at |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
193 */ |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
194 void dmPrintWrap(FILE *fh, const char *str, |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
195 const int spad, int const rpad, const int width) |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
196 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
197 size_t pos = 0; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
198 BOOL first = TRUE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
199 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
200 while (str[pos]) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
201 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
202 // Pre-pad line |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
203 int linelen = first ? spad : rpad; |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
204 dmPrintPad(fh, first ? 0 : rpad, ' '); |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
205 first = FALSE; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
206 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
207 // Skip whitespace at line start |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
208 while (isspace(str[pos]) || str[pos] == '\n') |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
209 pos++; |
0 | 210 |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
211 // Handle each word |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
212 while (str[pos] && str[pos] != '\n') |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
213 { |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
214 size_t next; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
215 int wlen; |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
216 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
217 // Find word length and next break |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
218 for (wlen = 0, next = pos; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
219 str[next] && !isspace(str[next]) && |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
220 str[next] != '\n'; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
221 next++, wlen++); |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
222 |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
223 // Check if we have too much of text? |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
224 if (linelen + wlen >= width) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
225 break; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
226 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
227 // Print what we have |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
228 for (;pos < next; pos++, linelen++) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
229 fputc(str[pos], fh); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
230 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
231 // Check if we are at end of input or hard linefeed |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
232 if (str[next] == '\n' || str[next] == 0) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
233 break; |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
234 else |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
235 { |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
236 fputc(str[pos], fh); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
237 pos++; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
238 linelen++; |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
239 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
240 } |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
241 fprintf(fh, "\n"); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
242 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
243 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
244 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
245 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
246 static inline const char *dmArgsGetOptArg(const DMOptArg *opt) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
247 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
248 #ifdef DM_USE_OPT_ARG |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
249 return opt->o_arg != NULL ? opt->o_arg : "ARG"; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
250 #else |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
251 (void) opt; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
252 return "ARG"; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
253 #endif |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
254 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
255 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
256 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
257 static void dmArgsPrintHelpPrintItem(FILE *fh, const DMOptArg *opt, int *lineWidth, const int maxLineWidth, const BOOL doPrint) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
258 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
259 const char *arg = dmArgsGetOptArg(opt); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
260 char fmtBuf[32]; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
261 int padWidth; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
262 BOOL hasLongOpt = opt->o_long != NULL; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
263 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
264 if (opt->o_short != 0) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
265 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
266 if (!hasLongOpt && (opt->flags & OPT_ARGREQ)) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
267 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
268 snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>", |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
269 opt->o_short, arg); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
270 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
271 else |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
272 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
273 snprintf(fmtBuf, sizeof(fmtBuf), " -%c,", |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
274 opt->o_short); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
275 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
276 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
277 *lineWidth = strlen(fmtBuf); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
278 if (doPrint) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
279 padWidth = hasLongOpt ? 2 : maxLineWidth - *lineWidth; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
280 else |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
281 padWidth = 2; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
282 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
283 else |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
284 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
285 fmtBuf[0] = 0; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
286 *lineWidth = 0; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
287 padWidth = 4 + 2; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
288 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
289 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
290 if (doPrint) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
291 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
292 fputs(fmtBuf, fh); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
293 dmPrintPad(fh, padWidth, ' '); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
294 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
295 *lineWidth += padWidth; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
296 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
297 if (hasLongOpt) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
298 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
299 if (opt->flags & OPT_ARGREQ) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
300 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
301 snprintf(fmtBuf, sizeof(fmtBuf), "--%s=<%s>", |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
302 opt->o_long, arg); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
303 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
304 else |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
305 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
306 snprintf(fmtBuf, sizeof(fmtBuf), "--%s", |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
307 opt->o_long); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
308 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
309 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
310 *lineWidth += strlen(fmtBuf); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
311 } |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
312 else |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
313 fmtBuf[0] = 0; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
314 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
315 if (doPrint) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
316 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
317 padWidth = hasLongOpt ? maxLineWidth - *lineWidth : 0; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
318 *lineWidth += padWidth; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
319 |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
320 fputs(fmtBuf, fh); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
321 dmPrintPad(fh, padWidth, ' '); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
322 dmPrintWrap(fh, opt->desc, *lineWidth, *lineWidth, 79); |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
323 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
324 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
325 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
326 |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
327 /** |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
328 * Print help for commandline arguments/options |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
329 * @param fh stdio file handle to output to |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
330 * @param opts options list array |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
331 * @param nopts number of elements in options list array |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
332 * @param flags flags (currently unused) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
333 */ |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
334 void dmArgsPrintHelp(FILE *fh, const DMOptArg *opts, |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
335 const int nopts, const int flags) |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
336 { |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
337 int index, maxLineWidth; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
338 |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
339 (void) flags; |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
340 |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
341 // Determine width of the options and arguments |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
342 maxLineWidth = 0; |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
343 for (index = 0; index < nopts; index++) |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
344 { |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
345 int lineWidth = 0; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
346 dmArgsPrintHelpPrintItem(NULL, &opts[index], &lineWidth, 0, FALSE); |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
347 if (lineWidth > maxLineWidth) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
348 maxLineWidth = lineWidth; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
349 } |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
350 |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
351 maxLineWidth += 2; |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
352 |
2400
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
353 // Print out the formatted option list |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
354 for (index = 0; index < nopts; index++) |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
355 { |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
356 int lineWidth; |
01c6a5261962
Sync commandline argument help printing/handling changes from th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
2361
diff
changeset
|
357 dmArgsPrintHelpPrintItem(fh, &opts[index], &lineWidth, maxLineWidth, TRUE); |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
358 } |
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
958
diff
changeset
|
359 } |