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.

172 lines
3.3 KiB

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