Command Argument Parsing

Overview

// typedefs

typedef void() apr_getopt_err_fn_t(
    void *arg,
    const char *err,
    ...
    );

typedef struct apr_getopt_t apr_getopt_t;
typedef struct apr_getopt_option_t apr_getopt_option_t;

// structs

struct apr_getopt_option_t;
struct apr_getopt_t;

// global functions

apr_status_t
apr_getopt_init(
    apr_getopt_t** os,
    apr_pool_t* cont,
    int argc,
    const char*const* argv
);

apr_status_t
apr_getopt(
    apr_getopt_t* os,
    const char* opts,
    char* option_ch,
    const char** option_arg
);

apr_status_t
apr_getopt_long(
    apr_getopt_t* os,
    const apr_getopt_option_t* opts,
    int* option_ch,
    const char** option_arg
);

Detailed Documentation

Typedefs

typedef void() apr_getopt_err_fn_t(
    void *arg,
    const char *err,
    ...
    )

An apr_getopt_t error callback function.

arg is this apr_getopt_t's errarg member.

typedef struct apr_getopt_t apr_getopt_t

See also:

apr_getopt_t

typedef struct apr_getopt_option_t apr_getopt_option_t

See also:

apr_getopt_option_t

Global Functions

apr_status_t
apr_getopt_init(
    apr_getopt_t** os,
    apr_pool_t* cont,
    int argc,
    const char*const* argv
)

Initialize the arguments for parsing by apr_getopt(). Arguments 3 and 4 are most commonly argc and argv from main(argc, argv) The (*os)->errfn is initialized to fprintf(stderr… but may be overridden.

Parameters:

os

The options structure created for apr_getopt()

cont

The pool to operate on

argc

The number of arguments to parse

argv

The array of arguments to parse

apr_status_t
apr_getopt(
    apr_getopt_t* os,
    const char* opts,
    char* option_ch,
    const char** option_arg
)

Parse the options initialized by apr_getopt_init().

Parameters:

os

The apr_opt_t structure returned by apr_getopt_init()

opts

A string of characters that are acceptable options to the program. Characters followed by “:” are required to have an option associated

option_ch

The next option character parsed

option_arg

The argument following the option character:

Returns:

There are four potential status values on exit. They are:

APR_EOF        No more options to parse
APR_BADCH      Found a bad option character
APR_BADARG     No argument followed the option flag
APR_SUCCESS    The next option was found.
apr_status_t
apr_getopt_long(
    apr_getopt_t* os,
    const apr_getopt_option_t* opts,
    int* option_ch,
    const char** option_arg
)

Parse the options initialized by apr_getopt_init(), accepting long options beginning with “–” in addition to single-character options beginning with “-“.

Parameters:

os

The apr_getopt_t structure created by apr_getopt_init()

opts

A pointer to a list of apr_getopt_option_t structures, which can be initialized with { “name”, optch, has_args }. has_args is nonzero if the option requires an argument. A structure with an optch value of 0 terminates the list.

option_ch

Receives the value of “optch” from the apr_getopt_option_t structure corresponding to the next option matched.

option_arg

Receives the argument following the option, if any.

Returns:

There are four potential status values on exit. They are:

APR_EOF      --  No more options to parse
APR_BADCH    --  Found a bad option character
APR_BADARG   --  No argument followed the option flag
APR_SUCCESS  --  The next option was found.

When APR_SUCCESS is returned, os->ind gives the index of the first non-option argument. On error, a message will be printed to stdout unless os->err is set to 0. If os->interleave is set to nonzero, options can come after arguments, and os->argv will be permuted to leave non-option arguments at the end (the original argv is unaffected).