Mercurial > hg > th-libs
annotate th_args.c @ 757:2ab2fece83ea
Add th_llist_length_slow().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 01 Feb 2023 14:09:25 +0200 |
parents | 56594b498180 |
children |
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 |
726 | 4 * (C) Copyright 2002-2022 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 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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. |
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
23 * @param[in] isLong true if the option is a --long-format one |
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 */ |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 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, |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
30 bool (*handle_option)(int id, char *, char *), |
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 35 |
634 | 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 | 46 } |
39 | 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 | 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 | 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 | 65 } |
136 | 66 } |
39 | 67 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
68 if (opt != NULL) |
39 | 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); |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 89 if (process && !handle_option(opt->id, optArg, currArg)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
90 return false; |
39 | 91 } |
92 else | |
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 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
99 return false; |
10 | 100 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 */ |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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, |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
121 bool(*handle_option)(int id, char *, char *), |
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 124 int handleFlags = flags & OPTH_ONLY_MASK; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 127 for (int argIndex = 1; argIndex < argc; argIndex++) |
39 | 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 | 131 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
132 // Should we process options? |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
133 bool process = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; |
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 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 | 142 { |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 145 } |
39 | 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 |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
148 isLong = true; |
39 | 149 } |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
150 else |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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 | 154 opts, nopts, handle_option, process, isLong)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
155 optionsOK = false; |
39 | 156 } |
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 | 159 { |
128 | 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 | 163 { |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
164 optionsOK = false; |
10 | 165 } |
166 } | |
39 | 167 |
128 | 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)) |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
170 return false; |
10 | 171 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
172 |
10 | 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 | 184 /** |
185 * Print given string indented in such a way that it is automatically | |
186 * line-wrapped as necessary, taking hard linefeeds into account as well. | |
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 | 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; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
197 bool first = true; |
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
|
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, ' '); |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
204 first = false; |
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
|
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, |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
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; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
263 bool hasLongOpt = opt->o_long != NULL; |
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
|
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 { |
750
56594b498180
Adjust short option cosmetics in help.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
274 snprintf(fmtBuf, sizeof(fmtBuf), " -%c%s", |
56594b498180
Adjust short option cosmetics in help.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
275 opt->o_short, |
56594b498180
Adjust short option cosmetics in help.
Matti Hamalainen <ccr@tnsp.org>
parents:
735
diff
changeset
|
276 hasLongOpt ? "," : ""); |
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
|
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 |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
279 *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
|
280 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
|
281 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
|
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; |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
288 *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
|
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 } |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
297 *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
|
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 |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
312 *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
|
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 { |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
319 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
|
320 *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
|
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, ' '); |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
324 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
|
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 /** |
622
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
616
diff
changeset
|
330 * Print neatly formatted help for specified array of commandline options. |
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
616
diff
changeset
|
331 * 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
|
332 * 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
|
333 * 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
|
334 * @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
|
335 * @param[in] opts options list array |
622
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
616
diff
changeset
|
336 * @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
|
337 * @param[in] flags (currently unused) |
6d99150a8f89
Some more slight Doxygenisation.
Matti Hamalainen <ccr@tnsp.org>
parents:
616
diff
changeset
|
338 * @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
|
339 */ |
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
|
340 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
|
341 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
|
342 { |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
343 int index, maxOptWidth; |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
344 (void) flags; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
345 |
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
|
346 // 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
|
347 maxOptWidth = 0; |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
348 for (index = 0; index < nopts; index++) |
39 | 349 { |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
350 int optWidth = 0; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
351 th_args_help_print_item(NULL, &opts[index], &optWidth, 0, width, false); |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
352 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
|
353 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
|
354 } |
39 | 355 |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
356 maxOptWidth += 2; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
357 |
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
|
358 // 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
|
359 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
|
360 { |
590
041c8d3030e0
Add new argument to th_args_help() to specify the desired print width.
Matti Hamalainen <ccr@tnsp.org>
parents:
588
diff
changeset
|
361 int optWidth; |
735
31bc1ed07cf5
Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
Matti Hamalainen <ccr@tnsp.org>
parents:
726
diff
changeset
|
362 th_args_help_print_item(fh, &opts[index], &optWidth, maxOptWidth, width, true); |
10 | 363 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
364 } |