COOPY » Guide
version 0.6.5
|
00001 /* getopt_long and getopt_long_only entry points for GNU getopt. 00002 Copyright (C) 1987-1994, 1996-1998, 2004, 2006, 2009-2012 Free Software 00003 Foundation, Inc. 00004 This file is part of the GNU C Library. 00005 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 3 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00018 00019 #include <gnulib_getopt/getopt.h> 00020 #include <gnulib_getopt/getopt_int.h> 00021 00022 #include <stdio.h> 00023 00024 /* This needs to come after some library #include 00025 to get __GNU_LIBRARY__ defined. */ 00026 #ifdef __GNU_LIBRARY__ 00027 #include <stdlib.h> 00028 #endif 00029 00030 #ifndef NULL 00031 #define NULL 0 00032 #endif 00033 00034 int 00035 gnulib_getopt_long (int argc, char *__getopt_argv_const *argv, const char *options, 00036 const struct option *long_options, int *opt_index) 00037 { 00038 return gnulib_getopt_internal (argc, (char **) argv, options, long_options, 00039 opt_index, 0, 0); 00040 } 00041 00042 int 00043 _getopt_long_r (int argc, char **argv, const char *options, 00044 const struct option *long_options, int *opt_index, 00045 struct _getopt_data *d) 00046 { 00047 return _getopt_internal_r (argc, argv, options, long_options, opt_index, 00048 0, d, 0); 00049 } 00050 00051 /* Like getopt_long, but '-' as well as '--' can indicate a long option. 00052 If an option that starts with '-' (not '--') doesn't match a long option, 00053 but does match a short option, it is parsed as a short option 00054 instead. */ 00055 00056 int 00057 getopt_long_only (int argc, char *__getopt_argv_const *argv, 00058 const char *options, 00059 const struct option *long_options, int *opt_index) 00060 { 00061 return gnulib_getopt_internal (argc, (char **) argv, options, long_options, 00062 opt_index, 1, 0); 00063 } 00064 00065 int 00066 _getopt_long_only_r (int argc, char **argv, const char *options, 00067 const struct option *long_options, int *opt_index, 00068 struct _getopt_data *d) 00069 { 00070 return _getopt_internal_r (argc, argv, options, long_options, opt_index, 00071 1, d, 0); 00072 } 00073 00074 00075 #ifdef TEST 00076 00077 #include <stdio.h> 00078 00079 int 00080 main (int argc, char **argv) 00081 { 00082 int c; 00083 int digit_optind = 0; 00084 00085 while (1) 00086 { 00087 int this_option_optind = optind ? optind : 1; 00088 int option_index = 0; 00089 static const struct option long_options[] = 00090 { 00091 {"add", 1, 0, 0}, 00092 {"append", 0, 0, 0}, 00093 {"delete", 1, 0, 0}, 00094 {"verbose", 0, 0, 0}, 00095 {"create", 0, 0, 0}, 00096 {"file", 1, 0, 0}, 00097 {0, 0, 0, 0} 00098 }; 00099 00100 c = getopt_long (argc, argv, "abc:d:0123456789", 00101 long_options, &option_index); 00102 if (c == -1) 00103 break; 00104 00105 switch (c) 00106 { 00107 case 0: 00108 printf ("option %s", long_options[option_index].name); 00109 if (optarg) 00110 printf (" with arg %s", optarg); 00111 printf ("\n"); 00112 break; 00113 00114 case '0': 00115 case '1': 00116 case '2': 00117 case '3': 00118 case '4': 00119 case '5': 00120 case '6': 00121 case '7': 00122 case '8': 00123 case '9': 00124 if (digit_optind != 0 && digit_optind != this_option_optind) 00125 printf ("digits occur in two different argv-elements.\n"); 00126 digit_optind = this_option_optind; 00127 printf ("option %c\n", c); 00128 break; 00129 00130 case 'a': 00131 printf ("option a\n"); 00132 break; 00133 00134 case 'b': 00135 printf ("option b\n"); 00136 break; 00137 00138 case 'c': 00139 printf ("option c with value '%s'\n", optarg); 00140 break; 00141 00142 case 'd': 00143 printf ("option d with value '%s'\n", optarg); 00144 break; 00145 00146 case '?': 00147 break; 00148 00149 default: 00150 printf ("?? getopt returned character code 0%o ??\n", c); 00151 } 00152 } 00153 00154 if (optind < argc) 00155 { 00156 printf ("non-option ARGV-elements: "); 00157 while (optind < argc) 00158 printf ("%s ", argv[optind++]); 00159 printf ("\n"); 00160 } 00161 00162 exit (0); 00163 } 00164 00165 #endif /* TEST */