Mercurial > hg > th-libs
annotate th_args.c @ 454:347bfd3e017e
Bump copyright years.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 02 Jan 2018 22:57:02 +0200 |
parents | efd33accdc81 |
children | 85fa3d333556 |
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 |
454 | 4 * (C) Copyright 2002-2018 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 |
50 | 10 #ifndef TH_EXTERNAL |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 #include "th_util.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
12 #include "th_args.h" |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 #include "th_string.h" |
49
598609fb49b0
Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
47
diff
changeset
|
14 #endif |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
15 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
436
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
17 /** |
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
18 * 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
|
19 * @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
|
20 * @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
|
21 * @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
|
22 * @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
|
23 * @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
|
24 * @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
|
25 * @param handle_option function pointer to callback that handles option arguments |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
26 * @param doProcess if true, actually handle the argument, aka call the handle_option() function. if false, only validity of options are checked. |
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
27 * @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
|
28 */ |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
29 static bool th_args_process_opt( |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
30 char *currArg, int *argIndex, |
136 | 31 int argc, char *argv[], |
380
ac10155d2b4a
Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents:
379
diff
changeset
|
32 const th_optarg opts[], int nopts, |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
33 bool (*handle_option)(int id, char *, char *), |
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
34 bool doProcess, bool isLong) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 { |
380
ac10155d2b4a
Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents:
379
diff
changeset
|
36 const th_optarg *opt = NULL; |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
37 char *optArg = NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
38 int optIndex; |
39 | 39 |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
40 for (optIndex = 0; optIndex < nopts; optIndex++) |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
41 { |
380
ac10155d2b4a
Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents:
379
diff
changeset
|
42 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
|
43 if (isLong && node->o_long != NULL) |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
44 { |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
45 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
|
46 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
47 opt = node; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
48 optArg = NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
49 break; |
10 | 50 } |
39 | 51 |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
52 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
|
53 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
|
54 currArg[len] == '=') |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
55 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
56 opt = node; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
57 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
|
58 break; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
59 } |
10 | 60 } |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
61 else |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
62 if (!isLong && node->o_short != 0) |
39 | 63 { |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
64 if (*currArg == node->o_short) |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
65 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
66 opt = node; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
67 optArg = (currArg[1] != 0) ? &currArg[1] : NULL; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
68 } |
39 | 69 } |
136 | 70 } |
39 | 71 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
72 if (opt != NULL) |
39 | 73 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
74 // Check for the possible option argument |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
75 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
|
76 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
77 if (*argIndex < argc) |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
78 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
79 (*argIndex)++; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
80 optArg = argv[*argIndex]; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
81 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
436
diff
changeset
|
82 |
173
503f0cb98775
Fix handling of option arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
161
diff
changeset
|
83 if (optArg == NULL) |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
84 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
85 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
|
86 isLong ? "--" : "-", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
87 currArg); |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
88 return false; |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
89 } |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
90 } |
441
2991e6b52d95
Get rid of trailing whitespace.
Matti Hamalainen <ccr@tnsp.org>
parents:
436
diff
changeset
|
91 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
92 // 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
|
93 if (doProcess && !handle_option(opt->id, optArg, currArg)) |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
94 return false; |
39 | 95 } |
96 else | |
97 { | |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
98 THERR("Unknown %s option '%s%s'\n", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
99 isLong ? "long" : "short", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
100 isLong ? "--" : "-", |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
101 currArg); |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
102 |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
103 return false; |
10 | 104 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
105 |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
106 return true; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
108 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
109 |
436
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
110 /** |
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
111 * 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
|
112 * 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
|
113 * |
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
114 * @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
|
115 * @param argv argument list |
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
116 * @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
|
117 * @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
|
118 * @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
|
119 * @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
|
120 * @param flags processing flags |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
121 * @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
|
122 */ |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
123 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
|
124 const th_optarg *opts, const int nopts, |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
125 bool(*handle_option)(int id, char *, char *), |
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
126 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
|
127 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
128 int argIndex, handleFlags = flags & OPTH_ONLY_MASK; |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
129 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
|
130 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
131 for (argIndex = 1; argIndex < argc; argIndex++) |
39 | 132 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
133 char *str = argv[argIndex]; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
134 if (*str == '-' && !endOfOptions) |
39 | 135 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
136 // Should we process options? |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
137 bool doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; |
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
138 bool isLong; |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
139 |
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 == '-') |
39 | 142 { |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
143 // 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
|
144 str++; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
145 if (*str == 0) |
39 | 146 { |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
147 endOfOptions = true; |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
148 continue; |
10 | 149 } |
39 | 150 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
151 // We have a long option |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
152 isLong = true; |
39 | 153 } |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
154 else |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
155 isLong = false; |
139
4ca0af6dbcf8
Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
138
diff
changeset
|
156 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
157 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
|
158 opts, nopts, handle_option, doProcess, isLong)) |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
159 optionsOK = false; |
39 | 160 } |
161 else | |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
162 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0) |
39 | 163 { |
128 | 164 // Was not option argument |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
165 if (handle_other == NULL || |
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
166 (handle_other != NULL && !handle_other(str))) |
39 | 167 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
168 THERR("Invalid argument '%s'\n", str); |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
169 optionsOK = false; |
10 | 170 } |
171 } | |
39 | 172 |
128 | 173 // Check if we bail out on invalid argument |
137
0f43a94516f4
Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents:
136
diff
changeset
|
174 if (!optionsOK && (flags & OPTH_BAILOUT)) |
453
efd33accdc81
Break backwards compatibility by renaming BOOL, TRUE and FALSE to lowercase.
Matti Hamalainen <ccr@tnsp.org>
parents:
441
diff
changeset
|
175 return false; |
10 | 176 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 |
10 | 178 return optionsOK; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 } |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 |
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 |
436
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
182 /** |
9148bc3fa838
Begin adding Doxygen documentation for some things. Very rudimentary, and
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
183 * 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
|
184 * @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
|
185 * @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
|
186 * @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
|
187 * @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
|
188 */ |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
189 void th_args_help(FILE *fh, |
380
ac10155d2b4a
Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents:
379
diff
changeset
|
190 const th_optarg *opts, const int nopts, |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
191 const int flags) |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
192 { |
136 | 193 int index; |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
194 (void) flags; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
195 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
196 // Print out option list |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
197 for (index = 0; index < nopts; index++) |
39 | 198 { |
380
ac10155d2b4a
Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents:
379
diff
changeset
|
199 const th_optarg *opt = &opts[index]; |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
200 char tmpStr[128]; |
39 | 201 |
128 | 202 // Print short option |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
203 if (opt->o_short != 0) |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
204 { |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
205 snprintf(tmpStr, sizeof(tmpStr), |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
206 "-%c,", opt->o_short); |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
207 } |
10 | 208 else |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
209 tmpStr[0] = 0; |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
210 |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
211 fprintf(fh, " %-5s", tmpStr); |
39 | 212 |
128 | 213 // Print long option |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
214 if (opt->o_long != NULL) |
39 | 215 { |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
216 snprintf(tmpStr, sizeof(tmpStr), "--%s%s", |
379
f4e182720870
Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents:
173
diff
changeset
|
217 opt->o_long, |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
218 (opt->flags & OPT_ARGREQ) ? "=ARG" : ""); |
39 | 219 } |
220 else | |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
221 tmpStr[0] = 0; |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
222 |
142
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
223 fprintf(fh, "%-20s", tmpStr); |
0a4fd9cfb929
Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
141
diff
changeset
|
224 |
143 | 225 th_print_wrap(fh, opt->desc, 26, 26, th_term_width() - 2); |
10 | 226 } |
0
bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
227 } |