Guest User

Untitled

a guest
Jun 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <getopt.h>
  5.  
  6. void get_short_option(int argc, char **argv)
  7. {
  8. int c;
  9. int i;
  10.  
  11. opterr = 0;
  12.  
  13. for (i = 0; i < argc; i++)
  14. {
  15. // check short option format (with length of 2)
  16. if (strstr(argv[i], "-") != NULL && strlen(argv[i]) != 2)
  17. {
  18. puts("wrong option format");
  19. return;
  20. }
  21. }
  22.  
  23. // multiple short options
  24. while(1)
  25. {
  26. c = getopt(argc, argv, "hs:m:");
  27.  
  28. if (c == -1)
  29. {
  30. break;
  31. }
  32.  
  33. switch (c)
  34. {
  35. // allow no argument
  36. case 'h':
  37. printf("option %c\n", c);
  38. break;
  39.  
  40. // allow single argument
  41. case 's':
  42. printf("option %c with arg '%s'\n", c, optarg);
  43. break;
  44.  
  45. // allow unlimitted arguments
  46. case 'm':
  47. optind--;
  48. while (optind < argc)
  49. {
  50. if (strstr(argv[optind], "-") != NULL)
  51. {
  52. break;
  53. }
  54. printf("option %c with arg '%s'\n", c, argv[optind++]);
  55. }
  56. break;
  57.  
  58. case '?':
  59. puts("invalid option");
  60. return;
  61.  
  62. default:
  63. printf("?? getopt returned character code 0%o ??\n", c);
  64. }
  65. }
  66.  
  67. if (optind < argc)
  68. {
  69. printf("non-option ARGV-elements: ");
  70. while (optind < argc)
  71. {
  72. printf("%s ", argv[optind++]);
  73. }
  74. printf("\n");
  75. }
  76.  
  77. return;
  78. }
  79.  
  80. void get_long_option(int argc, char **argv)
  81. {
  82. int c;
  83. int i;
  84. int index = 0;
  85. int do_add, do_append, do_delete, flag; /* flag variables */
  86.  
  87. // struct option {
  88. // const char *name;
  89. // int has_arg;
  90. // int *flag;
  91. // int val;
  92. // };
  93.  
  94. struct option long_options[] = {
  95. {"add", no_argument, &do_add, 1 }, // set do_add to 1
  96. {"append", required_argument, &do_append, 1 }, // set do_append to 1
  97. {"delete", optional_argument, &do_delete, 1 }, // set do_delete to 1
  98. {"setflag", no_argument, &flag, 1 }, // set flag to 1
  99. {"clrfalg", no_argument, &flag, 0 }, // set flag to 0
  100. {"verbose", no_argument, 0, 'v' }, // "--verbose" returns "-v"
  101. {"file", required_argument, 0, 'f' }, // "--file" returns "-f"
  102. {0, 0, 0, 0 } // null termination
  103. };
  104.  
  105. opterr = 0;
  106.  
  107. for (i = 0; i < argc; i++)
  108. {
  109. // check short option format (with length of 2)
  110. if (strstr(argv[i], "--") == NULL && strstr(argv[i], "-") != NULL && strlen(argv[i]) != 2)
  111. {
  112. puts("wrong option format");
  113. return;
  114. }
  115. }
  116.  
  117. while(1)
  118. {
  119. c = getopt_long(argc, argv, ":v:f:", long_options, &index);
  120.  
  121. if (c == -1)
  122. {
  123. break;
  124. }
  125.  
  126. switch (c)
  127. {
  128. case 0:
  129. printf("option %s", long_options[index].name);
  130. if (optarg)
  131. {
  132. printf(" with arg:");
  133. optind--;
  134. while (optind < argc)
  135. {
  136. if (strstr(argv[optind], "-") != NULL)
  137. {
  138. break;
  139. }
  140. printf(" %s", argv[optind++]);
  141. }
  142. }
  143. if (long_options[index].flag != NULL)
  144. {
  145. printf(" (flag is set to %d)", long_options[index].val);
  146. }
  147. printf("\n");
  148. break;
  149.  
  150. case 'v':
  151. case 'f':
  152. printf("option %c", c);
  153. if (optarg)
  154. {
  155. printf(" with arg:");
  156. optind--;
  157. while (optind < argc)
  158. {
  159. if (strstr(argv[optind], "-") != NULL)
  160. {
  161. break;
  162. }
  163. printf(" %s", argv[optind++]);
  164. }
  165. }
  166. printf("\n");
  167. break;
  168.  
  169. case ':':
  170. puts("missing argument");
  171. return;
  172.  
  173. case '?':
  174. puts("invalid option");
  175. return;
  176.  
  177. default:
  178. printf("?? getopt returned character code 0%o ??\n", c);
  179. }
  180. }
  181.  
  182. if (optind < argc)
  183. {
  184. printf("non-option ARGV-elements: ");
  185. while (optind < argc)
  186. {
  187. printf("%s ", argv[optind++]);
  188. }
  189. printf("\n");
  190. }
  191.  
  192. return;
  193. }
  194.  
  195. int main(int argc, char **argv)
  196. {
  197. get_short_option(argc, argv);
  198. // get_long_option(argc, argv);
  199.  
  200. return 0;
  201. }
Add Comment
Please, Sign In to add comment