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

sfdoc 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. -- \
  35. "Without command specified, will try to display documentation" \
  36. "in man(1) pager."
  37. }
  38. select_mfile() {
  39. #
  40. # Find and/or verify file that holds module $1
  41. #
  42. local module=$1
  43. local mfile
  44. case $module in
  45. */*) mfile="$module" ;;
  46. *) mfile=$(shellfu _select_mfile "$module") ;;
  47. esac
  48. debug -v mfile
  49. test -n "$mfile" || { warn "no such module found: $module"; return 3; }
  50. test -f "$mfile" || { warn "no such file found: $mfile"; return 3; }
  51. echo "$mfile"
  52. }
  53. main() {
  54. local action # what to do
  55. local format # export format
  56. local module # module name or path/to/module.sh
  57. local m # module helper var
  58. local RealModuleName # name to override eg. if accessing via filename
  59. local encoding # encoding
  60. local mpath # path to module file
  61. action=man
  62. format=markdown
  63. encoding=utf8
  64. #shellcheck disable=SC2034
  65. while true; do case "$1" in
  66. -d|--debug) PRETTY_DEBUG=true; shift ;;
  67. -a|--all) SFDOC_SHOW_HIDDEN=true; shift ;;
  68. -I|--include) SHELLFU_PATH="$2:$SHELLFU_PATH"; shift 2 || usage ;;
  69. -l|--ls) action=lsx; shift; break ;;
  70. -L|--lsmod) action=lsm; shift; break ;;
  71. --lsvar) action=lsv; shift; break ;;
  72. --lsfun) action=lsf; shift; break ;;
  73. -e|--export) action=exp; format="$2"; shift 2 || usage; break ;;
  74. --encoding) encoding="$2"; shift 2 || usage ;;
  75. --name) RealModuleName="$2"; shift 2 || usage ;;
  76. -*) usage ;;
  77. *) break ;;
  78. esac done
  79. module="$1"; shift
  80. debug -v SHELLFU_INCLUDE SHELLFU_PATH SFDOC_SHOW_HIDDEN
  81. debug -v action format module RealModuleName
  82. case $action:$module in
  83. lsx:*|lsm:*) true ;;
  84. *:) usage ;;
  85. esac
  86. case $action in
  87. lsm) : ;;
  88. *) mpath=$(select_mfile "$module") || die ;;
  89. esac
  90. case $action in
  91. exp) # --export
  92. grep -qw "$format" <<<manpage,markdown,pod \
  93. || die "unknown format: $format"
  94. sfdoc__export "$format" "${RealModuleName:-$module}" "$mpath"
  95. ;;
  96. lsm) # --lsmod
  97. sfdoc__ls_m | sort
  98. ;;
  99. lsv) # --lsvar MODULE
  100. sfdoc__ls v "$mpath" | sort
  101. ;;
  102. lsf) # --lsfun MODULE
  103. sfdoc__ls f "$mpath" | sort
  104. ;;
  105. lsx) # --ls [MODULE]
  106. case $module in
  107. "") shellfu _list_mfiles ;;
  108. *) echo "$mpath" ;;
  109. esac \
  110. | while read -r m;
  111. do
  112. sfdoc__ls v "$m"
  113. sfdoc__ls f "$m"
  114. done \
  115. | sort
  116. ;;
  117. man)
  118. which pod2man &>/dev/null \
  119. || die "pod2man is missing cannot use man page mode"
  120. sfdoc__export manpage "${RealModuleName:-$module}" "$mpath" \
  121. | man -l -
  122. ;;
  123. esac
  124. }
  125. main "$@"