4.14.5 User Interaction [ToC] [Index]     [Skip Back] [Skip Fwd]     [Prev] [Up] [Next]

This section briefly discusses libavl's data structures and functions for parsing command-line arguments. For more information on the command-line arguments accepted by the testing program, refer to the libavl reference manual.

The main way that the test program receives instructions from the user is through the set of arguments passed to main(). The program assumes that these arguments can be controlled easily by the user, presumably through some kind of command-based “shell” program. It allows for two kinds of options: traditional UNIX “short options” that take the form `-o' and GNU-style “long options” of the form `--option'. Either kind of option may take an argument.

Options are specified using an array of struct option, terminated by an all-zero structure:

134. <Test declarations 122> +=
/* A single command-line option. */
struct option 
  { const char *long_name; /* Long name ("-name"). */ int short_name; /* Short name ("n"); value returned. */ int has_arg; /* Has a required argument? */ };

There are two public functions in the option parser:

struct option_state *option_init (struct option *options, char **args)
Creates and returns a struct option_state, initializing it based on the array of arguments passed in. This structure is used to keep track of the option parsing state. Sets options as the set of options to parse.
int option_get (struct option_state *state, char **argp)
Parses the next option from state and returns the value of the short_name member from its struct option. Sets *argp to the option's argument or NULL if none. Returns -1 and destroys state if no options remain.

These functions' implementation are not too interesting for our purposes, so they are relegated to an appendix. See Option Parser, for the full story.

The option parser provides a lot of support for parsing the command line, but of course the individual options have to be handled once they are retrieved by option_get(). The parse_command_line() function takes care of the whole process:

void parse_command_line (char **args, struct test_options *options)
Parses the command-line arguments in args[], which must be terminated with an element set to all zeros, using option_init() and option_get(). Sets up options appropriately to correspond.

See Command-Line Parser, for source code. The struct test_options initialized by parse_command_line() is described in detail below.