run-usage.c 3.9 KB

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