Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

option.c File Reference

Go to the source code of this file.

Functions

Optionfind_option (Option_set *opt, char *flag)
 Returns a pointer to the Option data structure that represents a given option.

int get_argument (Option_set *opt, char *flag, char **arg)
 Returns a pointer to the argument associated with a given option.

int is_argument_required (Option_set *opt, char *flag)
 This function returns a value that represents the number of arguments (0 or 1 argument) required by for a given option.

int init_options (Option_set *opt)
 Initialize the array of option definitions.

int parse_command_line (char *argv[], int argc, Option_set *opt, char **err, char **flag, char **arg)
 Parse the command line and set up the option's data structure.


Variables

char self_option [] = ""
 Pointer used to set the argument of the options that do not require arguments (ex: --help).


Detailed Description

This file implements the options' reader.

Definition in file option.c.


Function Documentation

Option* find_option Option_set   opt,
char *    flag
[static]
 

Returns a pointer to the Option data structure that represents a given option.

Parameters:
opt Pointer to a Option_set data structure that defines all options.
flag Pointer to a zero terminated string of characters that represents the option's flag to locate.
Returns:
If the option represented by the string pointed by flag is found, then the function returns a non NULL pointer to the Option data structure that defines the option. Otherwize (the option was not found), the function returns the value NULL.

Definition at line 24 of file option.c.

References Option_set_::count, Option_::flag, and Option_set_::opt.

Referenced by get_argument(), is_argument_required(), and parse_command_line().

int get_argument Option_set   opt,
char *    flag,
char **    arg
 

Returns a pointer to the argument associated with a given option.

Parameters:
opt Pointer to a Option_set data structure that defines all options.
flag Pointer to a zero terminated string of characters that represents the option's flag that you want to get the argument.
arg If the option is valid (defined in the list of options), then the content of arg pointes to the argument associated with the option. If the content of arg is equal to NULL, this means that the option is not activated in the command line.
Returns:
If the option is found (that is: the option is defined in the Option_set data structure pointed by opt), the function returns the value 0. In this case:
  • if arg is NULL, it means that the option was not activated.
  • if arg is not NULL, it means that the option was activated.
    • If the option requires one argument, then arg pointes to a zero terminated string of characters that represents the argument.
    • If the option does not require any argument, then you don't care about the string of characters pointed by arg (this string exists, but it does not mean anything).
If the option can not be found (the option is not defined in the Option_set data structure pointed by opt), the function returns the value 1. Please note that a return value of 1 means that you've made an programming error (in your code, you are testing for options that you did not define earlier).
Examples:
test_options.c.

Definition at line 65 of file option.c.

References find_option(), and Option_::pointer.

int init_options Option_set   opt
 

Initialize the array of option definitions.

Parameters:
opt Pointer to a Option_set data structure that contains the definitions of all options.
Returns:
Upon successful completion the function returns the value 0. Otherwise it returns the value 1.
Warning:
Please note that an option's flag should verify the following rules:
  • At least 2 characters.
  • Begin with a '-'.
  • No spaces.
  • Not all '-'.
Examples:
test_options.c.

Definition at line 124 of file option.c.

References Option_set_::count, Option_::flag, Option_set_::last_index, and Option_set_::opt.

int is_argument_required Option_set   opt,
char *    flag
 

This function returns a value that represents the number of arguments (0 or 1 argument) required by for a given option.

Parameters:
opt Pointer to a Option_set data structure that contains the definitions of all options.
flag Pointer to a zero terminated string of characters that represents the option's flag to test.
Returns:
The function may return one of the following values:
  • -1: An error occured. The option's flag pointed by flag does not represent any pre-defined options. This is a programming error. You are trying to test an option that you did not define previously.
  • 0: The option does not require any argument.
  • 1: The option requires one argument.
Examples:
test_options.c.

Definition at line 96 of file option.c.

References Option_::arg, and find_option().

int parse_command_line char *    argv[],
int    argc,
Option_set   opt,
char **    err,
char **    flag,
char **    arg
 

Parse the command line and set up the option's data structure.

Parameters:
argv Array of sero terminated strings of characters that represents the command line. Please note that the first element of the array (indice 0) should point to the name of the process.
argc Number of elements in the array argv.
opt Pointer to the Option_set data structure that contains the option's definitions.
err Pointer to a pointer that will be used to store the address of a zero terminated string of characters. If an error occured, this string contains a literal description of the error.
flag Pointer to a pointer that will be used to store the address of a zero terminated string of characters. If an error occured, this string contains the option that generated the error.
arg Pointer to a pointer that will be used to store the address of a zero terminated string of characters. If an error occured, this string contains the argument that caused the error.
Returns:
Upon successful completion, the function returns the value 0. Otherwise the function returns the value 1.
Warning:
A valid command line should look something like:
<process name> [-flag1 [arg1]] [-flag2 [arg2]] [...] <required arg 1> ...
Examples:
test_options.c.

Definition at line 185 of file option.c.

References Option_::arg, find_option(), Option_set_::last_index, Option_::max_size, Option_::pointer, Option_::reg_exp, and self_option.


Generated on Thu Apr 3 16:23:46 2003 for Common_C_libraries by doxygen1.3-rc1