logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

getopt_long.c (798B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 202405L
  5. #include <getopt.h> // getopt_long
  6. #include <stdio.h> // puts
  7. #include <stdlib.h> // abort
  8. int
  9. main(int argc, char *argv[])
  10. {
  11. int optidx = 0;
  12. char *file = NULL;
  13. // clang-format off
  14. static struct option opts[] = {
  15. {"help", no_argument, 0, 'h'},
  16. {"file", required_argument, 0, 'f'},
  17. {0, 0, 0, 0}
  18. };
  19. // clang-format on
  20. int c = -1;
  21. while((c = getopt_long(argc, argv, ":hf:", opts, &optidx)) != -1)
  22. {
  23. switch(c)
  24. {
  25. case 'h':
  26. puts("Help!");
  27. return 1;
  28. break;
  29. case 'f':
  30. file = optarg;
  31. break;
  32. default:
  33. abort();
  34. }
  35. }
  36. puts(file);
  37. return 0;
  38. }