Differences between version 5 and predecessor to the previous major change of getopt(3).
Other diffs: Previous Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 5 | Last edited on Saturday, September 6, 2003 4:20:13 pm | by JohnMcPherson | Revert |
Older page: | version 1 | Last edited on Monday, June 3, 2002 6:54:09 pm | by perry | Revert |
@@ -1,36 +1,27 @@
-GETOPT
-!!!GETOPT
-NAME
-SYNOPSIS
-DESCRIPTION
-RETURN VALUE
-ENVIRONMENT VARIABLES
-EXAMPLE
-BUGS
-CONFORMING TO
-----
!!NAME
-
-
getopt - Parse command line options
!!SYNOPSIS
+ __#include <unistd.h>__
+
+ __int getopt(int __''argc''__, char * const __''argv''__[],__
+ __const char *__''optstring''__);__
+
+ __extern char *__''optarg''__;__
+ __extern int __''optind''__, __''opterr''__, __''optopt''__;__
+
+ __#define _GNU_SOURCE__
+ __#include <getopt.h>__
+
+ __int getopt_long(int __''argc''__, char * const __''argv''__[],__
+ __const char *__''optstring''__,__
+ __const struct option *__''longopts''__, int *__''longindex''__);__
+
+ __int getopt_long_only(int __''argc''__, char * const __''argv''__[],__
+ __const char *__''optstring''__,__
+ __const struct option *__''longopts''__, int *__''longindex''__);__
-__#include
-__ ''argc''__, char * const__ ''argv[[]''__,
-const char *__''optstring''__);
-extern char *__''optarg''__;
-extern int__ ''optind''__,__ ''opterr''__,__ ''optopt''__;
-#define _GNU_SOURCE
-#include
-__ ''argc''__, char * const__ ''argv[[]''__,
-const char *__''optstring''__,
-const struct option *__''longopts''__, int *__''longindex''__);
-int getopt_long_only(int__ ''argc''__, char * const__ ''argv[[]''__,
-const char *__''optstring''__,
-const struct option *__''longopts''__, int *__''longindex''__);
-__
!!DESCRIPTION
The __getopt()__ function parses the command line
@@ -109,14 +100,15 @@
array of __struct option__ declared in
____ as
-struct option {
-const char *name;
-int has_arg;
-int *flag;
-int val;
-};
+
struct option {
+
const char *name;
+
int has_arg;
+
int *flag;
+
int val;
+ };
+
The meanings of the different fields are:
''name''
@@ -167,10 +159,11 @@
`-' as well as `--' can indicate a long option. If an option
that starts with `-' (not `--') doesn't match a long option,
but does match a short option, it is parsed as a short
option instead.
-!!RETURN VALUE
+
+!!RETURN VALUE
The __getopt()__ function returns the option character if
the option was found successfully, `:' if there was a
missing parameter for one of the options, `?' for an unknown
@@ -192,11 +185,9 @@
If this is set, then option processing stops as soon as a
non-option argument is encountered.
-
-
_____
-
+___<PID>_GNU_nonoption_argv_flags
___
This variable was used by __bash__ 2.0 to communicate to
GNU libc which arguments are the results of wildcard
expansion and so should not be considered as options. This
@@ -206,15 +197,91 @@
The following example program illustrates the use of
__getopt_long()__ with most of its features.
+
+ #include <stdio.h> /* for printf */
+ #include <stdlib.h> /* for exit */
+ #include <getopt.h>
+
+ int
+ main (int argc, char **argv) {
+ int c;
+ int digit_optind = 0;
+
+ while (1) {
+ int this_option_optind = optind ? optind : 1;
+ int option_index = 0;
+ static struct option long_options[] = {
+ {"add", 1, 0, 0},
+ {"append", 0, 0, 0},
+ {"delete", 1, 0, 0},
+ {"verbose", 0, 0, 0},
+ {"create", 1, 0, 'c'},
+ {"file", 1, 0, 0},
+ {0, 0, 0, 0}
+ };
+
+ c = getopt_long (argc, argv, "abc:d:012",
+ long_options, &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 0:
+ printf ("option %s", long_options[[option_index].name);
+ if (optarg)
+ printf (" with arg %s", optarg);
+ printf ("\n");
+ break;
+
+ case '0':
+ case '1':
+ case '2':
+ if (digit_optind != 0 && digit_optind != this_option_optind)
+ printf ("digits occur in two different argv-elements.\n");
+ digit_optind = this_option_optind;
+ printf ("option %c\n", c);
+ break;
+
+ case 'a':
+ printf ("option a\n");
+ break;
+
+ case 'b':
+ printf ("option b\n");
+ break;
+
+ case 'c':
+ printf ("option c with value `%s'\n", optarg);
+ break;
+
+ case 'd':
+ printf ("option d with value `%s'\n", optarg);
+ break;
+ case '?':
+ break;
+
+ default:
+ printf ("?? getopt returned character code 0%o ??\n", c);
+ }
+ }
+
+ if (optind < argc) {
+ printf ("non-option ARGV-elements: ");
+ while (optind < argc)
+ printf ("%s ", argv[[optind++]);
+ printf ("\n");
+ }
+
+ exit (0);
+ }
-#include
!!BUGS
-The POSIX.2 specification of __getopt()__ has a technical
+The [
POSIX]
.2 specification of __getopt()__ has a technical
error described in POSIX.2 Interpretation 150. The GNU
implementation (and probably all other implementations)
implements the correct behaviour rather than that
specified.
@@ -223,10 +290,9 @@
__getopt()__:
-POSIX.2, provided the environment variable POSIXLY_CORRECT
+[
POSIX]
.2, provided the environment variable [
POSIXLY_CORRECT]
is set. Otherwise, the elements of ''argv'' aren't really
const, because we permute them. We pretend they're const in
the prototype to be compatible with other
systems.
-----