annotate th_args.c @ 161:a765a51cbd5c

Make th_print_wrap() function public and move the code to th_util.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Feb 2015 18:41:00 +0200
parents fdea1c3acc53
children 503f0cb98775
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
144
51eec969b07a Update copyrights.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
4 * (C) Copyright 2002-2015 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 */
50
1fa969d0f551 Sync changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 49
diff changeset
8 #ifndef TH_EXTERNAL
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "th_util.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include "th_args.h"
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "th_string.h"
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 47
diff changeset
12 #endif
0
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
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
15 /* Parse long and short options
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 */
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
17 static BOOL th_args_process_opt(
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
18 char *currArg, int *argIndex,
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
19 int argc, char *argv[],
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
20 const th_optarg_t opts[], int numOpts,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
21 BOOL (*handleOptionCB)(int, char *, char *),
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
22 BOOL doProcess, BOOL isLong)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
24 const th_optarg_t *opt = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
25 char *optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
26 int optIndex;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
27
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
28 for (optIndex = 0; optIndex < numOpts; optIndex++)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
29 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
30 const th_optarg_t *node = &opts[optIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
31 if (isLong && node->optLong != NULL)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
32 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
33 if (strcmp(currArg, node->optLong) == 0)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
34 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
35 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
36 optArg = NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
37 break;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
38 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
39
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
40 size_t len = strlen(node->optLong);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
41 if (strncmp(currArg, node->optLong, len) == 0 &&
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
42 currArg[len] == '=')
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
43 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
44 opt = node;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
45 optArg = (&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
46 break;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
47 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
48 }
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
49 else
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
50 if (!isLong && node->optShort != 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
51 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
52 if (*currArg == node->optShort)
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[1] != 0) ? &currArg[1] : NULL;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
56 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
57 }
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
58 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
59
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
60 if (opt != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
61 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
62 // Check for the possible option argument
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
63 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
64 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
65 if (*argIndex < argc)
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
66 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
67 (*argIndex)++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
68 optArg = argv[*argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
69 }
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
70 else
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
71 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
72 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
73 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
74 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
75 return FALSE;
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 }
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 // Option was given succesfully, try to process it
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
80 if (doProcess && !handleOptionCB(opt->id, optArg, currArg))
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
81 return FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
82 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
83 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
84 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
85 THERR("Unknown %s option '%s%s'\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
86 isLong ? "long" : "short",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
87 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
88 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
89
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
90 return FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
91 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
93 return TRUE;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 /* Process arguments, handling short and long options by
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 * calling the given callback functions.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 BOOL th_args_process(int argc, char *argv[],
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
101 const th_optarg_t *opts, const int numOpts,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
102 BOOL(*handleOptionCB) (int, char *, char *),
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
103 BOOL(*handleOther) (char *), const int flags)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
105 int argIndex, handleFlags = flags & OPTH_ONLY_MASK;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
106 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
107
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
108 for (argIndex = 1; argIndex < argc; argIndex++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
109 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
110 char *str = argv[argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
111 if (*str == '-' && !endOfOptions)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
112 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
113 // Should we process options?
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
114 BOOL doProcess = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
115 BOOL isLong;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
116
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
117 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
118 if (*str == '-')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
119 {
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
120 // 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
121 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
122 if (*str == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
123 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
124 endOfOptions = TRUE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
125 continue;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
126 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
127
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
128 // We have a long option
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
129 isLong = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
130 }
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
131 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
132 isLong = FALSE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
133
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
134 if (!th_args_process_opt(str, &argIndex, argc, argv,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
135 opts, numOpts, handleOptionCB, doProcess, isLong))
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
136 optionsOK = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
137 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
138 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
139 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
140 {
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
141 // Was not option argument
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
142 if (handleOther == NULL ||
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
143 (handleOther != NULL && !handleOther(str)))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
144 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
145 THERR("Invalid argument '%s'\n", str);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
146 optionsOK = FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
147 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
148 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
149
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
150 // Check if we bail out on invalid argument
137
0f43a94516f4 Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
151 if (!optionsOK && (flags & OPTH_BAILOUT))
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
152 return FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
153 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
155 return optionsOK;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 /* Print help for commandline arguments/options
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 */
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
161 void th_args_help(FILE *fh,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
162 const th_optarg_t *opts, const int numOpts,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
163 const int flags)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 {
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
165 int index;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
166 (void) flags;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
168 // Print out option list
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
169 for (index = 0; index < numOpts; index++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
170 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
171 const th_optarg_t *opt = &opts[index];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
172 char tmpStr[128];
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
173
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
174 // Print short option
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
175 if (opt->optShort != 0)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
176 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
177 snprintf(tmpStr, sizeof(tmpStr),
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
178 "-%c,", opt->optShort);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
179 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
180 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
181 tmpStr[0] = 0;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
182
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
183 fprintf(fh, " %-5s", tmpStr);
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
184
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
185 // Print long option
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
186 if (opt->optLong != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
187 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
188 snprintf(tmpStr, sizeof(tmpStr), "--%s%s",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
189 opt->optLong,
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
190 (opt->flags & OPT_ARGREQ) ? "=ARG" : "");
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
191 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
192 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
193 tmpStr[0] = 0;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
195 fprintf(fh, "%-20s", tmpStr);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
196
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
197 th_print_wrap(fh, opt->desc, 26, 26, th_term_width() - 2);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
198 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 }