1 /* 2 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 16 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 17 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <getopt.h> 29 30 /* 31 * Simple getopt_long() and getopt_long_only() excerciser. 32 * ENVIRONMENT: 33 * LONG_ONLY : use getopt_long_only() (default is getopt_long()) 34 * POSIXLY_CORRECT : don't permute args 35 */ 36 37 int 38 main(int argc, char **argv) 39 { 40 int ch, idx, goggles; 41 int (*gl)(int, char * const *, const char *, const struct option *, int *); 42 struct option longopts[] = { 43 { "force", no_argument, 0, 0 }, 44 { "fast", no_argument, 0, '1' }, 45 { "best", no_argument, 0, '9' }, 46 { "input", required_argument, 0, 'i' }, 47 { "illiterate", no_argument, 0, 0 }, 48 { "drinking", required_argument, &goggles, 42 }, 49 { "help", no_argument, 0, 'h' }, 50 { 0, 0, 0, 0 }, 51 }; 52 53 if (getenv("LONG_ONLY")) { 54 gl = getopt_long_only; 55 printf("getopt_long_only"); 56 } else { 57 gl = getopt_long; 58 printf("getopt_long"); 59 } 60 if (getenv("POSIXLY_CORRECT")) 61 printf(" (POSIXLY_CORRECT)"); 62 printf(": "); 63 for (idx = 1; idx < argc; idx++) 64 printf("%s ", argv[idx]); 65 printf("\n"); 66 67 goggles = 0; 68 for (;;) { 69 idx = -1; 70 ch = gl(argc, argv, "19bf:i:hW;-", longopts, &idx); 71 if (ch == -1) 72 break; 73 switch (ch) { 74 case 0: 75 case '1': 76 case '9': 77 case 'h': 78 case 'b': 79 case '-': 80 if (idx != -1) { 81 if (goggles == 42) 82 printf("option %s, arg %s\n", 83 longopts[idx].name, optarg); 84 else 85 printf("option %s\n", 86 longopts[idx].name); 87 } else 88 printf("option %c\n", ch); 89 break; 90 case 'f': 91 case 'i': 92 if (idx != -1) 93 printf("option %s, arg %s\n", 94 longopts[idx].name, optarg); 95 else 96 printf("option %c, arg %s\n", ch, optarg); 97 break; 98 99 case '?': 100 break; 101 102 default: 103 printf("unexpected return value: %c\n", ch); 104 break; 105 } 106 } 107 argc -= optind; 108 argv += optind; 109 110 if (argc > 0) { 111 printf("remaining ARGV: "); 112 while (argc--) 113 printf("%s ", *argv++); 114 printf("\n"); 115 } 116 printf("\n"); 117 118 exit (0); 119 } 120