annotate th_args.c @ 633:e3c4d8e28695

Adjust options help width check, it was off-by-one.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 20 Jan 2020 17:02:06 +0200
parents 6d99150a8f89
children c09b068ecb32
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
553
3a852e9f70a6 Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 481
diff changeset
4 * (C) Copyright 2002-2020 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 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "th_args.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "th_string.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
13 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
14 * Parse and optionally handle the given long or short option argument.
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
15 * @param currArg current argument string
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
16 * @param[in,out] argIndex pointer to index of current argument in argv[]
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
17 * @param argc number of arguments
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
18 * @param argv argument string array
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
19 * @param[in] opts options list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
20 * @param[in] nopts number of elements in options list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
21 * @param[in] handle_option function pointer to callback that handles option arguments
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
22 * @param[in] doProcess if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked.
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
23 * @param[in] isLong TRUE if the option is a --long-format one
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
24 * @returns returns @c TRUE if option processing was successful
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
26 static BOOL th_args_process_opt(
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
27 char *currArg, int *argIndex,
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
28 int argc, char *argv[],
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
29 const th_optarg opts[], int nopts,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
30 BOOL (*handle_option)(int id, char *, char *),
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
31 BOOL doProcess, BOOL isLong)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
33 const th_optarg *opt = NULL;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
34 char *optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
35 int optIndex;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
36
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
37 for (optIndex = 0; optIndex < nopts; optIndex++)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
38 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
39 const th_optarg *node = &opts[optIndex];
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
40 if (isLong && node->o_long != NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
41 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
42 if (strcmp(currArg, node->o_long) == 0)
142
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 = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
46 break;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
47 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
48
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
49 size_t len = strlen(node->o_long);
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
50 if (strncmp(currArg, node->o_long, len) == 0 &&
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
51 currArg[len] == '=')
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
52 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
53 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
54 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
55 break;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
56 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
57 }
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
58 else
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
59 if (!isLong && node->o_short != 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
60 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
61 if (*currArg == node->o_short)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
62 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
63 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
64 optArg = (currArg[1] != 0) ? &currArg[1] : NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
65 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
66 }
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
67 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
68
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
69 if (opt != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
70 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
71 // Check for the possible option argument
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
72 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
73 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
74 if (*argIndex < argc)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
75 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
76 (*argIndex)++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
77 optArg = argv[*argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
78 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
79
173
503f0cb98775 Fix handling of option arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
80 if (optArg == NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
81 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
82 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
83 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
84 currArg);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
85 return FALSE;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
86 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
87 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
88
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
89 // Option was given succesfully, try to process it
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
90 if (doProcess && !handle_option(opt->id, optArg, currArg))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
91 return FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
92 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
93 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
94 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
95 THERR("Unknown %s option '%s%s'\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
96 isLong ? "long" : "short",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
97 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
98 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
99
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
100 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
101 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
103 return TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
107 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
108 * Process given array of commandline arguments, handling short
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
109 * and long options by calling the respective callback functions.
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
110 *
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
111 * @param argc number of arguments
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
112 * @param argv argument list
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
113 * @param[in] opts supported option list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
114 * @param[in] nopts number of elements in the option list array
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
115 * @param[in] handle_option callback function
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
116 * @param[in] handle_other callback function
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
117 * @param[in] flags processing flags
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
118 * @return returns @c TRUE if arguments were processed successfully
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
120 BOOL th_args_process(int argc, char *argv[],
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
121 const th_optarg *opts, const int nopts,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
122 BOOL(*handle_option)(int id, char *, char *),
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
123 BOOL(*handle_other)(char *), const int flags)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
125 int argIndex, handleFlags = flags & OPTH_ONLY_MASK;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
126 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
127
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
128 for (argIndex = 1; argIndex < argc; argIndex++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
129 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
130 char *str = argv[argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
131 if (*str == '-' && !endOfOptions)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
132 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
133 // Should we process options?
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
134 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
135 BOOL isLong;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
136
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
137 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
138 if (*str == '-')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
139 {
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
140 // 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
141 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
142 if (*str == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
143 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
144 endOfOptions = TRUE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
145 continue;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
146 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
147
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
148 // We have a long option
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
149 isLong = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
150 }
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
151 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
152 isLong = FALSE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
153
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
154 if (!th_args_process_opt(str, &argIndex, argc, argv,
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
155 opts, nopts, handle_option, doProcess, isLong))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
156 optionsOK = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
157 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
158 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
159 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
160 {
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
161 // Was not option argument
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
162 if (handle_other == NULL ||
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
163 (handle_other != NULL && !handle_other(str)))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
164 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
165 THERR("Invalid argument '%s'\n", str);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
166 optionsOK = FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
167 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
168 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
169
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
170 // Check if we bail out on invalid argument
137
0f43a94516f4 Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
171 if (!optionsOK && (flags & OPTH_BAILOUT))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
172 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
173 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
175 return optionsOK;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178
584
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
179 void th_print_pad(FILE *fh, int count, const char och)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
180 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
181 while (count--)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
182 fputc(och, fh);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
183 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
184
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
185
585
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
186 /**
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
187 * Print given string indented in such a way that it is automatically
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
188 * line-wrapped as necessary, taking hard linefeeds into account as well.
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
189 * @param fh stdio file handle to output to
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
190 * @param[in] spad starting pad/indent of the first line
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
191 * @param[in] rpad how much to pad the other lines
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
192 * @param[in] width total line width to wrap at
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
193 * @param[in] str string to output
585
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
194 */
588
ef71ef9b5862 Change th_print_wrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 587
diff changeset
195 void th_print_wrap(FILE *fh, const int spad, int const rpad,
ef71ef9b5862 Change th_print_wrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 587
diff changeset
196 const int width, const char *str)
584
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
197 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
198 size_t pos = 0;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
199 BOOL first = TRUE;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
200
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
201 while (str[pos])
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
202 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
203 // Pre-pad line
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
204 int linelen = first ? spad : rpad;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
205 th_print_pad(fh, first ? 0 : rpad, ' ');
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
206 first = FALSE;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
207
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
208 // Skip whitespace at line start
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
209 while (th_isspace(str[pos]) || str[pos] == '\n')
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
210 pos++;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
211
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
212 // Handle each word
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
213 while (str[pos] && str[pos] != '\n')
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
214 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
215 size_t next;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
216 int wlen;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
217
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
218 // Find word length and next break
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
219 for (wlen = 0, next = pos;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
220 str[next] && !th_isspace(str[next]) &&
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
221 str[next] != '\n';
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
222 next++, wlen++);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
223
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
224 // Check if we have too much of text?
633
e3c4d8e28695 Adjust options help width check, it was off-by-one.
Matti Hamalainen <ccr@tnsp.org>
parents: 622
diff changeset
225 if (linelen + wlen > width)
584
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
226 break;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
227
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
228 // Print what we have
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
229 for (;pos < next; pos++, linelen++)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
230 fputc(str[pos], fh);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
231
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
232 // Check if we are at end of input or hard linefeed
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
233 if (str[next] == '\n' || str[next] == 0)
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
234 break;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
235 else
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
236 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
237 fputc(str[pos], fh);
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
238 pos++;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
239 linelen++;
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
240 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
241 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
242 fprintf(fh, "\n");
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
243 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
244 }
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
245
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
246
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
247 static inline const char *th_args_get_optarg(const th_optarg *opt)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
248 {
587
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
249 #ifdef TH_USE_OPT_ARG
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
250 return opt->o_arg != NULL ? opt->o_arg : "ARG";
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
251 #else
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
252 (void) opt;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
253 return "ARG";
587
98812eb78d8e Add new compile-time optional th_optarg struct field o_arg for specifying the option argument description/name. TH_USE_OPT_ARG must be defined at compile time.
Matti Hamalainen <ccr@tnsp.org>
parents: 586
diff changeset
254 #endif
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
255 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
256
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
257
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
258 static void th_args_help_print_item(FILE *fh, const th_optarg *opt,
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
259 int *optWidth, const int maxOptWidth, const int termWidth,
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
260 const BOOL doPrint)
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
261 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
262 const char *arg = th_args_get_optarg(opt);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
263 char fmtBuf[32];
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
264 int padWidth;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
265 BOOL hasLongOpt = opt->o_long != NULL;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
266
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
267 if (opt->o_short != 0)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
268 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
269 if (!hasLongOpt && (opt->flags & OPT_ARGREQ))
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
270 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
271 snprintf(fmtBuf, sizeof(fmtBuf), " -%c <%s>",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
272 opt->o_short, arg);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
273 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
274 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
275 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
276 snprintf(fmtBuf, sizeof(fmtBuf), " -%c,",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
277 opt->o_short);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
278 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
279
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
280 *optWidth = strlen(fmtBuf);
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
281 if (doPrint)
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
282 padWidth = hasLongOpt ? 2 : maxOptWidth - *optWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
283 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
284 padWidth = 2;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
285 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
286 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
287 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
288 fmtBuf[0] = 0;
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
289 *optWidth = 0;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
290 padWidth = 4 + 2;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
291 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
292
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
293 if (doPrint)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
294 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
295 fputs(fmtBuf, fh);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
296 th_print_pad(fh, padWidth, ' ');
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
297 }
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
298 *optWidth += padWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
299
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
300 if (hasLongOpt)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
301 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
302 if (opt->flags & OPT_ARGREQ)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
303 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
304 snprintf(fmtBuf, sizeof(fmtBuf), "--%s=<%s>",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
305 opt->o_long, arg);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
306 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
307 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
308 {
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
309 snprintf(fmtBuf, sizeof(fmtBuf), "--%s",
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
310 opt->o_long);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
311 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
312
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
313 *optWidth += strlen(fmtBuf);
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
314 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
315 else
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
316 fmtBuf[0] = 0;
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
317
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
318 if (doPrint)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
319 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
320 padWidth = hasLongOpt ? maxOptWidth - *optWidth : 0;
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
321 *optWidth += padWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
322
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
323 fputs(fmtBuf, fh);
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
324 th_print_pad(fh, padWidth, ' ');
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
325 th_print_wrap(fh, *optWidth, *optWidth, termWidth, opt->desc);
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
326 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
327 }
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
328
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
329
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
330 /**
622
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
331 * Print neatly formatted help for specified array of commandline options.
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
332 * The function will attempt to find the best indentation and formatting of
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
333 * the output using a simple fitting algorithm. Option descriptions will be
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
334 * automatically split over several lines if necessary.
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
335 * @param fh stdio file handle to output to
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
336 * @param[in] opts options list array
622
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
337 * @param[in] nopts number of elements in the options list array @p opts
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
338 * @param[in] flags (currently unused)
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
339 * @param[in] width width of the output terminal or the desired maximum width of the printout
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 */
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
341 void th_args_help(FILE *fh, const th_optarg *opts,
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
342 const int nopts, const int flags, const int width)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
344 int index, maxOptWidth;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
345 (void) flags;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
347 // Determine width of the options and arguments
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
348 maxOptWidth = 0;
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
349 for (index = 0; index < nopts; index++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
350 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
351 int optWidth = 0;
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
352 th_args_help_print_item(NULL, &opts[index], &optWidth, 0, width, FALSE);
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
353 if (optWidth > maxOptWidth)
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
354 maxOptWidth = optWidth;
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
355 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
356
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
357 maxOptWidth += 2;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358
586
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
359 // Print out the formatted option list
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
360 for (index = 0; index < nopts; index++)
446203207cba Refactor th_args_help() to be more flexible how it works and prints out the option list.
Matti Hamalainen <ccr@tnsp.org>
parents: 585
diff changeset
361 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
362 int optWidth;
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
363 th_args_help_print_item(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
364 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 }