Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

173 rindas
3.5 KiB

  1. #!/usr/bin/env bash
  2. #
  3. # Summary: Display help for a command
  4. #
  5. # Usage: pyenv help [--usage] COMMAND
  6. #
  7. # Parses and displays help contents from a command's source file.
  8. #
  9. # A command is considered documented if it starts with a comment block
  10. # that has a `Summary:' or `Usage:' section. Usage instructions can
  11. # span multiple lines as long as subsequent lines are indented.
  12. # The remainder of the comment block is displayed as extended
  13. # documentation.
  14. set -e
  15. [ -n "$PYENV_DEBUG" ] && set -x
  16. # Provide pyenv completions
  17. if [ "$1" = "--complete" ]; then
  18. echo --usage
  19. exec pyenv-commands
  20. fi
  21. command_path() {
  22. local command="$1"
  23. command -v pyenv-"$command" || command -v pyenv-sh-"$command" || true
  24. }
  25. extract_initial_comment_block() {
  26. LC_ALL= \
  27. LC_CTYPE=C \
  28. sed -ne "
  29. /^#/ !{
  30. q
  31. }
  32. s/^#$/# /
  33. /^# / {
  34. s/^# //
  35. p
  36. }
  37. "
  38. }
  39. collect_documentation() {
  40. # `tail` prevents "broken pipe" errors due to `head` closing thge pipe without reading everything
  41. # https://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error/642932#642932
  42. $(type -P gawk awk | tail -n +1 | head -1) '
  43. /^Summary:/ {
  44. summary = substr($0, 10)
  45. next
  46. }
  47. /^Usage:/ {
  48. reading_usage = 1
  49. usage = usage "\n" $0
  50. next
  51. }
  52. /^( *$| )/ && reading_usage {
  53. usage = usage "\n" $0
  54. next
  55. }
  56. {
  57. reading_usage = 0
  58. help = help "\n" $0
  59. }
  60. function escape(str) {
  61. gsub(/[`\\$"]/, "\\\\&", str)
  62. return str
  63. }
  64. function trim(str) {
  65. sub(/^\n*/, "", str)
  66. sub(/\n*$/, "", str)
  67. return str
  68. }
  69. END {
  70. if (usage || summary) {
  71. print "summary=\"" escape(summary) "\""
  72. print "usage=\"" escape(trim(usage)) "\""
  73. print "help=\"" escape(trim(help)) "\""
  74. }
  75. }
  76. '
  77. }
  78. documentation_for() {
  79. local filename
  80. filename="$(command_path "$1")"
  81. if [ -n "$filename" ]; then
  82. extract_initial_comment_block < "$filename" | collect_documentation
  83. fi
  84. }
  85. print_summary() {
  86. local command="$1"
  87. local summary usage help
  88. eval "$(documentation_for "$command")"
  89. if [ -n "$summary" ]; then
  90. printf " %-9s %s\n" "$command" "$summary"
  91. fi
  92. }
  93. print_summaries() {
  94. for command; do
  95. print_summary "$command"
  96. done
  97. }
  98. print_help() {
  99. local command="$1"
  100. local summary usage help
  101. eval "$(documentation_for "$command")"
  102. [ -n "$help" ] || help="$summary"
  103. if [ -n "$usage" ] || [ -n "$summary" ]; then
  104. if [ -n "$usage" ]; then
  105. echo "$usage"
  106. else
  107. echo "Usage: pyenv ${command}"
  108. fi
  109. if [ -n "$help" ]; then
  110. echo
  111. echo "$help"
  112. echo
  113. fi
  114. else
  115. echo "Sorry, this command isn't documented yet." >&2
  116. return 1
  117. fi
  118. }
  119. print_usage() {
  120. local command="$1"
  121. local summary usage help
  122. eval "$(documentation_for "$command")"
  123. [ -z "$usage" ] || echo "$usage"
  124. }
  125. unset usage
  126. if [ "$1" = "--usage" ]; then
  127. usage="1"
  128. shift
  129. fi
  130. if [ -z "$1" ] || [ "$1" == "pyenv" ]; then
  131. echo "Usage: pyenv <command> [<args>]"
  132. [ -z "$usage" ] || exit
  133. echo
  134. echo "Some useful pyenv commands are:"
  135. print_summaries $(exec pyenv-commands | sort -u)
  136. echo
  137. echo "See \`pyenv help <command>' for information on a specific command."
  138. echo "For full documentation, see: https://github.com/pyenv/pyenv#readme"
  139. else
  140. command="$1"
  141. if [ -n "$(command_path "$command")" ]; then
  142. if [ -n "$usage" ]; then
  143. print_usage "$command"
  144. else
  145. print_help "$command"
  146. fi
  147. else
  148. echo "pyenv: no such command \`$command'" >&2
  149. exit 1
  150. fi
  151. fi