| 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 | |
| 15 | set -e |
| 16 | [ -n "$PYENV_DEBUG" ] && set -x |
| 17 | |
| 18 | # Provide pyenv completions |
| 19 | if [ "$1" = "--complete" ]; then |
| 20 | echo --usage |
| 21 | exec pyenv-commands |
| 22 | fi |
| 23 | |
| 24 | command_path() { |
| 25 | local command="$1" |
| 26 | command -v pyenv-"$command" || command -v pyenv-sh-"$command" || true |
| 27 | } |
| 28 | |
| 29 | extract_initial_comment_block() { |
| 30 | LC_ALL= \ |
| 31 | LC_CTYPE=C \ |
| 32 | sed -ne " |
| 33 | /^#/ !{ |
| 34 | q |
| 35 | } |
| 36 | |
| 37 | s/^#$/# / |
| 38 | |
| 39 | /^# / { |
| 40 | s/^# // |
| 41 | p |
| 42 | } |
| 43 | " |
| 44 | } |
| 45 | |
| 46 | collect_documentation() { |
| 47 | # `tail` prevents "broken pipe" errors due to `head` closing the pipe without reading everything |
| 48 | # https://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error/642932#642932 |
| 49 | $(type -P gawk awk | tail -n +1 | head -n1) ' |
| 50 | /^Summary:/ { |
| 51 | summary = substr($0, 10) |
| 52 | next |
| 53 | } |
| 54 | |
| 55 | /^Usage:/ { |
| 56 | reading_usage = 1 |
| 57 | usage = usage "\n" $0 |
| 58 | next |
| 59 | } |
| 60 | |
| 61 | /^( *$| )/ && reading_usage { |
| 62 | usage = usage "\n" $0 |
| 63 | next |
| 64 | } |
| 65 | |
| 66 | { |
| 67 | reading_usage = 0 |
| 68 | help = help "\n" $0 |
| 69 | } |
| 70 | |
| 71 | function escape(str) { |
| 72 | gsub(/[`\\$"]/, "\\\\&", str) |
| 73 | return str |
| 74 | } |
| 75 | |
| 76 | function trim(str) { |
| 77 | sub(/^\n*/, "", str) |
| 78 | sub(/\n*$/, "", str) |
| 79 | return str |
| 80 | } |
| 81 | |
| 82 | END { |
| 83 | if (usage || summary) { |
| 84 | print "summary=\"" escape(summary) "\"" |
| 85 | print "usage=\"" escape(trim(usage)) "\"" |
| 86 | print "help=\"" escape(trim(help)) "\"" |
| 87 | } |
| 88 | } |
| 89 | ' |
| 90 | } |
| 91 | |
| 92 | documentation_for() { |
| 93 | local filename |
| 94 | filename="$(command_path "$1")" |
| 95 | if [ -n "$filename" ]; then |
| 96 | extract_initial_comment_block < "$filename" | collect_documentation |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | print_summary() { |
| 101 | local command="$1" |
| 102 | local summary usage help |
| 103 | eval "$(documentation_for "$command")" |
| 104 | |
| 105 | if [ -n "$summary" ]; then |
| 106 | printf " %-9s %s\n" "$command" "$summary" |
| 107 | fi |
| 108 | } |
| 109 | |
| 110 | print_summaries() { |
| 111 | for command; do |
| 112 | print_summary "$command" |
| 113 | done |
| 114 | } |
| 115 | |
| 116 | print_help() { |
| 117 | local command="$1" |
| 118 | local summary usage help |
| 119 | eval "$(documentation_for "$command")" |
| 120 | [ -n "$help" ] || help="$summary" |
| 121 | |
| 122 | if [ -n "$usage" ] || [ -n "$summary" ]; then |
| 123 | if [ -n "$usage" ]; then |
| 124 | echo "$usage" |
| 125 | else |
| 126 | echo "Usage: pyenv ${command}" |
| 127 | fi |
| 128 | if [ -n "$help" ]; then |
| 129 | echo |
| 130 | echo "$help" |
| 131 | echo |
| 132 | fi |
| 133 | else |
| 134 | echo "Sorry, this command isn't documented yet." >&2 |
| 135 | return 1 |
| 136 | fi |
| 137 | } |
| 138 | |
| 139 | print_usage() { |
| 140 | local command="$1" |
| 141 | local summary usage help |
| 142 | eval "$(documentation_for "$command")" |
| 143 | [ -z "$usage" ] || echo "$usage" |
| 144 | } |
| 145 | |
| 146 | unset usage |
| 147 | if [ "$1" = "--usage" ]; then |
| 148 | usage="1" |
| 149 | shift |
| 150 | fi |
| 151 | |
| 152 | if [ -z "$1" ] || [ "$1" == "pyenv" ]; then |
| 153 | echo "Usage: pyenv <command> [<args>]" |
| 154 | [ -z "$usage" ] || exit |
| 155 | echo |
| 156 | echo "Some useful pyenv commands are:" |
| 157 | print_summaries $(exec pyenv-commands | sort -u) |
| 158 | echo |
| 159 | echo "See \`pyenv help <command>' for information on a specific command." |
| 160 | echo "For full documentation, see: https://github.com/pyenv/pyenv#readme" |
| 161 | else |
| 162 | command="$1" |
| 163 | if [ -n "$(command_path "$command")" ]; then |
| 164 | if [ -n "$usage" ]; then |
| 165 | print_usage "$command" |
| 166 | else |
| 167 | print_help "$command" |
| 168 | fi |
| 169 | else |
| 170 | echo "pyenv: no such command \`$command'" >&2 |
| 171 | exit 1 |
| 172 | fi |
| 173 | fi |