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.

170 lines
3.3 KiB

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