Name

getopt_long - parses command line options

Library

libc.lib

Synopsis

  #include <getopt.h>

extern char *optarg;

extern int optind;

extern int optopt;

extern int opterr;

extern int optreset;

  int getopt_long (int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex);

Return values

If the flag field in struct option is NULL, getopt_long returns the value specified in the val field, which is usually just the corresponding short option. If flag is not NULL, this function returns 0 and stores val in the location pointed to by flag. This function returns :’ if there was a missing option argument, ?’ if the user specified an unknown or ambiguous option, and -1 when the argument list has been exhausted.

Detailed description

The getopt_long function is similar to getopt but it accepts options in two forms: words and characters. The getopt_long function provides a superset of the functionality of getopt. The getopt_long function can be used in two ways. In the first way, every long option understood by the program has a corresponding short option, and the option structure is only used to translate from long options to short options. When used in this fashion, getopt_long behaves identically to getopt. This is a good way to add long option processing to an existing program with the minimum of rewriting.

In the second mechanism, a long option sets a flag in the option structure passed, or will store a pointer to the command line argument in the option structure passed to it for options that take arguments. Additionally, the long option’s argument may be specified as a single argument with an equal sign, e.g.,

     myprogram --myoption=somevalue
When a long option is processed, the call to getopt_long will return 0. For this reason, long option processing without shortcuts is not backwards compatible with getopt. It is possible to combine these methods, providing for long options processing with short option equivalents for some options. Less frequently used options would be processed as long options only. The getopt_long call requires a structure to be initialized describing the long options. The structure is:
struct option {
        char *name;
        int has_arg;
        int *flag;
        int val;
};

         

The name field should contain the option name without the leading double dash.

The has_arg field should be one of:

no_argument no argument to the option is expect
required_argument
  an argument to the option is required
optional_argument
  an argument to the option may be presented.

If flag is not NULL, then the integer pointed to by it will be set to the value in the val field. If the flag field is NULL, then the val field will be returned. Setting flag to NULL and setting val to the corresponding short option will make this function act just like getopt.

If the longindex field is not NULL, then the integer pointed to by it will be set to the index of the long option relative to longopts.

The last element of the longopts array has to be filled with zeroes.


Examples

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <getopt.h>
#include <errno.h>
int main()
{
    int bflag, ch, fd;
    int daggerset;            
          
    int argc = 3;
         
    char *argv[] = { "getopt", "--daggerset","hi" };   
        
    struct option  longopts[] = {    
      { "buffy",      no_argument,            NULL,           'b' },
      { "fluoride",   required_argument,      NULL,           'f' },
      { "daggerset",  no_argument,           &daggerset,   1 },
      { NULL,         0,                      NULL,           0 }       
    };
         
    bflag = 0;
       
    while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) {      
        switch (ch) {
          
                case 'b':
                    printf("option is \"buffy\"\n");
                bflag = 1;
                break;
                 
                case 'f':
                    printf("option is \"fluoride\"\n");
                if ((fd = open(optarg, O_RDONLY, 0)) == -1)
                        printf("unable to open %s", optarg);
                break;
                
                case 0:
                if (daggerset) {
                        fprintf(stderr,"Buffy will use her dagger to apply \nfluoride to dracula's teeth\n");
                }
                break;
          default:
                printf("unknown option\n");
                
        }//end of switch
    }//end of while
 
return 0;
}

         

Output

Buffy will use her dagger to apply fluoride to dracula’s teeth

         

See also

getopt

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top