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

sfdoc 5.8KB

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