1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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.
22 * @brief APR Command Arguments (getopt)
25 #include "apr_pools.h"
29 #endif /* __cplusplus */
32 * @defgroup apr_getopt Command Argument Parsing
38 * defintion of a error function
40 typedef void (apr_getopt_err_fn_t)(void *arg, const char *err, ...);
42 /** @see apr_getopt_t */
43 typedef struct apr_getopt_t apr_getopt_t;
46 * Structure to store command line argument information.
49 /** context for processing */
51 /** function to print error message (NULL == no messages) */
52 apr_getopt_err_fn_t *errfn;
53 /** user defined first arg to pass to error message */
55 /** index into parent argv vector */
57 /** character checked for validity */
61 /** count of arguments */
63 /** array of pointers to arguments */
65 /** argument associated with option */
67 /** set to nonzero to support interleaving options with regular args */
69 /** start of non-option arguments skipped for interleaving */
71 /** end of non-option arguments skipped for interleaving */
75 /** @see apr_getopt_option_t */
76 typedef struct apr_getopt_option_t apr_getopt_option_t;
79 * Structure used to describe options that getopt should search for.
81 struct apr_getopt_option_t {
82 /** long option name, or NULL if option has no long name */
84 /** option letter, or a value greater than 255 if option has no letter */
86 /** nonzero if option takes an argument */
88 /** a description of the option */
89 const char *description;
93 * Initialize the arguments for parsing by apr_getopt().
94 * @param os The options structure created for apr_getopt()
95 * @param cont The pool to operate on
96 * @param argc The number of arguments to parse
97 * @param argv The array of arguments to parse
98 * @remark Arguments 2 and 3 are most commonly argc and argv from main(argc, argv)
99 * The errfn is initialized to fprintf(stderr... but may be overridden.
101 APR_DECLARE(apr_status_t) apr_getopt_init(apr_getopt_t **os, apr_pool_t *cont,
102 int argc, const char * const *argv);
105 * Parse the options initialized by apr_getopt_init().
106 * @param os The apr_opt_t structure returned by apr_getopt_init()
107 * @param opts A string of characters that are acceptable options to the
108 * program. Characters followed by ":" are required to have an
110 * @param option_ch The next option character parsed
111 * @param option_arg The argument following the option character:
112 * @return There are four potential status values on exit. They are:
114 * APR_EOF -- No more options to parse
115 * APR_BADCH -- Found a bad option character
116 * APR_BADARG -- No argument followed the option flag
117 * APR_SUCCESS -- The next option was found.
120 APR_DECLARE(apr_status_t) apr_getopt(apr_getopt_t *os, const char *opts,
121 char *option_ch, const char **option_arg);
124 * Parse the options initialized by apr_getopt_init(), accepting long
125 * options beginning with "--" in addition to single-character
126 * options beginning with "-".
127 * @param os The apr_getopt_t structure created by apr_getopt_init()
128 * @param opts A pointer to a list of apr_getopt_option_t structures, which
129 * can be initialized with { "name", optch, has_args }. has_args
130 * is nonzero if the option requires an argument. A structure
131 * with an optch value of 0 terminates the list.
132 * @param option_ch Receives the value of "optch" from the apr_getopt_option_t
133 * structure corresponding to the next option matched.
134 * @param option_arg Receives the argument following the option, if any.
135 * @return There are four potential status values on exit. They are:
137 * APR_EOF -- No more options to parse
138 * APR_BADCH -- Found a bad option character
139 * APR_BADARG -- No argument followed the option flag
140 * APR_SUCCESS -- The next option was found.
142 * When APR_SUCCESS is returned, os->ind gives the index of the first
143 * non-option argument. On error, a message will be printed to stdout unless
144 * os->err is set to 0. If os->interleave is set to nonzero, options can come
145 * after arguments, and os->argv will be permuted to leave non-option arguments
146 * at the end (the original argv is unaffected).
148 APR_DECLARE(apr_status_t) apr_getopt_long(apr_getopt_t *os,
149 const apr_getopt_option_t *opts,
151 const char **option_arg);
158 #endif /* ! APR_GETOPT_H */