run.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <ccan/tap/tap.h>
  2. #include <stdlib.h>
  3. #include <ccan/opt/opt.c>
  4. #include <ccan/opt/usage.c>
  5. #include <ccan/opt/helpers.c>
  6. #include <ccan/opt/parse.c>
  7. #include "utils.h"
  8. static void reset_options(void)
  9. {
  10. free(opt_table);
  11. opt_table = NULL;
  12. opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
  13. free(err_output);
  14. err_output = NULL;
  15. }
  16. int main(int argc, char *argv[])
  17. {
  18. const char *myname = argv[0];
  19. plan_tests(215);
  20. /* Simple short arg.*/
  21. opt_register_noarg("-a", test_noarg, NULL, "All");
  22. ok1(parse_args(&argc, &argv, "-a", NULL));
  23. ok1(argc == 1);
  24. ok1(argv[0] == myname);
  25. ok1(argv[1] == NULL);
  26. ok1(test_cb_called == 1);
  27. /* Simple long arg. */
  28. opt_register_noarg("--aaa", test_noarg, NULL, "AAAAll");
  29. ok1(parse_args(&argc, &argv, "--aaa", NULL));
  30. ok1(argc == 1);
  31. ok1(argv[0] == myname);
  32. ok1(argv[1] == NULL);
  33. ok1(test_cb_called == 2);
  34. /* Both long and short args. */
  35. opt_register_noarg("--aaa|-a", test_noarg, NULL, "AAAAAAll");
  36. ok1(parse_args(&argc, &argv, "--aaa", "-a", NULL));
  37. ok1(argc == 1);
  38. ok1(argv[0] == myname);
  39. ok1(argv[1] == NULL);
  40. ok1(test_cb_called == 4);
  41. /* Extra arguments preserved. */
  42. ok1(parse_args(&argc, &argv, "--aaa", "-a", "extra", "args", NULL));
  43. ok1(argc == 3);
  44. ok1(argv[0] == myname);
  45. ok1(strcmp(argv[1], "extra") == 0);
  46. ok1(strcmp(argv[2], "args") == 0);
  47. ok1(test_cb_called == 6);
  48. /* Malformed versions. */
  49. ok1(!parse_args(&argc, &argv, "--aaa=arg", NULL));
  50. ok1(strstr(err_output, ": --aaa: doesn't allow an argument"));
  51. ok1(!parse_args(&argc, &argv, "--aa", NULL));
  52. ok1(strstr(err_output, ": --aa: unrecognized option"));
  53. ok1(!parse_args(&argc, &argv, "--aaargh", NULL));
  54. ok1(strstr(err_output, ": --aaargh: unrecognized option"));
  55. /* Argument variants. */
  56. reset_options();
  57. test_cb_called = 0;
  58. opt_register_arg("-a|--aaa", test_arg, NULL, "aaa", "AAAAAAll");
  59. ok1(parse_args(&argc, &argv, "--aaa", "aaa", NULL));
  60. ok1(argc == 1);
  61. ok1(argv[0] == myname);
  62. ok1(test_cb_called == 1);
  63. ok1(parse_args(&argc, &argv, "--aaa=aaa", NULL));
  64. ok1(argc == 1);
  65. ok1(argv[0] == myname);
  66. ok1(test_cb_called == 2);
  67. ok1(parse_args(&argc, &argv, "-a", "aaa", NULL));
  68. ok1(argc == 1);
  69. ok1(argv[0] == myname);
  70. ok1(test_cb_called == 3);
  71. /* Malformed versions. */
  72. ok1(!parse_args(&argc, &argv, "-a", NULL));
  73. ok1(strstr(err_output, ": -a: requires an argument"));
  74. ok1(!parse_args(&argc, &argv, "--aaa", NULL));
  75. ok1(strstr(err_output, ": --aaa: requires an argument"));
  76. ok1(!parse_args(&argc, &argv, "--aa", NULL));
  77. ok1(strstr(err_output, ": --aa: unrecognized option"));
  78. ok1(!parse_args(&argc, &argv, "--aaargh", NULL));
  79. ok1(strstr(err_output, ": --aaargh: unrecognized option"));
  80. /* Now, tables. */
  81. /* Short table: */
  82. reset_options();
  83. test_cb_called = 0;
  84. opt_register_table(short_table, NULL);
  85. ok1(parse_args(&argc, &argv, "-a", NULL));
  86. ok1(argc == 1);
  87. ok1(argv[0] == myname);
  88. ok1(argv[1] == NULL);
  89. ok1(test_cb_called == 1);
  90. /* This one needs an arg. */
  91. ok1(parse_args(&argc, &argv, "-b", NULL) == false);
  92. ok1(test_cb_called == 1);
  93. ok1(parse_args(&argc, &argv, "-b", "b", NULL));
  94. ok1(argc == 1);
  95. ok1(argv[0] == myname);
  96. ok1(argv[1] == NULL);
  97. ok1(test_cb_called == 2);
  98. /* Long table: */
  99. reset_options();
  100. test_cb_called = 0;
  101. opt_register_table(long_table, NULL);
  102. ok1(parse_args(&argc, &argv, "--ddd", NULL));
  103. ok1(argc == 1);
  104. ok1(argv[0] == myname);
  105. ok1(argv[1] == NULL);
  106. ok1(test_cb_called == 1);
  107. /* This one needs an arg. */
  108. ok1(parse_args(&argc, &argv, "--eee", NULL) == false);
  109. ok1(test_cb_called == 1);
  110. ok1(parse_args(&argc, &argv, "--eee", "eee", NULL));
  111. ok1(argc == 1);
  112. ok1(argv[0] == myname);
  113. ok1(argv[1] == NULL);
  114. ok1(test_cb_called == 2);
  115. /* Short and long, both. */
  116. reset_options();
  117. test_cb_called = 0;
  118. opt_register_table(long_and_short_table, NULL);
  119. ok1(parse_args(&argc, &argv, "-g", NULL));
  120. ok1(argc == 1);
  121. ok1(argv[0] == myname);
  122. ok1(argv[1] == NULL);
  123. ok1(test_cb_called == 1);
  124. ok1(parse_args(&argc, &argv, "--ggg", NULL));
  125. ok1(argc == 1);
  126. ok1(argv[0] == myname);
  127. ok1(argv[1] == NULL);
  128. ok1(test_cb_called == 2);
  129. /* This one needs an arg. */
  130. ok1(parse_args(&argc, &argv, "-h", NULL) == false);
  131. ok1(test_cb_called == 2);
  132. ok1(parse_args(&argc, &argv, "-h", "hhh", NULL));
  133. ok1(argc == 1);
  134. ok1(argv[0] == myname);
  135. ok1(argv[1] == NULL);
  136. ok1(test_cb_called == 3);
  137. ok1(parse_args(&argc, &argv, "--hhh", NULL) == false);
  138. ok1(test_cb_called == 3);
  139. ok1(parse_args(&argc, &argv, "--hhh", "hhh", NULL));
  140. ok1(argc == 1);
  141. ok1(argv[0] == myname);
  142. ok1(argv[1] == NULL);
  143. ok1(test_cb_called == 4);
  144. /* Those will all work as tables. */
  145. test_cb_called = 0;
  146. reset_options();
  147. opt_register_table(subtables, NULL);
  148. ok1(parse_args(&argc, &argv, "-a", NULL));
  149. ok1(argc == 1);
  150. ok1(argv[0] == myname);
  151. ok1(argv[1] == NULL);
  152. ok1(test_cb_called == 1);
  153. /* This one needs an arg. */
  154. ok1(parse_args(&argc, &argv, "-b", NULL) == false);
  155. ok1(test_cb_called == 1);
  156. ok1(parse_args(&argc, &argv, "-b", "b", NULL));
  157. ok1(argc == 1);
  158. ok1(argv[0] == myname);
  159. ok1(argv[1] == NULL);
  160. ok1(test_cb_called == 2);
  161. ok1(parse_args(&argc, &argv, "--ddd", NULL));
  162. ok1(argc == 1);
  163. ok1(argv[0] == myname);
  164. ok1(argv[1] == NULL);
  165. ok1(test_cb_called == 3);
  166. /* This one needs an arg. */
  167. ok1(parse_args(&argc, &argv, "--eee", NULL) == false);
  168. ok1(test_cb_called == 3);
  169. ok1(parse_args(&argc, &argv, "--eee", "eee", NULL));
  170. ok1(argc == 1);
  171. ok1(argv[0] == myname);
  172. ok1(argv[1] == NULL);
  173. ok1(test_cb_called == 4);
  174. /* Short and long, both. */
  175. ok1(parse_args(&argc, &argv, "-g", NULL));
  176. ok1(argc == 1);
  177. ok1(argv[0] == myname);
  178. ok1(argv[1] == NULL);
  179. ok1(test_cb_called == 5);
  180. ok1(parse_args(&argc, &argv, "--ggg", NULL));
  181. ok1(argc == 1);
  182. ok1(argv[0] == myname);
  183. ok1(argv[1] == NULL);
  184. ok1(test_cb_called == 6);
  185. /* This one needs an arg. */
  186. ok1(parse_args(&argc, &argv, "-h", NULL) == false);
  187. ok1(test_cb_called == 6);
  188. ok1(parse_args(&argc, &argv, "-h", "hhh", NULL));
  189. ok1(argc == 1);
  190. ok1(argv[0] == myname);
  191. ok1(argv[1] == NULL);
  192. ok1(test_cb_called == 7);
  193. ok1(parse_args(&argc, &argv, "--hhh", NULL) == false);
  194. ok1(test_cb_called == 7);
  195. ok1(parse_args(&argc, &argv, "--hhh", "hhh", NULL));
  196. ok1(argc == 1);
  197. ok1(argv[0] == myname);
  198. ok1(argv[1] == NULL);
  199. ok1(test_cb_called == 8);
  200. /* Now the tricky one: -? must not be confused with an unknown option */
  201. test_cb_called = 0;
  202. reset_options();
  203. /* glibc's getopt does not handle ? with arguments. */
  204. opt_register_noarg("-?", test_noarg, NULL, "Help");
  205. ok1(parse_args(&argc, &argv, "-?", NULL));
  206. ok1(test_cb_called == 1);
  207. ok1(parse_args(&argc, &argv, "-a", NULL) == false);
  208. ok1(test_cb_called == 1);
  209. ok1(strstr(err_output, ": -a: unrecognized option"));
  210. ok1(parse_args(&argc, &argv, "--aaaa", NULL) == false);
  211. ok1(test_cb_called == 1);
  212. ok1(strstr(err_output, ": --aaaa: unrecognized option"));
  213. test_cb_called = 0;
  214. reset_options();
  215. /* Corner cases involving short arg parsing weirdness. */
  216. opt_register_noarg("-a|--aaa", test_noarg, NULL, "a");
  217. opt_register_arg("-b|--bbb", test_arg, NULL, "bbb", "b");
  218. opt_register_arg("-c|--ccc", test_arg, NULL, "aaa", "c");
  219. /* -aa == -a -a */
  220. ok1(parse_args(&argc, &argv, "-aa", NULL));
  221. ok1(test_cb_called == 2);
  222. ok1(parse_args(&argc, &argv, "-aab", NULL) == false);
  223. ok1(test_cb_called == 4);
  224. ok1(strstr(err_output, ": -b: requires an argument"));
  225. ok1(parse_args(&argc, &argv, "-bbbb", NULL));
  226. ok1(test_cb_called == 5);
  227. ok1(parse_args(&argc, &argv, "-aabbbb", NULL));
  228. ok1(test_cb_called == 8);
  229. ok1(parse_args(&argc, &argv, "-aabbbb", "-b", "bbb", NULL));
  230. ok1(test_cb_called == 12);
  231. ok1(parse_args(&argc, &argv, "-aabbbb", "--bbb", "bbb", NULL));
  232. ok1(test_cb_called == 16);
  233. ok1(parse_args(&argc, &argv, "-aabbbb", "--bbb=bbb", NULL));
  234. ok1(test_cb_called == 20);
  235. ok1(parse_args(&argc, &argv, "-aacaaa", NULL));
  236. ok1(test_cb_called == 23);
  237. ok1(parse_args(&argc, &argv, "-aacaaa", "-a", NULL));
  238. ok1(test_cb_called == 27);
  239. ok1(parse_args(&argc, &argv, "-aacaaa", "--bbb", "bbb", "-aacaaa",
  240. NULL));
  241. ok1(test_cb_called == 34);
  242. test_cb_called = 0;
  243. reset_options();
  244. /* -- and POSIXLY_CORRECT */
  245. opt_register_noarg("-a|--aaa", test_noarg, NULL, "a");
  246. ok1(parse_args(&argc, &argv, "-a", "--", "-a", NULL));
  247. ok1(test_cb_called == 1);
  248. ok1(argc == 2);
  249. ok1(strcmp(argv[1], "-a") == 0);
  250. ok1(!argv[2]);
  251. unsetenv("POSIXLY_CORRECT");
  252. ok1(parse_args(&argc, &argv, "-a", "somearg", "-a", "--", "-a", NULL));
  253. ok1(test_cb_called == 3);
  254. ok1(argc == 3);
  255. ok1(strcmp(argv[1], "somearg") == 0);
  256. ok1(strcmp(argv[2], "-a") == 0);
  257. ok1(!argv[3]);
  258. setenv("POSIXLY_CORRECT", "1", 1);
  259. ok1(parse_args(&argc, &argv, "-a", "somearg", "-a", "--", "-a", NULL));
  260. ok1(test_cb_called == 4);
  261. ok1(argc == 5);
  262. ok1(strcmp(argv[1], "somearg") == 0);
  263. ok1(strcmp(argv[2], "-a") == 0);
  264. ok1(strcmp(argv[3], "--") == 0);
  265. ok1(strcmp(argv[4], "-a") == 0);
  266. ok1(!argv[5]);
  267. /* parse_args allocates argv */
  268. free(argv);
  269. return exit_status();
  270. }