NDGetOpt Class Reference

Provides command-line argument parsing. More...

List of all members.

Public Member Functions

 NDGetOpt (int argc, const char **argv, const char *opt_str)
 Constructor.
 ~NDGetOpt ()
 Destructor.
uint32_t getNumOpts (void)
 Determines number of command line arguments Returns the number of command line arguments supplied by the caller.
char getOpt (uint32_t idx)
 Returns the specified option.
const char * getArg (uint32_t idx)
 Returns the specified argument value.

Detailed Description

An utility class that provides command line argument parsing of argc and argv. Similar in concept to the standard UNIX getopt() library routine, but provides a consistent API accross all platforms.

Example of NDGetOpt:

    int main(int argc, const char **argv) {
        // Default to localhost:6079 if no args are passed
        char serverAddr[1024];
        strcpy(serverAddr, "127.0.0.1");
        int serverPort = 6079;
        bool useEncryption = false;
        const char *remote_fname = NULL;
        const char *local_fname = NULL;

        NDGetOpt opts(argc, argv, "s:p:e");
        for(uint32_t i = 0;i < opts.getNumOpts();i ++){
            switch(opts.getOpt(i)){
                case 's':
                    strcpy(serverAddr, opts.getArg(i));
                    break;
                case 'p':
                    serverPort = atoi(opts.getArg(i));
                    break;
                case 'e':
                    useEncryption = true;
                    break;
                case '*':
                    if(remote_fname == NULL)
                        remote_fname = opts.getArg(i);
                    else if(local_fname == NULL)
                        local_fname = opts.getArg(i);
                    else
                        usage();
                    break;
                default:
                    usage();
            }
        }
        if(remote_fname == NULL || local_fname == NULL)
            usage();

        ...

    

Constructor & Destructor Documentation

NDGetOpt::NDGetOpt ( int  argc,
const char **  argv,
const char *  opt_str 
)

Creates a new NDGetOpt parsing object.

Parameters:
argc The same "int argc" passed to main()
argv The same "int argv" passed to main()
opt_str A string describing the allowable command line options

Member Function Documentation

const char * NDGetOpt::getArg ( uint32_t  idx  ) 

Returns the option value text (if any).

Ex. Given a command line of "<prog_name> -d -b alpha beta", then getArg(0) would return NULL, getArg(1) would return 'alpha', and getArg(2) would return 'beta'.

Parameters:
idx The index of the command line options to return. Valid values are integers from 0 to getNumOpts()-1.
Returns:
Argument value of option idx.
char NDGetOpt::getOpt ( uint32_t  idx  ) 

Returns the specified option flag identifier

Ex. Given a command line of "<prog_name> -d -b alpha beta", then getOpt(0) would return 'd', getOpt(1) would return 'b', and getOpt(2) would return '*'.

For command line arguments with out flag designators, then

Parameters:
idx The index of the command line options to return. Valid values are integers from 0 to getNumOpts()-1.
Returns:
Character identifier of option idx.

Generated on Wed Jul 21 18:14:48 2010 for nd2 by  doxygen 1.6.1