| 1 | #!/usr/bin/envscip-shell filesystem . . `/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 | [scip-shell system . . `[`. -n "$PYENV_DEBUG" ] && set -x |
| 17 | |
| 18 | # Provide pyenv completions |
| 19 | if [scip-shell system . . `[`. "$1" = "--complete" ]; then |
| 20 | echoscip-shell system . . echo. --usage |
| 21 | exec pyenv-commands |
| 22 | fi |
| 23 | |
| 24 | command_pathscip-shell shell shell-project 0.0.0 command_path().() { |
| 25 | local command="$1" |
| 26 | command -v pyenv-"$command" || command -v pyenv-sh-"$command" || truescip-shell system . . true. |
| 27 | } |
| 28 | |
| 29 | extract_initial_comment_blockscip-shell shell shell-project 0.0.0 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_documentationscip-shell shell shell-project 0.0.0 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 | tailscip-shell system . . tail. -n +1 | headscip-shell system . . 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_forscip-shell shell shell-project 0.0.0 documentation_for().() { |
| 93 | local filename |
| 94 | filename="$(command_pathscip-shell shell shell-project 0.0.0 command_path(). "$1")" |
| 95 | if [scip-shell system . . `[`. -n "$filename" ]; then |
| 96 | extract_initial_comment_blockscip-shell shell shell-project 0.0.0 extract_initial_comment_block(). < "$filename" | collect_documentationscip-shell shell shell-project 0.0.0 collect_documentation(). |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | print_summaryscip-shell shell shell-project 0.0.0 print_summary().() { |
| 101 | local command="$1" |
| 102 | local summary usage help |
| 103 | eval "$(documentation_forscip-shell shell shell-project 0.0.0 documentation_for(). "$command")" |
| 104 | |
| 105 | if [scip-shell system . . `[`. -n "$summary" ]; then |
| 106 | printfscip-shell system . . printf. " %-9s %s\n" "$command" "$summary" |
| 107 | fi |
| 108 | } |
| 109 | |
| 110 | print_summariesscip-shell shell shell-project 0.0.0 print_summaries().() { |
| 111 | for command; do |
| 112 | print_summaryscip-shell shell shell-project 0.0.0 print_summary(). "$command" |
| 113 | done |
| 114 | } |
| 115 | |
| 116 | print_helpscip-shell shell shell-project 0.0.0 print_help().() { |
| 117 | local command="$1" |
| 118 | local summary usage help |
| 119 | eval "$(documentation_forscip-shell shell shell-project 0.0.0 documentation_for(). "$command")" |
| 120 | [scip-shell system . . `[`. -n "$help" ] || help="$summary" |
| 121 | |
| 122 | if [scip-shell system . . `[`. -n "$usage" ] || [scip-shell system . . `[`. -n "$summary" ]; then |
| 123 | if [scip-shell system . . `[`. -n "$usage" ]; then |
| 124 | echoscip-shell system . . echo. "$usage" |
| 125 | else |
| 126 | echoscip-shell system . . echo. "Usage: pyenv ${command}" |
| 127 | fi |
| 128 | if [scip-shell system . . `[`. -n "$help" ]; then |
| 129 | echoscip-shell system . . echo. |
| 130 | echoscip-shell system . . echo. "$help" |
| 131 | echoscip-shell system . . echo. |
| 132 | fi |
| 133 | else |
| 134 | echoscip-shell system . . echo. "Sorry, this command isn't documented yet." >&2 |
| 135 | return 1 |
| 136 | fi |
| 137 | } |
| 138 | |
| 139 | print_usagescip-shell shell shell-project 0.0.0 print_usage().() { |
| 140 | local command="$1" |
| 141 | local summary usage help |
| 142 | eval "$(documentation_forscip-shell shell shell-project 0.0.0 documentation_for(). "$command")" |
| 143 | [scip-shell system . . `[`. -z "$usage" ] || echoscip-shell system . . echo. "$usage" |
| 144 | } |
| 145 | |
| 146 | unset usage |
| 147 | if [scip-shell system . . `[`. "$1" = "--usage" ]; then |
| 148 | usage="1" |
| 149 | shift |
| 150 | fi |
| 151 | |
| 152 | if [scip-shell system . . `[`. -z "$1" ] || [scip-shell system . . `[`. "$1" == "pyenv" ]; then |
| 153 | echoscip-shell system . . echo. "Usage: pyenv <command> [<args>]" |
| 154 | [scip-shell system . . `[`. -z "$usage" ] || exit |
| 155 | echoscip-shell system . . echo. |
| 156 | echoscip-shell system . . echo. "Some useful pyenv commands are:" |
| 157 | print_summariesscip-shell shell shell-project 0.0.0 print_summaries(). $(exec pyenv-commands | sortscip-shell system . . sort. -u) |
| 158 | echoscip-shell system . . echo. |
| 159 | echoscip-shell system . . echo. "See \`pyenv help <command>' for information on a specific command." |
| 160 | echoscip-shell system . . echo. "For full documentation, see: https://github.com/pyenv/pyenv#readme" |
| 161 | else |
| 162 | command="$1" |
| 163 | if [scip-shell system . . `[`. -n "$(command_pathscip-shell shell shell-project 0.0.0 command_path(). "$command")" ]; then |
| 164 | if [scip-shell system . . `[`. -n "$usage" ]; then |
| 165 | print_usagescip-shell shell shell-project 0.0.0 print_usage(). "$command" |
| 166 | else |
| 167 | print_helpscip-shell shell shell-project 0.0.0 print_help(). "$command" |
| 168 | fi |
| 169 | else |
| 170 | echoscip-shell system . . echo. "pyenv: no such command \`$command'" >&2 |
| 171 | exit 1 |
| 172 | fi |
| 173 | fi |