*nix Documentation Project
·  Home
 +   man pages
·  Linux HOWTOs
·  FreeBSD Tips
·  *niX Forums

  man pages->FreeBSD man pages -> getopt_long (3)              



NAME    [Toc]    [Back]

     getopt_long -- get long options from command line argument list

LIBRARY    [Toc]    [Back]

     Standard C Library (libc, -lc)

SYNOPSIS    [Toc]    [Back]

     #include <getopt.h>

     getopt_long(int argc, char * const *argv, const char *optstring,
	 struct option *long options, int *index);

DESCRIPTION    [Toc]    [Back]

     The getopt_long() function is similar to getopt(3) but it accepts options
     in two forms: words and characters.  The getopt_long() function provides
     a superset of the functionality of getopt(3).  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(3).  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(3).

     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

     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(3).

EXAMPLES    [Toc]    [Back]

     extern char *optarg;
     extern int optind;
     int bflag, ch, fd;
     int daggerset;

     /* options descriptor */
     static struct option longopts[] = {
	     { "buffy",      no_argument,	     0, 	     'b' },
	     { "floride",    required_argument,      0, 	     'f' },
	     { "daggerset",  no_argument,	     &daggerset,     1 },
	     { 0,	     0, 		     0, 	     0 }

     bflag = 0;
     while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1)
	     switch(ch) {
	     case 'b':
		     bflag = 1;
	     case 'f':
		     if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
				 "myname: %s: %s\n", optarg, strerror(errno));
	     case 0:
		     if(daggerset) {
			     fprintf(stderr,"Buffy will use her dagger to "
					    "apply floride to dracula's teeth\n");
	     case '?':
     argc -= optind;
     argv += optind;


     This section describes differences to the GNU implementation found in

     +o	 Handling of `-' as first char of option string in presence of environment

	 GNU	 ignores POSIXLY_CORRECT and returns non-options as arguments
		 to option '\1'.

	 NetBSD  honors POSIXLY_CORRECT and stops at the first non-option.

     +o	 Handling of `::' in options string in presence of POSIXLY_CORRECT:

	 Both	 GNU and NetBSD ignore POSIXLY_CORRECT here and take `::' to
		 mean the preceding option takes an optional argument.

     +o	 Return value in case of missing argument if first character (after
	 `+' or `-') in option string is not `:':

	 GNU	 returns `?'

	 NetBSD  returns `:' (since NetBSD's getopt() does).

     +o	 Handling of `--a' in getopt:

	 GNU	 parses this as option `-', option `a'.

	 NetBSD  parses this as `--', and returns -1 (ignoring the `a').
		 (Because the original getopt() does.)

     +o	 Setting of optopt for long options with flag != NULL:

	 GNU	 sets optopt to val.

	 NetBSD  sets optopt to 0 (since val would never be returned).

     +o	 Handling of `-W' with `W'; in option string in getopt() (not

	 GNU	 causes a segfault.

	 NetBSD  returns -1, with optind pointing past the argument of `-W'
		 (as if `-W arg' were `--arg', and thus `--' had been found).

     +o	 Setting of optarg for long options without an argument that are
	 invoked via `-W' (`W'; in option string):

	 GNU	 sets optarg to the option name (the argument of `-W').

	 NetBSD  sets optarg to NULL (the argument of the long option).

     +o	 Handling of `-W' with an argument that is not (a prefix to) a known
	 long option (`W'; in option string):

	 GNU	 returns `-W' with optarg set to the unknown option.

	 NetBSD  treats this as an error (unknown option) and returns `?' with
		 optopt set to 0 and optarg set to NULL (as GNU's man page

     +o	 The error messages are different.

     +o	 NetBSD does not permute the argument vector at the same points in the
	 calling sequence as GNU does.	The aspects normally used by the
	 caller (ordering after -1 is returned, value of optind relative to
	 current positions) are the same, though.  (We do fewer variable

SEE ALSO    [Toc]    [Back]


HISTORY    [Toc]    [Back]

     The getopt_long() function first appeared in GNU libiberty.  The first
     NetBSD implementation appeared in 1.5.

BUGS    [Toc]    [Back]

     The implementation can completely replace getopt(3), but right now we are
     using separate code.

FreeBSD 5.2.1			 April 1, 2000			 FreeBSD 5.2.1
[ Back ]
 Similar pages
Name OS Title
getopt OpenBSD get option character from command line argument list
getopt NetBSD get option character from command line argument list
getopt FreeBSD get option character from command line argument list
pxfgetarg IRIX Returns a command-line argument
getarg IRIX return Fortran command-line argument
xargs IRIX construct argument list(s) and execute command
xargs HP-UX construct argument list(s) and execute command
getopt Linux Parse command line options
parseargv IRIX process command-line options
Getopt::Long IRIX extended processing of command line options
Copyright © 2004-2005 DeniX Solutions SRL
newsletter delivery service