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.

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