Main Page   Modules   File List   Related Pages   Examples  

getopt -- commandline option handling


Detailed Description

Module Xgetopt is written by Hans Dietrich <hdietrich2@hotmail.com>

Module Xgetopt is released into the public domain. You are free to use it in any way you like.

This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


Functions

int getopt (int argc, char *argv[], char *optstring)
 parse command line options


Variables

char * optarg
 optarg points to the current option's argument.

int optind = 0
 argv index of next argument to process.

int opterr
 opterr is not used by this version of getopt().


Function Documentation

int getopt int  argc,
char *  argv[],
char *  optstring
 

The getopt() function parses the command line arguments. Its arguments argc and argv are the argument count and array as passed into the application on program invocation. In the case of Visual C++ programs, argc and argv are available via the variables __argc and __argv (double underscores), respectively. getopt() returns the next option letter in argv that matches a letter in optstring.

optstring is a string of recognized option letters; if a letter is followed by a colon, the option is expected to have an argument that may or may not be separated from it by white space. optarg is set to point to the start of the option argument on return from getopt().

Option letters may be combined, e.g., "-ab" is equivalent to "-a -b". Option letters are case sensitive.

getopt() places in the external variable optind the argv index of the next argument to be processed. optind is initialized to 0 before the first call to getopt().

When all options have been processed (i.e., up to the first non-option argument), getopt() returns EOF, optarg will point to the argument, and optind will be set to the argv index of the argument. If there are no non-option arguments, optarg will be set to NULL.

The special option "--" may be used to delimit the end of the options; EOF will be returned, and "--" (and everything after it) will be skipped.

Returns:
For option letters contained in the string optstring, getopt() will return the option letter. getopt() returns a question mark (?) when it encounters an option letter not included in optstring. EOF is returned when processing is finished.
Author:
Hans Dietrich <hdietrich2@hotmail.com>

Bug:
  • Long options are not supported.
  • The GNU double-colon extension is not supported.
  • The environment variable POSIXLY_CORRECT is not supported.
  • The + syntax is not supported.
  • The automatic permutation of arguments is not supported.
  • This implementation of getopt() returns EOF if an error is encountered, instead of -1 as the latest standard requires.
Example:

 BOOL CMyApp::ProcessCommandLine(int argc, char *argv[])
 {
     int c;

     while ((c = getopt(argc, argv, "aBn:")) != EOF)
     {
         switch (c)
         {
             case 'a':
                 TRACE(_T("option a\n"));
                 //
                 // set some flag here
                 //
                 break;

             case 'B':
                 TRACE( _T("option B\n"));
                 //
                 // set some other flag here
                 //
                 break;

             case 'n':
                 TRACE(_T("option n: value=%d\n"), atoi(optarg));
                 //
                 // do something with value here
                 //
                 break;

             case '?':
                 TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]);
                 return FALSE;
                 break;

             default:
                 TRACE(_T("WARNING: no handler for option %c\n"), c);
                 return FALSE;
                 break;
         }
     }
     //
     // check for non-option args here
     //
     return TRUE;
 }


Variable Documentation

char* optarg
 

On return from getopt(), optarg is set to point to the start of the option argument. If there are no non-option arguments, optarg will be set to NULL.

int opterr
 

This version of getopt() does not fill opterr.

int optind = 0
 

getopt() places in the external variable optind the argv index of the next argument to be processed. optind is initialized to 0 before the first call to getopt().


Generated on Tue Apr 29 11:20:47 2003 for Edit Env by doxygen 1.3