Mercurial > hg > th-libs
annotate th_args.c @ 161:a765a51cbd5c
Make th_print_wrap() function public and move the code to th_util.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Feb 2015 18:41:00 +0200 |
parents | fdea1c3acc53 |
children | 503f0cb98775 |
rev | line source |
---|---|
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Simple commandline argument processing |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 * Programmed and designed by Matti 'ccr' Hamalainen |
144 | 4 * (C) Copyright 2002-2015 Tecnic Software productions (TNSP) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 * |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 * Please read file 'COPYING' for information on license and distribution. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 */ |
50 | 8 #ifndef TH_EXTERNAL |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 #include "th_util.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 #include "th_args.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 #include "th_string.h" |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
47
diff
changeset
|
12 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
15 /* Parse long and short options |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 */ |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
17 static BOOL th_args_process_opt( |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
18 char *currArg, int *argIndex, |
136 | 19 int argc, char *argv[], |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
20 const th_optarg_t opts[], int numOpts, |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
21 BOOL (*handleOptionCB)(int, char *, char *), |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
22 BOOL doProcess, BOOL isLong) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
24 const th_optarg_t *opt = NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
25 char *optArg = NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
26 int optIndex; |
39 | 27 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
28 for (optIndex = 0; optIndex < numOpts; optIndex++) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
29 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
30 const th_optarg_t *node = &opts[optIndex]; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
31 if (isLong && node->optLong != NULL) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
32 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
33 if (strcmp(currArg, node->optLong) == 0) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
34 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
35 opt = node; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
36 optArg = NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
37 break; |
10 | 38 } |
39 | 39 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
40 size_t len = strlen(node->optLong); |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
41 if (strncmp(currArg, node->optLong, len) == 0 && |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
42 currArg[len] == '=') |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
43 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
44 opt = node; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
45 optArg = (&currArg[len+1] != 0) ? &currArg[len+1] : NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
46 break; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
47 } |
10 | 48 } |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
49 else |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
50 if (!isLong && node->optShort != 0) |
39 | 51 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
52 if (*currArg == node->optShort) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
53 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
54 opt = node; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
55 optArg = (currArg[1] != 0) ? &currArg[1] : NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
56 } |
39 | 57 } |
136 | 58 } |
39 | 59 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
60 if (opt != NULL) |
39 | 61 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
62 // Check for the possible option argument |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
63 if ((opt->flags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
64 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
65 if (*argIndex < argc) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
66 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
67 (*argIndex)++; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
68 optArg = argv[*argIndex]; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
69 } |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
70 else |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
71 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
72 THERR("Option '%s%s' requires an argument.\n", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
73 isLong ? "--" : "-", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
74 currArg); |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
75 return FALSE; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
76 } |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
77 } |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
78 |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
79 // Option was given succesfully, try to process it |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
80 if (doProcess && !handleOptionCB(opt->id, optArg, currArg)) |
136 | 81 return FALSE; |
39 | 82 } |
83 else | |
84 { | |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
85 THERR("Unknown %s option '%s%s'\n", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
86 isLong ? "long" : "short", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
87 isLong ? "--" : "-", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
88 currArg); |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
89 |
10 | 90 return FALSE; |
91 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 |
10 | 93 return TRUE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 /* Process arguments, handling short and long options by |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 * calling the given callback functions. |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 */ |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 BOOL th_args_process(int argc, char *argv[], |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
101 const th_optarg_t *opts, const int numOpts, |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
102 BOOL(*handleOptionCB) (int, char *, char *), |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
103 BOOL(*handleOther) (char *), const int flags) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
104 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
105 int argIndex, handleFlags = flags & OPTH_ONLY_MASK; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
106 BOOL optionsOK = TRUE, endOfOptions = FALSE; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
108 for (argIndex = 1; argIndex < argc; argIndex++) |
39 | 109 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
110 char *str = argv[argIndex]; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
111 if (*str == '-' && !endOfOptions) |
39 | 112 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
113 // Should we process options? |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
114 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
115 BOOL isLong; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
116 |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
117 str++; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
118 if (*str == '-') |
39 | 119 { |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
120 // Check for "--", which ends the options-list |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
121 str++; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
122 if (*str == 0) |
39 | 123 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
124 endOfOptions = TRUE; |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
125 continue; |
10 | 126 } |
39 | 127 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
128 // We have a long option |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
129 isLong = TRUE; |
39 | 130 } |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
131 else |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
132 isLong = FALSE; |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
133 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
134 if (!th_args_process_opt(str, &argIndex, argc, argv, |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
135 opts, numOpts, handleOptionCB, doProcess, isLong)) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
136 optionsOK = FALSE; |
39 | 137 } |
138 else | |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
139 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0) |
39 | 140 { |
128 | 141 // Was not option argument |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
142 if (handleOther == NULL || |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
143 (handleOther != NULL && !handleOther(str))) |
39 | 144 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
145 THERR("Invalid argument '%s'\n", str); |
10 | 146 optionsOK = FALSE; |
147 } | |
148 } | |
39 | 149 |
128 | 150 // Check if we bail out on invalid argument |
137
0f43a94516f4
Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents:
136
diff
changeset
|
151 if (!optionsOK && (flags & OPTH_BAILOUT)) |
10 | 152 return FALSE; |
153 } | |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
154 |
10 | 155 return optionsOK; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
156 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 /* Print help for commandline arguments/options |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 */ |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
161 void th_args_help(FILE *fh, |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
162 const th_optarg_t *opts, const int numOpts, |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
163 const int flags) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
164 { |
136 | 165 int index; |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
166 (void) flags; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
167 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
168 // Print out option list |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
169 for (index = 0; index < numOpts; index++) |
39 | 170 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
171 const th_optarg_t *opt = &opts[index]; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
172 char tmpStr[128]; |
39 | 173 |
128 | 174 // Print short option |
136 | 175 if (opt->optShort != 0) |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
176 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
177 snprintf(tmpStr, sizeof(tmpStr), |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
178 "-%c,", opt->optShort); |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
179 } |
10 | 180 else |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
181 tmpStr[0] = 0; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
182 |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
183 fprintf(fh, " %-5s", tmpStr); |
39 | 184 |
128 | 185 // Print long option |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
186 if (opt->optLong != NULL) |
39 | 187 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
188 snprintf(tmpStr, sizeof(tmpStr), "--%s%s", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
189 opt->optLong, |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
190 (opt->flags & OPT_ARGREQ) ? "=ARG" : ""); |
39 | 191 } |
192 else | |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
193 tmpStr[0] = 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
195 fprintf(fh, "%-20s", tmpStr); |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
196 |
143 | 197 th_print_wrap(fh, opt->desc, 26, 26, th_term_width() - 2); |
10 | 198 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 } |