getopt.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Getopt tests */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <getopt.h>
  6. int main (int argc, char **argv)
  7. {
  8. int c;
  9. int digit_optind = 0;
  10. while (1)
  11. {
  12. int this_option_optind = optind ? optind : 1;
  13. c = getopt (argc, argv, "abc:d:0123456789");
  14. if (c == EOF)
  15. break;
  16. switch (c)
  17. {
  18. case '0':
  19. case '1':
  20. case '2':
  21. case '3':
  22. case '4':
  23. case '5':
  24. case '6':
  25. case '7':
  26. case '8':
  27. case '9':
  28. if (digit_optind != 0 && digit_optind != this_option_optind)
  29. printf ("digits occur in two different argv-elements.\n");
  30. digit_optind = this_option_optind;
  31. printf ("option %c\n", c);
  32. break;
  33. case 'a':
  34. printf ("option a\n");
  35. break;
  36. case 'b':
  37. printf ("option b\n");
  38. break;
  39. case 'c':
  40. printf ("option c with value `%s'\n", optarg);
  41. break;
  42. case '?':
  43. break;
  44. default:
  45. printf ("?? getopt returned character code 0%o ??\n", c);
  46. }
  47. }
  48. if (optind < argc)
  49. {
  50. printf ("non-option ARGV-elements: ");
  51. while (optind < argc)
  52. printf ("%s ", argv[optind++]);
  53. printf ("\n");
  54. }
  55. exit (0);
  56. }