comparison src/dmargs.c @ 2402:b7cd5dd0b82e

Merge one more change from th-libs args processing.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 12 Jan 2020 20:50:45 +0200
parents 263093248f26
children 8d057a2459e3
comparison
equal deleted inserted replaced
2401:263093248f26 2402:b7cd5dd0b82e
252 return "ARG"; 252 return "ARG";
253 #endif 253 #endif
254 } 254 }
255 255
256 256
257 static void dmArgsPrintHelpPrintItem(FILE *fh, const DMOptArg *opt, int *lineWidth, const int maxLineWidth, const BOOL doPrint) 257 static void dmArgsPrintHelpPrintItem(FILE *fh, const DMOptArg *opt,
258 int *optWidth, const int maxOptWidth, const int termWidth,
259 const BOOL doPrint)
258 { 260 {
259 const char *arg = dmArgsGetOptArg(opt); 261 const char *arg = dmArgsGetOptArg(opt);
260 char fmtBuf[32]; 262 char fmtBuf[32];
261 int padWidth; 263 int padWidth;
262 BOOL hasLongOpt = opt->o_long != NULL; 264 BOOL hasLongOpt = opt->o_long != NULL;
272 { 274 {
273 snprintf(fmtBuf, sizeof(fmtBuf), " -%c,", 275 snprintf(fmtBuf, sizeof(fmtBuf), " -%c,",
274 opt->o_short); 276 opt->o_short);
275 } 277 }
276 278
277 *lineWidth = strlen(fmtBuf); 279 *optWidth = strlen(fmtBuf);
278 if (doPrint) 280 if (doPrint)
279 padWidth = hasLongOpt ? 2 : maxLineWidth - *lineWidth; 281 padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth;
280 else 282 else
281 padWidth = 2; 283 padWidth = 2;
282 } 284 }
283 else 285 else
284 { 286 {
285 fmtBuf[0] = 0; 287 fmtBuf[0] = 0;
286 *lineWidth = 0; 288 *optWidth = 0;
287 padWidth = 4 + 2; 289 padWidth = 4 + 2;
288 } 290 }
289 291
290 if (doPrint) 292 if (doPrint)
291 { 293 {
292 fputs(fmtBuf, fh); 294 fputs(fmtBuf, fh);
293 dmPrintPad(fh, padWidth, ' '); 295 dmPrintPad(fh, padWidth, ' ');
294 } 296 }
295 *lineWidth += padWidth; 297 *optWidth += padWidth;
296 298
297 if (hasLongOpt) 299 if (hasLongOpt)
298 { 300 {
299 if (opt->flags & OPT_ARGREQ) 301 if (opt->flags & OPT_ARGREQ)
300 { 302 {
305 { 307 {
306 snprintf(fmtBuf, sizeof(fmtBuf), "--%s", 308 snprintf(fmtBuf, sizeof(fmtBuf), "--%s",
307 opt->o_long); 309 opt->o_long);
308 } 310 }
309 311
310 *lineWidth += strlen(fmtBuf); 312 *optWidth += strlen(fmtBuf);
311 } 313 }
312 else 314 else
313 fmtBuf[0] = 0; 315 fmtBuf[0] = 0;
314 316
315 if (doPrint) 317 if (doPrint)
316 { 318 {
317 padWidth = hasLongOpt ? maxLineWidth - *lineWidth : 0; 319 padWidth = hasLongOpt ? maxOptWidth - *optWidth : 0;
318 *lineWidth += padWidth; 320 *optWidth += padWidth;
319 321
320 fputs(fmtBuf, fh); 322 fputs(fmtBuf, fh);
321 dmPrintPad(fh, padWidth, ' '); 323 dmPrintPad(fh, padWidth, ' ');
322 dmPrintWrap(fh, *lineWidth, *lineWidth, 79, opt->desc); 324 dmPrintWrap(fh, *optWidth, *optWidth, termWidth, opt->desc);
323 } 325 }
324 } 326 }
325 327
326 328
327 /** 329 /**
328 * Print help for commandline arguments/options 330 * Print help for commandline arguments/options
329 * @param fh stdio file handle to output to 331 * @param fh stdio file handle to output to
330 * @param opts options list array 332 * @param opts options list array
331 * @param nopts number of elements in options list array 333 * @param nopts number of elements in options list array
332 * @param flags flags (currently unused) 334 * @param flags flags (currently unused)
335 * @param width width of the terminal or desired width of the print out
333 */ 336 */
334 void dmArgsPrintHelp(FILE *fh, const DMOptArg *opts, 337 void dmArgsPrintHelp(FILE *fh, const DMOptArg *opts,
335 const int nopts, const int flags) 338 const int nopts, const int flags, const int width)
336 { 339 {
337 int index, maxLineWidth; 340 int index, maxOptWidth;
338
339 (void) flags; 341 (void) flags;
340 342
341 // Determine width of the options and arguments 343 // Determine width of the options and arguments
342 maxLineWidth = 0; 344 maxOptWidth = 0;
343 for (index = 0; index < nopts; index++) 345 for (index = 0; index < nopts; index++)
344 { 346 {
345 int lineWidth = 0; 347 int optWidth = 0;
346 dmArgsPrintHelpPrintItem(NULL, &opts[index], &lineWidth, 0, FALSE); 348 dmArgsPrintHelpPrintItem(NULL, &opts[index], &optWidth, 0, width, FALSE);
347 if (lineWidth > maxLineWidth) 349 if (optWidth > maxOptWidth)
348 maxLineWidth = lineWidth; 350 maxOptWidth = optWidth;
349 } 351 }
350 352
351 maxLineWidth += 2; 353 maxOptWidth += 2;
352 354
353 // Print out the formatted option list 355 // Print out the formatted option list
354 for (index = 0; index < nopts; index++) 356 for (index = 0; index < nopts; index++)
355 { 357 {
356 int lineWidth; 358 int optWidth;
357 dmArgsPrintHelpPrintItem(fh, &opts[index], &lineWidth, maxLineWidth, TRUE); 359 dmArgsPrintHelpPrintItem(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE);
358 } 360 }
359 } 361 }