usage.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <ccan/opt/opt.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include "private.h"
  7. /* We only use this for pointer comparisons. */
  8. const char opt_table_hidden[1];
  9. static unsigned write_short_options(char *str)
  10. {
  11. unsigned int i, num = 0;
  12. for (i = 0; i < opt_count; i++) {
  13. if (opt_table[i].flags == OPT_SUBTABLE) {
  14. if (opt_table[i].desc == opt_table_hidden) {
  15. /* Skip these options. */
  16. i += (intptr_t)opt_table[i].arg - 1;
  17. continue;
  18. }
  19. } else if (opt_table[i].shortopt)
  20. str[num++] = opt_table[i].shortopt;
  21. }
  22. return num;
  23. }
  24. #define OPT_SPACE_PAD " "
  25. /* FIXME: Get all purdy. */
  26. char *opt_usage(const char *argv0, const char *extra)
  27. {
  28. unsigned int i, num, len;
  29. char *ret, *p;
  30. /* An overestimate of our length. */
  31. len = strlen("Usage: %s ") + strlen(argv0)
  32. + strlen("[-%.*s]") + opt_count + 1
  33. + strlen(" ") + strlen(extra)
  34. + strlen("\n");
  35. for (i = 0; i < opt_count; i++) {
  36. if (opt_table[i].flags == OPT_SUBTABLE) {
  37. len += strlen("\n") + strlen(opt_table[i].desc)
  38. + strlen(":\n");
  39. } else {
  40. len += strlen("--%s/-%c") + strlen(" <arg>");
  41. if (opt_table[i].longopt)
  42. len += strlen(opt_table[i].longopt);
  43. if (opt_table[i].desc) {
  44. len += strlen(OPT_SPACE_PAD)
  45. + strlen(opt_table[i].desc) + 1;
  46. }
  47. if (opt_table[i].show) {
  48. len += strlen("(default: %s)")
  49. + OPT_SHOW_LEN + sizeof("...");
  50. }
  51. len += strlen("\n");
  52. }
  53. }
  54. p = ret = malloc(len);
  55. if (!ret)
  56. return NULL;
  57. p += sprintf(p, "Usage: %s", argv0);
  58. p += sprintf(p, " [-");
  59. num = write_short_options(p);
  60. if (num) {
  61. p += num;
  62. p += sprintf(p, "]");
  63. } else {
  64. /* Remove start of single-entry options */
  65. p -= 3;
  66. }
  67. if (extra)
  68. p += sprintf(p, " %s", extra);
  69. p += sprintf(p, "\n");
  70. for (i = 0; i < opt_count; i++) {
  71. if (opt_table[i].flags == OPT_SUBTABLE) {
  72. if (opt_table[i].desc == opt_table_hidden) {
  73. /* Skip these options. */
  74. i += (intptr_t)opt_table[i].arg - 1;
  75. continue;
  76. }
  77. p += sprintf(p, "%s:\n", opt_table[i].desc);
  78. continue;
  79. }
  80. if (opt_table[i].shortopt && opt_table[i].longopt)
  81. len = sprintf(p, "--%s/-%c",
  82. opt_table[i].longopt,
  83. opt_table[i].shortopt);
  84. else if (opt_table[i].shortopt)
  85. len = sprintf(p, "-%c", opt_table[i].shortopt);
  86. else
  87. len = sprintf(p, "--%s", opt_table[i].longopt);
  88. if (opt_table[i].flags == OPT_HASARG)
  89. len += sprintf(p + len, " <arg>");
  90. if (opt_table[i].desc || opt_table[i].show)
  91. len += sprintf(p + len, "%.*s",
  92. len < strlen(OPT_SPACE_PAD)
  93. ? strlen(OPT_SPACE_PAD) - len : 1,
  94. OPT_SPACE_PAD);
  95. if (opt_table[i].desc)
  96. len += sprintf(p + len, "%s", opt_table[i].desc);
  97. if (opt_table[i].show) {
  98. char buf[OPT_SHOW_LEN + sizeof("...")];
  99. strcpy(buf + OPT_SHOW_LEN, "...");
  100. opt_table[i].show(buf, opt_table[i].arg);
  101. len += sprintf(p + len, "%s(default: %s)",
  102. opt_table[i].desc ? " " : "", buf);
  103. }
  104. p += len;
  105. p += sprintf(p, "\n");
  106. }
  107. *p = '\0';
  108. return ret;
  109. }