annotate th_args.c @ 673:1763d9b26a58

Do not show 'Invalid argument' message, assume the caller does it.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 24 Feb 2020 16:11:38 +0200
parents c5ce9a4bfc3e
children 4ca6a3b30fe8
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
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
22 * @param[in] process if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked.
616
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 *),
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
31 BOOL process, 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;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
35
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
36 for (int optIndex = 0; optIndex < nopts; optIndex++)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
37 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
38 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
39 if (isLong && node->o_long != NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
40 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
41 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
42 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
43 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
44 optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
45 break;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
46 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
47
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
48 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
49 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
50 currArg[len] == '=')
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
51 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
52 opt = node;
636
c5ce9a4bfc3e Fix the check for --long option argument.
Matti Hamalainen <ccr@tnsp.org>
parents: 634
diff changeset
53 optArg = (currArg[len+1] != 0) ? &currArg[len+1] : NULL;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
54 break;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
55 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
56 }
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
57 else
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
58 if (!isLong && node->o_short != 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
59 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
60 if (*currArg == node->o_short)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
61 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
62 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
63 optArg = (currArg[1] != 0) ? &currArg[1] : NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
64 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
65 }
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
66 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
67
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
68 if (opt != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
69 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
70 // Check for the possible option argument
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
71 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
72 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
73 if (*argIndex < argc)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
74 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
75 (*argIndex)++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
76 optArg = argv[*argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
77 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
78
173
503f0cb98775 Fix handling of option arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
79 if (optArg == NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
80 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
81 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
82 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
83 currArg);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
84 return FALSE;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
85 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
86 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
87
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
88 // Option was given succesfully, try to process it
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
89 if (process && !handle_option(opt->id, optArg, currArg))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
90 return FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
91 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
92 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
93 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
94 THERR("Unknown %s option '%s%s'\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
95 isLong ? "long" : "short",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
96 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
97 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
98
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
99 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
100 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
102 return TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 }
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
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
106 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
107 * 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
108 * 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
109 *
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
110 * @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
111 * @param argv argument list
616
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
112 * @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
113 * @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
114 * @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
115 * @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
116 * @param[in] flags processing flags
594f197f7005 Slight improvements to the almost non-existing Doxygen documentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 591
diff changeset
117 * @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
118 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
119 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
120 const th_optarg *opts, const int nopts,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
121 BOOL(*handle_option)(int id, char *, char *),
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
122 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
123 {
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
124 int handleFlags = flags & OPTH_ONLY_MASK;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
125 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
126
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
127 for (int argIndex = 1; argIndex < argc; argIndex++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
128 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
129 char *str = argv[argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
130 if (*str == '-' && !endOfOptions)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
131 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
132 // Should we process options?
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
133 BOOL process = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
134 BOOL isLong;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
135
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
136 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
137 if (*str == '-')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
138 {
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
139 // 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
140 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
141 if (*str == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
142 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
143 endOfOptions = TRUE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
144 continue;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
145 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
146
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
147 // We have a long option
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
148 isLong = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
149 }
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
150 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
151 isLong = FALSE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
152
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
153 if (!th_args_process_opt(str, &argIndex, argc, argv,
634
c09b068ecb32 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 633
diff changeset
154 opts, nopts, handle_option, process, isLong))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
155 optionsOK = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
156 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
157 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
158 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
159 {
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
160 // Was not option argument
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
161 if (handle_other == NULL ||
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
162 (handle_other != NULL && !handle_other(str)))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
163 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
164 optionsOK = FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
165 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
166 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
167
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
168 // Check if we bail out on invalid argument
137
0f43a94516f4 Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
169 if (!optionsOK && (flags & OPTH_BAILOUT))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
170 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
171 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
173 return optionsOK;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176
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
177 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
178 {
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 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
180 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
181 }
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
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
585
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
184 /**
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
185 * 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
186 * 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
187 * @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
188 * @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
189 * @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
190 * @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
191 * @param[in] str string to output
585
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
192 */
588
ef71ef9b5862 Change th_print_wrap() API and move the string argument last.
Matti Hamalainen <ccr@tnsp.org>
parents: 587
diff changeset
193 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
194 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
195 {
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
196 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
197 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
198
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 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
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 // 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
202 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
203 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
204 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
205
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 // 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
207 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
208 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
209
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 // 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
211 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
212 {
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 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
214 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
215
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 // 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
217 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
218 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
219 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
220 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
221
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 // 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
223 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
224 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
225
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 // 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
227 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
228 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
229
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 // 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
231 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
232 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
233 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
234 {
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 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
236 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
237 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
238 }
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 }
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 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
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 }
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
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
245 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
246 {
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
247 #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
248 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
249 #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
250 (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
251 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
252 #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
253 }
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
254
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
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
256 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
257 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
258 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
259 {
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
260 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
261 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
262 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
263 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
264
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 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
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 (!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
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 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
270 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
271 }
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 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
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 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
275 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
276 }
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
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
278 *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
279 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
280 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
281 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
282 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
283 }
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 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
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 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
287 *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
288 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
289 }
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
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 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
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 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
294 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
295 }
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
296 *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
297
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
298 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
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 (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
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 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
303 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
304 }
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 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
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 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
308 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
309 }
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
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
311 *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
312 }
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
313 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
314 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
315
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 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
317 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
318 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
319 *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
320
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
321 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
322 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
323 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
324 }
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
325 }
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
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
328 /**
622
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
329 * Print neatly formatted help for specified array of commandline options.
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
330 * 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
331 * 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
332 * 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
333 * @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
334 * @param[in] opts options list array
622
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
335 * @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
336 * @param[in] flags (currently unused)
6d99150a8f89 Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents: 616
diff changeset
337 * @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
338 */
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
339 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
340 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
341 {
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 int index, maxOptWidth;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
343 (void) flags;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344
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
345 // 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
346 maxOptWidth = 0;
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
347 for (index = 0; index < nopts; index++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
348 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
349 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
350 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
351 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
352 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
353 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
354
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
355 maxOptWidth += 2;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356
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
357 // 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
358 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
359 {
590
041c8d3030e0 Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents: 588
diff changeset
360 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
361 th_args_help_print_item(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
362 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 }