run-checkopt.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <ccan/tap/tap.h>
  4. #include <setjmp.h>
  5. #include <stdlib.h>
  6. #include <limits.h>
  7. #include <err.h>
  8. #include "utils.h"
  9. /* We don't actually want it to exit... */
  10. static jmp_buf exited;
  11. #define errx save_and_jump
  12. static void save_and_jump(int ecode, const char *fmt, ...);
  13. #include <ccan/opt/helpers.c>
  14. #include <ccan/opt/opt.c>
  15. #include <ccan/opt/usage.c>
  16. #include <ccan/opt/parse.c>
  17. static char *output = NULL;
  18. static int saved_vprintf(const char *fmt, va_list ap)
  19. {
  20. char *p;
  21. int ret = vasprintf(&p, fmt, ap);
  22. if (output) {
  23. output = realloc(output, strlen(output) + strlen(p) + 1);
  24. strcat(output, p);
  25. free(p);
  26. } else
  27. output = p;
  28. return ret;
  29. }
  30. static void save_and_jump(int ecode, const char *fmt, ...)
  31. {
  32. va_list ap;
  33. va_start(ap, fmt);
  34. saved_vprintf(fmt, ap);
  35. va_end(ap);
  36. longjmp(exited, ecode + 1);
  37. }
  38. static void reset(void)
  39. {
  40. free(output);
  41. output = NULL;
  42. free(opt_table);
  43. opt_table = NULL;
  44. opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
  45. }
  46. int main(int argc, char *argv[])
  47. {
  48. int exitval;
  49. plan_tests(14);
  50. exitval = setjmp(exited);
  51. if (exitval == 0) {
  52. /* Bad type. */
  53. _opt_register("-a", OPT_SUBTABLE, (void *)opt_version_and_exit,
  54. NULL, NULL, "1.2.3", "");
  55. fail("_opt_register returned?");
  56. } else {
  57. ok1(exitval - 1 == 1);
  58. ok1(strstr(output, "Option -a: unknown entry type"));
  59. }
  60. reset();
  61. exitval = setjmp(exited);
  62. if (exitval == 0) {
  63. /* NULL description. */
  64. opt_register_noarg("-a", test_noarg, "", NULL);
  65. fail("_opt_register returned?");
  66. } else {
  67. ok1(exitval - 1 == 1);
  68. ok1(strstr(output, "Option -a: description cannot be NULL"));
  69. }
  70. reset();
  71. exitval = setjmp(exited);
  72. if (exitval == 0) {
  73. /* Bad option name. */
  74. opt_register_noarg("a", test_noarg, "", "");
  75. fail("_opt_register returned?");
  76. } else {
  77. ok1(exitval - 1 == 1);
  78. ok1(strstr(output, "Option a: does not begin with '-'"));
  79. }
  80. reset();
  81. exitval = setjmp(exited);
  82. if (exitval == 0) {
  83. /* Bad option name. */
  84. opt_register_noarg("--", test_noarg, "", "");
  85. fail("_opt_register returned?");
  86. } else {
  87. ok1(exitval - 1 == 1);
  88. ok1(strstr(output, "Option --: invalid long option '--'"));
  89. }
  90. reset();
  91. exitval = setjmp(exited);
  92. if (exitval == 0) {
  93. /* Bad option name. */
  94. opt_register_noarg("--a|-aaa", test_noarg, "", "");
  95. fail("_opt_register returned?");
  96. } else {
  97. ok1(exitval - 1 == 1);
  98. ok1(strstr(output,
  99. "Option --a|-aaa: invalid short option '-aaa'"));
  100. }
  101. reset();
  102. exitval = setjmp(exited);
  103. if (exitval == 0) {
  104. /* Documentation for non-optios. */
  105. opt_register_noarg("--a foo", test_noarg, "", "");
  106. fail("_opt_register returned?");
  107. } else {
  108. ok1(exitval - 1 == 1);
  109. ok1(strstr(output,
  110. "Option --a foo: does not take arguments 'foo'"));
  111. }
  112. reset();
  113. exitval = setjmp(exited);
  114. if (exitval == 0) {
  115. /* Documentation for non-optios. */
  116. opt_register_noarg("--a=foo", test_noarg, "", "");
  117. fail("_opt_register returned?");
  118. } else {
  119. ok1(exitval - 1 == 1);
  120. ok1(strstr(output,
  121. "Option --a=foo: does not take arguments 'foo'"));
  122. }
  123. return exit_status();
  124. }