You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
3.3 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. sed -ne "
  27. /^#/ !{
  28. q
  29. }
  30. s/^#$/# /
  31. /^# / {
  32. s/^# //
  33. p
  34. }
  35. "
  36. }
  37. collect_documentation() {
  38. $(type -p gawk awk | head -1) '
  39. /^Summary:/ {
  40. summary = substr($0, 10)
  41. next
  42. }
  43. /^Usage:/ {
  44. reading_usage = 1
  45. usage = usage "\n" $0
  46. next
  47. }
  48. /^( *$| )/ && reading_usage {
  49. usage = usage "\n" $0
  50. next
  51. }
  52. {
  53. reading_usage = 0
  54. help = help "\n" $0
  55. }
  56. function escape(str) {
  57. gsub(/[`\\$"]/, "\\\\&", str)
  58. return str
  59. }
  60. function trim(str) {
  61. sub(/^\n*/, "", str)
  62. sub(/\n*$/, "", str)
  63. return str
  64. }
  65. END {
  66. if (usage || summary) {
  67. print "summary=\"" escape(summary) "\""
  68. print "usage=\"" escape(trim(usage)) "\""
  69. print "help=\"" escape(trim(help)) "\""
  70. }
  71. }
  72. '
  73. }
  74. documentation_for() {
  75. local filename="$(command_path "$1")"
  76. if [ -n "$filename" ]; then
  77. extract_initial_comment_block < "$filename" | collect_documentation
  78. fi
  79. }
  80. print_summary() {
  81. local command="$1"
  82. local summary usage help
  83. eval "$(documentation_for "$command")"
  84. if [ -n "$summary" ]; then
  85. printf " %-9s %s\n" "$command" "$summary"
  86. fi
  87. }
  88. print_summaries() {
  89. for command; do
  90. print_summary "$command"
  91. done
  92. }
  93. print_help() {
  94. local command="$1"
  95. local summary usage help
  96. eval "$(documentation_for "$command")"
  97. [ -n "$help" ] || help="$summary"
  98. if [ -n "$usage" -o -n "$summary" ]; then
  99. if [ -n "$usage" ]; then
  100. echo "$usage"
  101. else
  102. echo "Usage: pyenv ${command}"
  103. fi
  104. if [ -n "$help" ]; then
  105. echo
  106. echo "$help"
  107. echo
  108. fi
  109. else
  110. echo "Sorry, this command isn't documented yet." >&2
  111. return 1
  112. fi
  113. }
  114. print_usage() {
  115. local command="$1"
  116. local summary usage help
  117. eval "$(documentation_for "$command")"
  118. [ -z "$usage" ] || echo "$usage"
  119. }
  120. unset usage
  121. if [ "$1" = "--usage" ]; then
  122. usage="1"
  123. shift
  124. fi
  125. if [ -z "$1" ] || [ "$1" == "pyenv" ]; then
  126. echo "Usage: pyenv <command> [<args>]"
  127. [ -z "$usage" ] || exit
  128. echo
  129. echo "Some useful pyenv commands are:"
  130. print_summaries commands local global shell install uninstall rehash version versions which whence
  131. echo
  132. echo "See \`pyenv help <command>' for information on a specific command."
  133. echo "For full documentation, see: https://github.com/yyuu/pyenv#readme"
  134. else
  135. command="$1"
  136. if [ -n "$(command_path "$command")" ]; then
  137. if [ -n "$usage" ]; then
  138. print_usage "$command"
  139. else
  140. print_help "$command"
  141. fi
  142. else
  143. echo "pyenv: no such command \`$command'" >&2
  144. exit 1
  145. fi
  146. fi