2 * Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Test if agetarg works as expected
55 enum { GA_SUCCESS
= 0, GA_FAILURE
} retval
;
59 /* XXX TODO: aarg_negative_flag, manualpage generation ? */
66 test_simple_string (void)
71 { AARG_GNUSTYLE
, 2, { "string", "--string=foo", NULL
} },
72 { AARG_GNUSTYLE
, 3, { "string", "-s", "foo", NULL
} },
73 { AARG_AFSSTYLE
, 3, { "string", "-string", "foo", NULL
} },
74 { AARG_AFSSTYLE
, 3, { "string", "-strin", "foo", NULL
} },
75 { AARG_AFSSTYLE
, 3, { "string", "-st", "foo", NULL
}, GA_FAILURE
},
76 { AARG_AFSSTYLE
, 2, { "string", "--flag"}, GA_FAILURE
},
77 { AARG_AFSSTYLE
, 2, { "string", "foo", NULL
} }
80 struct agetargs args
[] = {
81 { "string", 's', aarg_string
, NULL
,
82 "string test", "stringfoo", aarg_mandatory
},
83 { "strip", 0, aarg_string
, NULL
,
84 "strip test", "stripfoo", aarg_optional
},
85 { NULL
, 0, aarg_end
, NULL
, NULL
}
90 for (i
= 0 ; i
< sizeof(tests
)/sizeof(*tests
); i
++) {
94 if (agetarg (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
,
96 if (tests
[i
].retval
== GA_FAILURE
)
98 warnx ("test_string: %s failed for test %d",
102 if (tests
[i
].retval
!= GA_SUCCESS
) {
103 warnx ("test_string: %s failed to fail for test %d",
104 tests
[i
].argv
[1], i
);
109 if (optind
!= tests
[i
].argc
) {
110 warnx ("argc != optind for test %s, %d", tests
[i
].argv
[1], i
);
114 if (string
== NULL
|| strcmp (string
, "foo") != 0) {
115 warnx ("error parsing for test %d: string", i
);
126 test_simple_strings (void)
128 agetarg_strings strings
;
132 { AARG_GNUSTYLE
, 3, { "strings",
133 "--strings=foo", "--strings=bar", NULL
} },
134 { AARG_GNUSTYLE
, 5, { "strings", "-s", "foo", "-s", "bar", NULL
} },
135 { AARG_AFSSTYLE
, 4, { "strings", "-string", "foo", "bar", NULL
} }
137 { AARG_AFSSTYLE
, 3, { "strings", "foo", "bar", NULL
} }
141 struct agetargs args
[] = {
142 { "strings", 's', aarg_strings
, NULL
,
143 "strings test", "stringsfoo", aarg_optional
},
144 { NULL
, 0, aarg_end
, NULL
, NULL
}
149 for (i
= 0 ; i
< sizeof(tests
)/sizeof(*tests
); i
++) {
150 strings
.num_strings
= 0;
151 strings
.strings
= NULL
;
154 if (agetarg (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
,
156 if (tests
[i
].retval
== GA_FAILURE
)
158 warnx ("test_strings: %s failed for test %d",
159 tests
[i
].argv
[1], i
);
162 if (tests
[i
].retval
!= GA_SUCCESS
) {
163 warnx ("test_strings: %s failed to fail for test %d",
164 tests
[i
].argv
[1], i
);
169 if (optind
!= tests
[i
].argc
) {
170 warnx ("argc != optind for test %s, %d",
171 tests
[i
].argv
[1], i
);
175 if (strings
.num_strings
!= 2
176 || strcmp(strings
.strings
[0], "foo") != 0
177 || strcmp(strings
.strings
[1], "bar") != 0)
179 warnx ("error parsing for test %d: strings", i
);
190 test_simple_integer (void)
195 { AARG_GNUSTYLE
, 2, { "integer", "--integer=4711", NULL
} },
196 { AARG_GNUSTYLE
, 3, { "integer", "-i", "4711", NULL
} },
197 { AARG_AFSSTYLE
, 3, { "integer", "-integer", "4711", NULL
} },
198 { AARG_AFSSTYLE
, 2, { "integer", "4711", NULL
} }
201 struct agetargs args
[] = {
202 { "integer", 'i', aarg_integer
, NULL
,
203 "integer test", "integer", aarg_mandatory
},
204 { NULL
, 0, aarg_end
, NULL
, NULL
}
209 for (i
= 0 ; i
< sizeof(tests
)/sizeof(*tests
); i
++) {
213 if (agetarg (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
,
215 if (tests
[i
].retval
== GA_FAILURE
)
217 warnx ("test_integer: %s failed for test %d",
218 tests
[i
].argv
[1], i
);
221 if (tests
[i
].retval
!= GA_SUCCESS
) {
222 warnx ("test_integer: %s failed to fail for test %d",
223 tests
[i
].argv
[1], i
);
228 if (optind
!= tests
[i
].argc
) {
229 warnx ("argc != optind for test %s, %d",
230 tests
[i
].argv
[1], i
);
234 if (integer
!= 4711) {
235 warnx ("error parsing for test %d: integer 4711", i
);
246 test_simple_flag (void)
251 { AARG_GNUSTYLE
, 2, { "flag", "--flag=yes", NULL
}, GA_SUCCESS
},
252 { AARG_GNUSTYLE
, 2, { "flag", "-g", NULL
}, GA_SUCCESS
},
253 { AARG_AFSSTYLE
, 2, { "flag", "--flag"}, GA_FAILURE
},
254 { AARG_AFSSTYLE
, 2, { "flag", "-flag", NULL
}, GA_SUCCESS
},
257 { AARG_AFSSTYLE
, 2, { "flag", "yes", NULL
}, GA_SUCCESS
},
259 { AARG_GNUSTYLE
, 2, { "flag", "--no-flag", NULL
}, GA_SUCCESS
}
262 struct agetargs args
[] = {
263 { "flag", 'g', aarg_flag
, NULL
,
264 "flag", "flag bar", aarg_optional
},
265 { NULL
, 0, aarg_end
, NULL
, NULL
}
270 for (i
= 0 ; i
< sizeof(tests
)/sizeof(*tests
); i
++) {
277 if (agetarg (args
, tests
[i
].argc
, tests
[i
].argv
, &optind
,
279 if (tests
[i
].retval
== GA_FAILURE
)
281 warnx ("test_flag: %s failed for test %d",
282 tests
[i
].argv
[1], i
);
285 if (tests
[i
].retval
!= GA_SUCCESS
) {
286 warnx ("test_flag: %s failed to fail for test %d",
287 tests
[i
].argv
[1], i
);
292 if (optind
!= tests
[i
].argc
) {
293 warnx ("argc != optind for test %s, %d",
294 tests
[i
].argv
[1], i
);
300 warnx ("error parsing for test %d: flag %s",
307 warnx ("error parsing test %d: flag %s",
321 main (int argc
, char **argv
)
323 setprogname (argv
[0]);
325 test_simple_string();
326 test_simple_strings();
327 test_simple_integer();