Functions

getargs.h File Reference

Command-line argument parsing module. More...

Go to the source code of this file.

Functions

int parseArgs (int argc, char *argv[])
 Parses a command-line argument array.
const char * getOptionVal (const char option[])
 Retrieves the corresponding value for a given option key.
const char * getLeadingArg (unsigned int i)
 Retrieves a leading argument.
const char * getTrailingArg (unsigned int i)
 Retrieves a trailing argument.

Detailed Description

Command-line argument parsing module.


Function Documentation

const char* getLeadingArg ( unsigned int  i  ) 

Retrieves a leading argument.

Parameters:
i Argument index.
Returns:
Corresponding leading argument, or NULL if i is out of range.
Note:
The program name (argv[0]) itself is omitted, so i = 0 will yield argv[1]. It can be retrieved by calling getProgramName().
const char* getOptionVal ( const char  option[]  ) 

Retrieves the corresponding value for a given option key.

If the command line contains more than one pair with the same key, the value of the last pair is returned.

Parameters:
option Option key. Must begin with a dash ('-').
Returns:
Value corresponding to the key, or NULL if no mapping with this key was found.
const char* getTrailingArg ( unsigned int  i  ) 

Retrieves a trailing argument.

Parameters:
i Argument index.
Returns:
Corresponding trailing argument, or NULL if i is out of range.
int parseArgs ( int  argc,
char *  argv[] 
)

Parses a command-line argument array.

This function must be invoked before any of the other functions. It parses the given argument array containing key-to-value mappings. The array has to come in a form like this:

program -key0 value0 -key1 value1 ...

Keys have to begin with a dash ('-') whereas values must not. The value associated with a given key can be retrieved using getOptionVal(). The array may contain additional arguments that are not part of the mapping before or after the key-value pairs, but not in between. They can be retrieved using getLeadingArg() or getTrailingArg(), respectively. If there are no key-value pairs, all arguments are considered to be both, leading and trailing.

Parameters:
argc Number of command-line arguments.
argv Array of command-line arguments.
Returns:
0 on success; -1 if a parsing error occurred.
 All Files Functions