annotate th_args.c @ 380:ac10155d2b4a

Rename th_optarg_t to t_optarg. API break.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 01 Mar 2016 17:21:22 +0200
parents f4e182720870
children 1b3472ba7b23
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[],
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
20 const th_optarg opts[], int nopts,
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
21 BOOL (*handle_option)(int id, char *, char *),
142
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 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
24 const th_optarg *opt = NULL;
142
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
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
28 for (optIndex = 0; optIndex < nopts; optIndex++)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
29 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
30 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
31 if (isLong && node->o_long != NULL)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
32 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
33 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
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
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
40 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
41 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
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
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
50 if (!isLong && node->o_short != 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
51 {
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
52 if (*currArg == node->o_short)
142
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 }
173
503f0cb98775 Fix handling of option arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
70
503f0cb98775 Fix handling of option arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 161
diff changeset
71 if (optArg == NULL)
142
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 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
74 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
75 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
76 return FALSE;
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
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
80 // 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
81 if (doProcess && !handle_option(opt->id, optArg, currArg))
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
82 return FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
83 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
84 else
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
85 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
86 THERR("Unknown %s option '%s%s'\n",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
87 isLong ? "long" : "short",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
88 isLong ? "--" : "-",
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
89 currArg);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
90
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
91 return FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
92 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
94 return TRUE;
0
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 /* 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
99 * calling the given callback functions.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 */
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 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
102 const th_optarg *opts, const int nopts,
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
103 BOOL(*handle_option)(int id, char *, char *),
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
104 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
105 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
106 int argIndex, handleFlags = flags & OPTH_ONLY_MASK;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
107 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
108
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
109 for (argIndex = 1; argIndex < argc; argIndex++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
110 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
111 char *str = argv[argIndex];
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
112 if (*str == '-' && !endOfOptions)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
113 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
114 // Should we process options?
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
115 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
116 BOOL isLong;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
117
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
118 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
119 if (*str == '-')
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
120 {
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
121 // 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
122 str++;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
123 if (*str == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
124 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
125 endOfOptions = TRUE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
126 continue;
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
127 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
128
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
129 // We have a long option
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
130 isLong = TRUE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
131 }
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
132 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
133 isLong = FALSE;
139
4ca0af6dbcf8 Another fix in the option handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 138
diff changeset
134
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
135 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
136 opts, nopts, handle_option, doProcess, isLong))
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
137 optionsOK = FALSE;
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
138 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
139 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
140 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
141 {
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
142 // Was not option argument
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
143 if (handle_other == NULL ||
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
144 (handle_other != NULL && !handle_other(str)))
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
145 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
146 THERR("Invalid argument '%s'\n", str);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
147 optionsOK = FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
148 }
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
149 }
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
150
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
151 // Check if we bail out on invalid argument
137
0f43a94516f4 Improve argument handling module.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
152 if (!optionsOK && (flags & OPTH_BAILOUT))
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
153 return FALSE;
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
154 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
156 return optionsOK;
0
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
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 /* 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
161 */
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
162 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
163 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
164 const int flags)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 {
136
286b2249c5d2 Clean up th_args API.
Matti Hamalainen <ccr@tnsp.org>
parents: 128
diff changeset
166 int index;
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
167 (void) flags;
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
169 // Print out option list
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
170 for (index = 0; index < nopts; index++)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
171 {
380
ac10155d2b4a Rename th_optarg_t to t_optarg. API break.
Matti Hamalainen <ccr@tnsp.org>
parents: 379
diff changeset
172 const th_optarg *opt = &opts[index];
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
173 char tmpStr[128];
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
174
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
175 // Print short option
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
176 if (opt->o_short != 0)
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
177 {
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
178 snprintf(tmpStr, sizeof(tmpStr),
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
179 "-%c,", opt->o_short);
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
180 }
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
181 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
182 tmpStr[0] = 0;
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
183
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
184 fprintf(fh, " %-5s", tmpStr);
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
185
128
c22caa6e3fcb Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 127
diff changeset
186 // Print long option
379
f4e182720870 Rename some function arguments and option argument structure members.
Matti Hamalainen <ccr@tnsp.org>
parents: 173
diff changeset
187 if (opt->o_long != NULL)
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
188 {
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
189 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
190 opt->o_long,
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
191 (opt->flags & OPT_ARGREQ) ? "=ARG" : "");
39
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
192 }
0d55592e0e01 Cosmetic cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
193 else
142
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
194 tmpStr[0] = 0;
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 fprintf(fh, "%-20s", tmpStr);
0a4fd9cfb929 Revise the argument handling API. Breaks compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 141
diff changeset
197
143
c878cdcfea9d Implement terminal width.
Matti Hamalainen <ccr@tnsp.org>
parents: 142
diff changeset
198 th_print_wrap(fh, opt->desc, 26, 26, th_term_width() - 2);
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 2
diff changeset
199 }
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 }