Mercurial > hg > index.cgi
comparison lwlib/lw_cmdline.c @ 5:0e01d1343c02
Added basic --help routine to lw_cmdline
author | lost@l-w.ca |
---|---|
date | Fri, 21 Jan 2011 23:19:19 -0700 |
parents | a4812d57ed13 |
children | 1e5e8ec406fb |
comparison
equal
deleted
inserted
replaced
4:a4812d57ed13 | 5:0e01d1343c02 |
---|---|
131 /* clean up scratch lists */ | 131 /* clean up scratch lists */ |
132 lw_free(slist); | 132 lw_free(slist); |
133 lw_free(llist); | 133 lw_free(llist); |
134 } | 134 } |
135 | 135 |
136 static void lw_cmdline_help(struct lw_cmdline_parser *parser) | 136 static void lw_cmdline_help(struct lw_cmdline_parser *parser, char *name) |
137 { | 137 { |
138 printf("TODO: help\n"); | 138 struct lw_cmdline_options **llist; |
139 int nopt; | |
140 int i; | |
141 int t; | |
142 char *tstr; | |
143 | |
144 tstr = parser -> doc; | |
145 for (nopt = 0; parser -> options[nopt].name; nopt++) | |
146 /* do nothing */ ; | |
147 | |
148 llist = lw_alloc(sizeof(struct lw_cmdline_options *) * (nopt + 3)); | |
149 | |
150 for (i = 0; i < nopt; i++) | |
151 { | |
152 llist[i] = &(parser -> options[i]); | |
153 } | |
154 | |
155 /* now sort the list */ | |
156 | |
157 /* now append the automatic options */ | |
158 llist[nopt] = &(builtin[0]); | |
159 llist[nopt + 1] = &(builtin[1]); | |
160 llist[nopt + 2] = &(builtin[2]); | |
161 | |
162 /* print brief usage */ | |
163 printf("Usage: %s [OPTION...] %s\n", name, parser -> args_doc ? parser -> args_doc : ""); | |
164 if (tstr) | |
165 { | |
166 while (*tstr && *tstr != '\v') | |
167 fputc(*tstr++, stdout); | |
168 if (*tstr) | |
169 tstr++; | |
170 } | |
171 fputc('\n', stdout); | |
172 fputc('\n', stdout); | |
173 | |
174 /* display options - do it the naïve way for now */ | |
175 for (i = 0; i < (nopt + 3); i++) | |
176 { | |
177 if (llist[i] -> key > 0x20 && llist[i] -> key < 0x7F) | |
178 { | |
179 printf(" -%c, ", llist[i] -> key); | |
180 } | |
181 else | |
182 { | |
183 printf(" "); | |
184 } | |
185 | |
186 printf("--%s", llist[i] -> name); | |
187 if (llist[i] -> arg) | |
188 { | |
189 if (llist[i] -> flags & lw_cmdline_opt_optional) | |
190 { | |
191 printf("[=%s]", llist[i] -> arg); | |
192 } | |
193 else | |
194 { | |
195 printf("=%s", llist[i] -> arg); | |
196 } | |
197 } | |
198 if (llist[i] -> doc) | |
199 { | |
200 printf("\t\t%s", llist[i] -> doc); | |
201 } | |
202 fputc('\n', stdout); | |
203 } | |
204 | |
205 printf("\nMandatory or optional arguments to long options are also mandatory or optional\nfor any corresponding short options.\n"); | |
206 | |
207 if (*tstr) | |
208 { | |
209 printf("\n%s\n", tstr); | |
210 } | |
211 | |
212 /* clean up scratch lists */ | |
213 lw_free(llist); | |
139 } | 214 } |
140 | 215 |
141 int lw_cmdline_parse(struct lw_cmdline_parser *parser, int argc, char **argv, unsigned flags, int *arg_index, void *input) | 216 int lw_cmdline_parse(struct lw_cmdline_parser *parser, int argc, char **argv, unsigned flags, int *arg_index, void *input) |
142 { | 217 { |
143 int i, j, r; | 218 int i, j, r; |
283 } | 358 } |
284 r = (*(parser -> parser))(lw_cmdline_key_end, NULL, input); | 359 r = (*(parser -> parser))(lw_cmdline_key_end, NULL, input); |
285 return r; | 360 return r; |
286 | 361 |
287 do_help: | 362 do_help: |
288 lw_cmdline_help(parser); | 363 lw_cmdline_help(parser, argv[0]); |
289 exit(0); | 364 exit(0); |
290 | 365 |
291 do_version: | 366 do_version: |
292 printf("%s\n", parser -> program_version); | 367 printf("%s\n", parser -> program_version); |
293 exit(0); | 368 exit(0); |