logo

utils-std

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

getopt_long.c (795B)


  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. for(int c = -1;(c = getopt_long(argc, argv, ":hf:", opts, &optidx)) != -1;)
  21. {
  22. switch(c)
  23. {
  24. case 'h':
  25. puts("Help!");
  26. return 1;
  27. break;
  28. case 'f':
  29. file = optarg;
  30. break;
  31. default:
  32. abort();
  33. }
  34. }
  35. puts(file);
  36. return 0;
  37. }