annotate th_args.c @ 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.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 12 Jan 2020 18:03:47 +0200
parents 446203207cba
children ef71ef9b5862
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 */
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
8 /// @file
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
9 /// @brief Simple commandline argument processing functions
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "th_args.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include "th_string.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
15 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
16 * 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
17 * @param currArg current argument string
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
18 * @param argIndex pointer to index of current argument in argv[]
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
19 * @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
20 * @param argv argument string array
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
21 * @param opts options list array
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
22 * @param nopts number of elements in options list array
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
23 * @param handle_option function pointer to callback that handles option arguments
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
24 * @param doProcess if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked.
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
25 * @param isLong TRUE if the option is a --long-format one
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
27 static BOOL th_args_process_opt(
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
28 char *currArg, int *argIndex,
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
29 int argc, char *argv[],
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
30 const th_optarg opts[], int nopts,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
31 BOOL (*handle_option)(int id, char *, char *),
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
32 BOOL doProcess, BOOL isLong)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
34 const th_optarg *opt = NULL;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
35 char *optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
36 int optIndex;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
37
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
38 for (optIndex = 0; optIndex < nopts; optIndex++)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
39 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
40 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
41 if (isLong && node->o_long != NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
42 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
43 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
44 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
45 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
46 optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
47 break;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
48 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
49
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
50 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
51 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
52 currArg[len] == '=')
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
53 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
54 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
55 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
56 break;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
57 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
58 }
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
59 else
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
60 if (!isLong && node->o_short != 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
61 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
62 if (*currArg == node->o_short)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
63 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
64 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
65 optArg = (currArg[1] != 0) ? &currArg[1] : NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
66 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
67 }
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
68 }
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 if (opt != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
71 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
72 // Check for the possible option argument
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
73 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
74 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
75 if (*argIndex < argc)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
76 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
77 (*argIndex)++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
78 optArg = argv[*argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
79 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
80
173
503f0cb98775 Fix handling of option arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
81 if (optArg == NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
82 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
83 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
84 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
85 currArg);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
86 return FALSE;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
87 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
88 }
441
2991e6b52d95 Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents: 436
diff changeset
89
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
90 // 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
91 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
92 return FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
93 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
94 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
95 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
96 THERR("Unknown %s option '%s%s'\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
97 isLong ? "long" : "short",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
98 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
99 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
100
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
101 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
102 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
104 return TRUE;
0
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
108 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
109 * 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
110 * 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
111 *
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
112 * @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
113 * @param argv argument list
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
114 * @param opts supported option list array
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
115 * @param nopts number of elements in the option list array
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
116 * @param handle_option callback function
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
117 * @param handle_other callback function
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
118 * @param flags processing flags
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
119 * @return return TRUE if all is well
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 */
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
121 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
122 const th_optarg *opts, const int nopts,
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
123 BOOL(*handle_option)(int id, char *, char *),
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
124 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
125 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
126 int argIndex, handleFlags = flags & OPTH_ONLY_MASK;
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
127 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
128
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
129 for (argIndex = 1; argIndex < argc; argIndex++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
130 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
131 char *str = argv[argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
132 if (*str == '-' && !endOfOptions)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
133 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
134 // Should we process options?
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
135 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
136 BOOL isLong;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
137
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
138 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
139 if (*str == '-')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
140 {
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
141 // 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
142 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
143 if (*str == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
144 {
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
145 endOfOptions = TRUE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
146 continue;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
147 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
148
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
149 // We have a long option
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
150 isLong = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
151 }
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
152 else
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
153 isLong = FALSE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
154
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
155 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
156 opts, nopts, handle_option, doProcess, isLong))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
157 optionsOK = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
158 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
159 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
160 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
161 {
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
162 // Was not option argument
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
163 if (handle_other == NULL ||
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
164 (handle_other != NULL && !handle_other(str)))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
165 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
166 THERR("Invalid argument '%s'\n", str);
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
167 optionsOK = FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
168 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
169 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
170
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
171 // Check if we bail out on invalid argument
137
0f43a94516f4 Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
172 if (!optionsOK && (flags & OPTH_BAILOUT))
457
85fa3d333556 Actually, revert the boolean changes .. meh.
Matti Hamalainen <ccr@tnsp.org>
parents: 454
diff changeset
173 return FALSE;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
174 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
176 return optionsOK;
0
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179
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
180 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
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 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
183 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
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
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
186
585
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
187 /**
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
188 * 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
189 * 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
190 * @param fh stdio file handle to output to
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
191 * @param str string to output
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
192 * @param spad starting pad/indent of the first line
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
193 * @param rpad how much to pad the other lines
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
194 * @param width total line width to wrap at
1a4359ecc5d5 Doxygen th_print_wrap().
Matti Hamalainen <ccr@tnsp.org>
parents: 584
diff changeset
195 */
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
196 void th_print_wrap(FILE *fh, const char *str,
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 const int spad, int const rpad, const int width)
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 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
200 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
201
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 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
203 {
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 // 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
205 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
206 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
207 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
208
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 // 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
210 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
211 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
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 // 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
214 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
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 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
217 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
218
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 // 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
220 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
221 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
222 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
223 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
224
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 // Check if we have too much of text?
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 if (linelen + wlen >= width)
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 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
228
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 // 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
230 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
231 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
232
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 // 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
234 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
235 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
236 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
237 {
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 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
239 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
240 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
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 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
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
390c66af09cf Move th_print_wrap() and th_print_pad() to th_args from th_string.
Matti Hamalainen <ccr@tnsp.org>
parents: 566
diff changeset
247
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
248 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
249 {
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
250 #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
251 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
252 #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
253 (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
254 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
255 #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
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
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
258
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 static void th_args_help_print_item(FILE *fh, const th_optarg *opt, int *lineWidth, const int maxLineWidth, const BOOL 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
260 {
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 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
262 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
263 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
264 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
265
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 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
267 {
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 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
269 {
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 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
271 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
272 }
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 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
274 {
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 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
276 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
277 }
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 *lineWidth = strlen(fmtBuf);
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
280 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
281 padWidth = hasLongOpt ? 2 : maxLineWidth - *lineWidth;
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 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
283 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
284 }
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 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
286 {
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 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
288 *lineWidth = 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
289 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
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
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 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
293 {
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 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
295 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
296 }
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 *lineWidth += 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
298
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 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
300 {
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 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
302 {
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 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
304 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
305 }
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 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
307 {
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 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
309 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
310 }
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 *lineWidth += strlen(fmtBuf);
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 }
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 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
315 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
316
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 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
318 {
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 padWidth = hasLongOpt ? maxLineWidth - *lineWidth : 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
320 *lineWidth += 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
321
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 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
323 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
324 th_print_wrap(fh, opt->desc, *lineWidth, *lineWidth, th_term_width() - 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
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
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
436
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
329 /**
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
330 * Print help for commandline arguments/options
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
331 * @param fh stdio file handle to output to
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
332 * @param opts options list array
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
333 * @param nopts number of elements in options list array
9148bc3fa838 Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
334 * @param flags flags (currently unused)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 */
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
336 void th_args_help(FILE *fh, const th_optarg *opts,
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
337 const int nopts, const int flags)
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 int index, maxLineWidth;
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
340
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
341 (void) flags;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342
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
343 // Determine width of the options and arguments
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
344 maxLineWidth = 0;
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
345 for (index = 0; index < nopts; index++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
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 int lineWidth = 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
348 th_args_help_print_item(NULL, &opts[index], &lineWidth, 0, FALSE);
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
349 if (lineWidth > maxLineWidth)
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
350 maxLineWidth = lineWidth;
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
351 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
352
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 maxLineWidth += 2;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354
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 // 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
356 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
357 {
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 int lineWidth;
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 th_args_help_print_item(fh, &opts[index], &lineWidth, maxLineWidth, TRUE);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
360 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 }