1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#include "apr_errno.h"
18
#include "apr_general.h"
19
#include "apr_getopt.h"
20
#include "apr_strings.h"
23
static void format_arg(char *str, char option, const char *arg)
26
apr_snprintf(str, 8196, "%soption: %c with %s\n", str, option, arg);
29
apr_snprintf(str, 8196, "%soption: %c\n", str, option);
33
static void unknown_arg(void *str, const char *err, ...)
38
apr_vsnprintf(str, 8196, err, va);
42
static void no_options_found(abts_case *tc, void *data)
45
const char * const largv[] = {"testprog", "-a", "-b", "-c", "-d"};
53
rv = apr_getopt_init(&opt, p, largc, largv);
54
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
56
while (apr_getopt(opt, "abcd", &ch, &optarg) == APR_SUCCESS) {
63
format_arg(str, ch, optarg);
66
ABTS_STR_EQUAL(tc, "option: a\n"
72
static void no_options(abts_case *tc, void *data)
75
const char * const largv[] = {"testprog", "-a", "-b", "-c", "-d"};
83
rv = apr_getopt_init(&opt, p, largc, largv);
84
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
86
opt->errfn = unknown_arg;
89
while (apr_getopt(opt, "efgh", &ch, &optarg) == APR_SUCCESS) {
95
format_arg(str, ch, optarg);
101
ABTS_STR_EQUAL(tc, "testprog: illegal option -- a\n", str);
104
static void required_option(abts_case *tc, void *data)
107
const char * const largv[] = {"testprog", "-a", "foo"};
115
rv = apr_getopt_init(&opt, p, largc, largv);
116
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
118
opt->errfn = unknown_arg;
121
while (apr_getopt(opt, "a:", &ch, &optarg) == APR_SUCCESS) {
124
format_arg(str, ch, optarg);
130
ABTS_STR_EQUAL(tc, "option: a with foo\n", str);
133
static void required_option_notgiven(abts_case *tc, void *data)
136
const char * const largv[] = {"testprog", "-a"};
144
rv = apr_getopt_init(&opt, p, largc, largv);
145
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
147
opt->errfn = unknown_arg;
150
while (apr_getopt(opt, "a:", &ch, &optarg) == APR_SUCCESS) {
153
format_arg(str, ch, optarg);
159
ABTS_STR_EQUAL(tc, "testprog: option requires an argument -- a\n", str);
162
static void optional_option(abts_case *tc, void *data)
165
const char * const largv[] = {"testprog", "-a", "foo"};
173
rv = apr_getopt_init(&opt, p, largc, largv);
174
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
176
opt->errfn = unknown_arg;
179
while (apr_getopt(opt, "a::", &ch, &optarg) == APR_SUCCESS) {
182
format_arg(str, ch, optarg);
188
ABTS_STR_EQUAL(tc, "option: a with foo\n", str);
191
static void optional_option_notgiven(abts_case *tc, void *data)
194
const char * const largv[] = {"testprog", "-a"};
202
rv = apr_getopt_init(&opt, p, largc, largv);
203
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
205
opt->errfn = unknown_arg;
208
while (apr_getopt(opt, "a::", &ch, &optarg) == APR_SUCCESS) {
211
format_arg(str, ch, optarg);
218
/* Our version of getopt doesn't allow for optional arguments. */
219
ABTS_STR_EQUAL(tc, "option: a\n", str);
221
ABTS_STR_EQUAL(tc, "testprog: option requires an argument -- a\n", str);
224
abts_suite *testgetopt(abts_suite *suite)
226
suite = ADD_SUITE(suite)
228
abts_run_test(suite, no_options, NULL);
229
abts_run_test(suite, no_options_found, NULL);
230
abts_run_test(suite, required_option, NULL);
231
abts_run_test(suite, required_option_notgiven, NULL);
232
abts_run_test(suite, optional_option, NULL);
233
abts_run_test(suite, optional_option_notgiven, NULL);