run-checkopt.c 3.4 KB

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