args.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef __ARGS_H__
  2. #define __ARGS_H__
  3. #include <stdlib.h>
  4. #include <argp.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include "io.h"
  8. const char *argp_program_version = "Cider 0.1";
  9. const char *argp_program_bug_address = "https://notabug.org/cryptarch/dice/issues";
  10. /*
  11. OPTIONS. Field 1 in ARGP.
  12. Order of fields: {NAME, KEY, ARG, FLAGS, DOC}.
  13. */
  14. static struct argp_option options[] = {
  15. {"help", 'h', NULL, 0, "Print this help message."},
  16. {"version", 'v', NULL, 0, "Print version information."},
  17. {0}
  18. };
  19. /*
  20. PARSER. Field 2 in ARGP.
  21. Order of parameters: KEY, ARG, STATE.
  22. */
  23. static error_t parse_opt (int key, char *arg, struct argp_state *state) {
  24. struct arguments *arguments = state->input;
  25. switch (key) {
  26. case 'h':
  27. {
  28. argp_state_help(state, stdout, ARGP_HELP_USAGE | ARGP_HELP_LONG | ARGP_HELP_DOC);
  29. exit(EXIT_SUCCESS);
  30. }
  31. break;
  32. case 'v':
  33. {
  34. printf("%s\n", argp_program_version);
  35. exit(0);
  36. }
  37. break;
  38. case ARGP_KEY_ARG:
  39. {
  40. {
  41. arguments->mode = SCRIPTED;
  42. if(0 != strcmp(arg, "-")) {
  43. errno = 0;
  44. arguments->ist = fopen(arg, "r");
  45. if(arguments->ist == NULL) {
  46. fprintf(stderr, "Error %d (%s) opening file %s\n", errno, strerror(errno), arg);
  47. exit(1);
  48. }
  49. }
  50. }
  51. }
  52. break;
  53. default:
  54. return ARGP_ERR_UNKNOWN;
  55. }
  56. return 0;
  57. }
  58. /*
  59. ARGS_DOC. Field 3 in ARGP.
  60. A description of the non-option command-line arguments
  61. that we accept.
  62. */
  63. static char args_doc[] = "[file]";
  64. /*
  65. DOC. Field 4 in ARGP.
  66. Program documentation.
  67. */
  68. static char doc[] = "Convert IPv4 ranges into CIDR form";
  69. /*
  70. The ARGP structure itself.
  71. */
  72. static struct argp argp = {options, parse_opt, args_doc, doc};
  73. #endif // __ARGS_H__