run-usage.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <ccan/tap/tap.h>
  2. #include <stdarg.h>
  3. #include <setjmp.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include "utils.h"
  7. #include <ccan/opt/opt.c>
  8. #include <ccan/opt/usage.c>
  9. #include <ccan/opt/helpers.c>
  10. #include <ccan/opt/parse.c>
  11. static char *my_cb(void *p)
  12. {
  13. return NULL;
  14. }
  15. static void reset_options(void)
  16. {
  17. free(opt_table);
  18. opt_table = NULL;
  19. opt_count = opt_num_short = opt_num_short_arg = opt_num_long = 0;
  20. }
  21. /* Test helpers. */
  22. int main(int argc, char *argv[])
  23. {
  24. char *output;
  25. char *longname = strdup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  26. char *shortname = strdup("shortname");
  27. plan_tests(48);
  28. opt_register_table(subtables, NULL);
  29. opt_register_noarg("--kkk|-k", my_cb, NULL, "magic kkk option");
  30. opt_register_noarg("-?", opt_usage_and_exit, "<MyArgs>...",
  31. "This message");
  32. opt_register_arg("--longname", opt_set_charp, opt_show_charp,
  33. &longname, "a really long option default");
  34. opt_register_arg("--shortname", opt_set_charp, opt_show_charp,
  35. &shortname, "a short option default");
  36. output = opt_usage("my name", "ExTrA Args");
  37. diag("%s", output);
  38. ok1(strstr(output, "Usage: my name"));
  39. ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
  40. ok1(strstr(output, "ExTrA Args"));
  41. ok1(strstr(output, "-a "));
  42. ok1(strstr(output, " Description of a\n"));
  43. ok1(strstr(output, "-b <arg>"));
  44. ok1(strstr(output, " Description of b (default: b)\n"));
  45. ok1(strstr(output, "--ddd "));
  46. ok1(strstr(output, " Description of ddd\n"));
  47. ok1(strstr(output, "--eee <filename> "));
  48. ok1(strstr(output, " (default: eee)\n"));
  49. ok1(strstr(output, "long table options:\n"));
  50. ok1(strstr(output, "--ggg|-g "));
  51. ok1(strstr(output, " Description of ggg\n"));
  52. ok1(strstr(output, "-h|--hhh <arg>"));
  53. ok1(strstr(output, " Description of hhh\n"));
  54. ok1(strstr(output, "--kkk|-k"));
  55. ok1(strstr(output, "magic kkk option"));
  56. /* This entry is hidden. */
  57. ok1(!strstr(output, "--mmm|-m"));
  58. free(output);
  59. /* NULL should use string from registered options. */
  60. output = opt_usage("my name", NULL);
  61. diag("%s", output);
  62. ok1(strstr(output, "Usage: my name"));
  63. ok1(strstr(output, "--jjj|-j|--lll|-l <arg>"));
  64. ok1(strstr(output, "<MyArgs>..."));
  65. ok1(strstr(output, "-a "));
  66. ok1(strstr(output, " Description of a\n"));
  67. ok1(strstr(output, "-b <arg>"));
  68. ok1(strstr(output, " Description of b (default: b)\n"));
  69. ok1(strstr(output, "--ddd "));
  70. ok1(strstr(output, " Description of ddd\n"));
  71. ok1(strstr(output, "--eee <filename> "));
  72. ok1(strstr(output, " (default: eee)\n"));
  73. ok1(strstr(output, "long table options:\n"));
  74. ok1(strstr(output, "--ggg|-g "));
  75. ok1(strstr(output, " Description of ggg\n"));
  76. ok1(strstr(output, "-h|--hhh <arg>"));
  77. ok1(strstr(output, " Description of hhh\n"));
  78. ok1(strstr(output, "--kkk|-k"));
  79. ok1(strstr(output, "magic kkk option"));
  80. ok1(strstr(output, "--longname"));
  81. ok1(strstr(output, "a really long option default"));
  82. ok1(strstr(output, "(default: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"...)"));
  83. ok1(strstr(output, "--shortname"));
  84. ok1(strstr(output, "a short option default"));
  85. ok1(strstr(output, "(default: \"shortname\")"));
  86. /* This entry is hidden. */
  87. ok1(!strstr(output, "--mmm|-m"));
  88. free(output);
  89. reset_options();
  90. /* Empty table test. */
  91. output = opt_usage("nothing", NULL);
  92. ok1(strstr(output, "Usage: nothing \n"));
  93. free(output);
  94. /* No short args. */
  95. opt_register_noarg("--aaa", test_noarg, NULL, "AAAAll");
  96. output = opt_usage("onearg", NULL);
  97. ok1(strstr(output, "Usage: onearg \n"));
  98. ok1(strstr(output, "--aaa"));
  99. ok1(strstr(output, "AAAAll"));
  100. free(output);
  101. free(shortname);
  102. free(longname);
  103. return exit_status();
  104. }