shell dot on steroids https://pagure.io/shellfu

sfdoc 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. shellfu import sfdoc
  6. usage() {
  7. mkusage \
  8. "[options] MODULE" \
  9. "[options] --ls [MODULE]" \
  10. "[options] --ls{var|fun} MODULE" \
  11. "[options] --lsmod" \
  12. "[options] --export FMT MODULE" \
  13. -c \
  14. "-l, --ls [MODULE] list contents of MODULE if specified," \
  15. " otherwise of all modules" \
  16. "-L, --lsmod show list of modules" \
  17. " --lsfun MODULE show list of functions in MODULE" \
  18. " --lsvar MODULE show list of variables in MODULE" \
  19. "-e, --export FMT MODULE export MODULE documentation in" \
  20. " format FMT: 'markdown', 'manpage' and" \
  21. " 'pod' are supported" \
  22. -o \
  23. "-d, --debug Turn on debugging" \
  24. "-a, --all Don't ignore hidden (starting with" \
  25. " underscore) modules or objects" \
  26. "--encoding Override encoding of the source text" \
  27. " (default: utf8)" \
  28. "--name Override module name (useful when" \
  29. " exporting from a file and the filename" \
  30. " is not helpful)" \
  31. "-I PTH, --include PTH Include path PTH when searching" \
  32. " for modules; can be specified multiple" \
  33. " times." \
  34. "-o PTH, --only-from PTH Searcho only under path PTH." \
  35. -- \
  36. "Without command specified, will try to display documentation" \
  37. "in man(1) pager."
  38. }
  39. select_mfile() {
  40. #
  41. # Find and/or verify file that holds module $1
  42. #
  43. local module=$1
  44. local mfile
  45. case $module in
  46. */*) mfile="$module" ;;
  47. *) mfile=$(shellfu _select_mfile "$module") ;;
  48. esac
  49. debug -v mfile
  50. test -n "$mfile" || { warn "no such module found: $module"; return 3; }
  51. test -f "$mfile" || { warn "no such file found: $mfile"; return 3; }
  52. echo "$mfile"
  53. }
  54. main() {
  55. local action # what to do
  56. local format # export format
  57. local module # module name or path/to/module.sh
  58. local m # module helper var
  59. local RealModuleName # name to override eg. if accessing via filename
  60. local encoding # encoding
  61. local mpath # path to module file
  62. action=man
  63. format=markdown
  64. encoding=utf8
  65. #shellcheck disable=SC2034
  66. while true; do case "$1" in
  67. -d|--debug) PRETTY_DEBUG=true; shift ;;
  68. -a|--all) SFDOC_SHOW_HIDDEN=true; shift ;;
  69. -I|--include) SHELLFU_PATH="$2:$SHELLFU_PATH"; shift 2 || usage ;;
  70. -l|--ls) action=lsx; shift; break ;;
  71. -L|--lsmod) action=lsm; shift; break ;;
  72. -o|--only-from) SHELLFU_PATH="$2"; SHELLFU_INCLUDE=""; shift 2 || usage ;;
  73. --lsvar) action=lsv; shift; break ;;
  74. --lsfun) action=lsf; shift; break ;;
  75. -e|--export) action=exp; format="$2"; shift 2 || usage; break ;;
  76. --encoding) encoding="$2"; shift 2 || usage ;;
  77. --name) RealModuleName="$2"; shift 2 || usage ;;
  78. -*) usage ;;
  79. *) break ;;
  80. esac done
  81. module="$1"; shift
  82. debug -v SHELLFU_INCLUDE SHELLFU_PATH SFDOC_SHOW_HIDDEN
  83. debug -v action format module RealModuleName
  84. case $action:$module in
  85. lsx:*|lsm:*) true ;;
  86. *:) usage ;;
  87. esac
  88. case $action in
  89. lsm) : ;;
  90. *) mpath=$(select_mfile "$module") || die ;;
  91. esac
  92. case $action in
  93. exp) # --export
  94. grep -qw "$format" <<<manpage,markdown,pod \
  95. || die "unknown format: $format"
  96. sfdoc__export "$format" "${RealModuleName:-$module}" "$mpath"
  97. ;;
  98. lsm) # --lsmod
  99. sfdoc__ls_m | LC_ALL=C sort
  100. ;;
  101. lsv) # --lsvar MODULE
  102. sfdoc__ls v "$mpath" | LC_ALL=C sort
  103. ;;
  104. lsf) # --lsfun MODULE
  105. sfdoc__ls f "$mpath" | LC_ALL=C sort
  106. ;;
  107. lsx) # --ls [MODULE]
  108. case $module in
  109. "") shellfu _list_mfiles ;;
  110. *) echo "$mpath" ;;
  111. esac \
  112. | while read -r m;
  113. do
  114. sfdoc__ls v "$m"
  115. sfdoc__ls f "$m"
  116. done \
  117. | LC_ALL=C sort
  118. ;;
  119. man)
  120. which pod2man &>/dev/null \
  121. || die "pod2man is missing cannot use man page mode"
  122. sfdoc__export manpage "${RealModuleName:-$module}" "$mpath" \
  123. | man -l -
  124. ;;
  125. esac
  126. }
  127. main "$@"