Provides command-line argument parsing. More...
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. | |
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();
...
| NDGetOpt::NDGetOpt | ( | int | argc, | |
| const char ** | argv, | |||
| const char * | opt_str | |||
| ) |
Creates a new NDGetOpt parsing object.
| 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 |
| 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'.
| idx | The index of the command line options to return. Valid values are integers from 0 to getNumOpts()-1. |
| 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
| idx | The index of the command line options to return. Valid values are integers from 0 to getNumOpts()-1. |
1.6.1