comparison src/dmargs.c @ 2592:70b589a22495

Sync some changes from th-libs/th_args.c
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 30 Dec 2022 10:24:20 +0200
parents 9807ae37ad69
children
comparison
equal deleted inserted replaced
2591:b2c510f851dc 2592:70b589a22495
1 /* 1 /*
2 * Simple commandline argument processing 2 * Simple commandline argument processing
3 * Programmed and designed by Matti 'ccr' Hamalainen 3 * Programmed and designed by Matti 'ccr' Hamalainen
4 * (C) Copyright 2002-2021 Tecnic Software productions (TNSP) 4 * (C) Copyright 2002-2022 Tecnic Software productions (TNSP)
5 * 5 *
6 * Please read file 'COPYING' for information on license and distribution. 6 * Please read file 'COPYING' for information on license and distribution.
7 */ 7 */
8 /// @file 8 /// @file
9 /// @brief Simple commandline argument processing functions 9 /// @brief Simple commandline argument processing functions
11 11
12 12
13 /** 13 /**
14 * Parse and optionally handle the given long or short option argument. 14 * Parse and optionally handle the given long or short option argument.
15 * @param currArg current argument string 15 * @param currArg current argument string
16 * @param argIndex pointer to index of current argument in argv[] 16 * @param[in,out] argIndex pointer to index of current argument in argv[]
17 * @param argc number of arguments 17 * @param argc number of arguments
18 * @param argv argument string array 18 * @param argv argument string array
19 * @param opts options list array 19 * @param[in] opts options list array
20 * @param nopts number of elements in options list array 20 * @param[in] nopts number of elements in options list array
21 * @param handle_option function pointer to callback that handles option arguments 21 * @param[in] handle_option function pointer to callback that handles option arguments
22 * @param process if true, actually handle the argument, aka call the handle_option() function. if false, only validity of options are checked. 22 * @param[in] process if true, actually handle the argument, aka call the handle_option() function. if false, only validity of options are checked.
23 * @param isLong true if the option is a --long-format one 23 * @param[in] isLong true if the option is a --long-format one
24 * @returns returns @c true if option processing was successful
24 */ 25 */
25 static bool dmArgsProcessOpt( 26 static bool dmArgsProcessOpt(
26 char *currArg, int *argIndex, 27 char *currArg, int *argIndex,
27 int argc, char *argv[], 28 int argc, char *argv[],
28 const DMOptArg opts[], int nopts, 29 const DMOptArg opts[], int nopts,
106 * Process given array of commandline arguments, handling short 107 * Process given array of commandline arguments, handling short
107 * and long options by calling the respective callback functions. 108 * and long options by calling the respective callback functions.
108 * 109 *
109 * @param argc number of arguments 110 * @param argc number of arguments
110 * @param argv argument list 111 * @param argv argument list
111 * @param opts supported option list array 112 * @param[in] opts supported option list array
112 * @param nopts number of elements in the option list array 113 * @param[in] nopts number of elements in the option list array
113 * @param handle_option callback function 114 * @param[in] handle_option callback function
114 * @param handle_other callback function 115 * @param[in] handle_other callback function
115 * @param flags processing flags 116 * @param[in] flags processing flags
116 * @return return true if all is well 117 * @return returns @c true if arguments were processed successfully
117 */ 118 */
118 bool dmArgsProcess(int argc, char *argv[], 119 bool dmArgsProcess(int argc, char *argv[],
119 const DMOptArg *opts, const int nopts, 120 const DMOptArg *opts, const int nopts,
120 bool(*handle_option)(int id, char *, char *), 121 bool(*handle_option)(int id, char *, char *),
121 bool(*handle_other)(char *), const int flags) 122 bool(*handle_other)(char *), const int flags)
182 183
183 /** 184 /**
184 * Print given string indented in such a way that it is automatically 185 * Print given string indented in such a way that it is automatically
185 * line-wrapped as necessary, taking hard linefeeds into account as well. 186 * line-wrapped as necessary, taking hard linefeeds into account as well.
186 * @param fh stdio file handle to output to 187 * @param fh stdio file handle to output to
187 * @param spad starting pad/indent of the first line 188 * @param[in] spad starting pad/indent of the first line
188 * @param rpad how much to pad the other lines 189 * @param[in] rpad how much to pad the other lines
189 * @param width total line width to wrap at 190 * @param[in] width total line width to wrap at
190 * @param str string to output 191 * @param[in] str string to output
191 */ 192 */
192 void dmPrintWrap(FILE *fh, const int spad, int const rpad, 193 void dmPrintWrap(FILE *fh, const int spad, int const rpad,
193 const int width, const char *str) 194 const int width, const char *str)
194 { 195 {
195 size_t pos = 0; 196 size_t pos = 0;
268 snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>", 269 snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>",
269 opt->o_short, arg); 270 opt->o_short, arg);
270 } 271 }
271 else 272 else
272 { 273 {
273 snprintf(fmtBuf, sizeof(fmtBuf), " -%c,", 274 snprintf(fmtBuf, sizeof(fmtBuf), " -%c%s",
274 opt->o_short); 275 opt->o_short,
276 hasLongOpt ? "," : "");
275 } 277 }
276 278
277 *optWidth = strlen(fmtBuf); 279 *optWidth = strlen(fmtBuf);
278 if (doPrint) 280 if (doPrint)
279 padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth; 281 padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth;
323 } 325 }
324 } 326 }
325 327
326 328
327 /** 329 /**
328 * Print help for commandline arguments/options 330 * Print neatly formatted help for specified array of commandline options.
331 * The function will attempt to find the best indentation and formatting of
332 * the output using a simple fitting algorithm. Option descriptions will be
333 * automatically split over several lines if necessary.
329 * @param fh stdio file handle to output to 334 * @param fh stdio file handle to output to
330 * @param opts options list array 335 * @param[in] opts options list array
331 * @param nopts number of elements in options list array 336 * @param[in] nopts number of elements in the options list array @p opts
332 * @param flags flags (currently unused) 337 * @param[in] flags (currently unused)
333 * @param width width of the terminal or desired width of the print out 338 * @param[in] width width of the output terminal or the desired maximum width of the printout
334 */ 339 */
335 void dmArgsPrintHelp(FILE *fh, const DMOptArg *opts, 340 void dmArgsPrintHelp(FILE *fh, const DMOptArg *opts,
336 const int nopts, const int flags, const int width) 341 const int nopts, const int flags, const int width)
337 { 342 {
338 int index, maxOptWidth; 343 int index, maxOptWidth;