2019-04-23 10:23:33 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
2013-01-18 03:41:41 -05:00
|
|
|
# Summary: Display help for a command
|
|
|
|
#
|
|
|
|
# Usage: pyenv help [--usage] COMMAND
|
|
|
|
#
|
|
|
|
# Parses and displays help contents from a command's source file.
|
|
|
|
#
|
|
|
|
# A command is considered documented if it starts with a comment block
|
|
|
|
# that has a `Summary:' or `Usage:' section. Usage instructions can
|
|
|
|
# span multiple lines as long as subsequent lines are indented.
|
|
|
|
# The remainder of the comment block is displayed as extended
|
|
|
|
# documentation.
|
|
|
|
|
2012-08-31 02:23:41 -04:00
|
|
|
set -e
|
|
|
|
[ -n "$PYENV_DEBUG" ] && set -x
|
|
|
|
|
2015-11-20 23:13:52 -05:00
|
|
|
# Provide pyenv completions
|
2015-11-13 15:06:29 -05:00
|
|
|
if [ "$1" = "--complete" ]; then
|
|
|
|
echo --usage
|
2015-11-20 23:13:52 -05:00
|
|
|
exec pyenv-commands
|
2015-11-13 15:06:29 -05:00
|
|
|
fi
|
|
|
|
|
2013-01-18 03:41:41 -05:00
|
|
|
command_path() {
|
|
|
|
local command="$1"
|
2013-09-30 05:02:12 -04:00
|
|
|
command -v pyenv-"$command" || command -v pyenv-sh-"$command" || true
|
2013-01-18 03:41:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extract_initial_comment_block() {
|
2021-02-17 11:12:45 -05:00
|
|
|
LC_ALL= \
|
|
|
|
LC_CTYPE=C \
|
2013-01-18 03:41:41 -05:00
|
|
|
sed -ne "
|
|
|
|
/^#/ !{
|
|
|
|
q
|
|
|
|
}
|
|
|
|
|
|
|
|
s/^#$/# /
|
|
|
|
|
|
|
|
/^# / {
|
|
|
|
s/^# //
|
|
|
|
p
|
|
|
|
}
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
|
|
|
collect_documentation() {
|
2022-05-08 02:46:47 -04:00
|
|
|
# `tail` prevents "broken pipe" errors due to `head` closing the pipe without reading everything
|
2021-09-20 22:05:29 -04:00
|
|
|
# https://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error/642932#642932
|
2023-02-22 12:11:48 -05:00
|
|
|
$(type -P gawk awk | tail -n +1 | head -n1) '
|
2013-01-18 03:41:41 -05:00
|
|
|
/^Summary:/ {
|
|
|
|
summary = substr($0, 10)
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
|
|
|
/^Usage:/ {
|
|
|
|
reading_usage = 1
|
|
|
|
usage = usage "\n" $0
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
|
|
|
/^( *$| )/ && reading_usage {
|
|
|
|
usage = usage "\n" $0
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
reading_usage = 0
|
|
|
|
help = help "\n" $0
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape(str) {
|
|
|
|
gsub(/[`\\$"]/, "\\\\&", str)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
function trim(str) {
|
2013-09-30 05:02:12 -04:00
|
|
|
sub(/^\n*/, "", str)
|
|
|
|
sub(/\n*$/, "", str)
|
2013-01-18 03:41:41 -05:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
END {
|
|
|
|
if (usage || summary) {
|
|
|
|
print "summary=\"" escape(summary) "\""
|
|
|
|
print "usage=\"" escape(trim(usage)) "\""
|
|
|
|
print "help=\"" escape(trim(help)) "\""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
|
|
|
documentation_for() {
|
2017-06-23 18:12:09 -04:00
|
|
|
local filename
|
|
|
|
filename="$(command_path "$1")"
|
2013-01-18 03:41:41 -05:00
|
|
|
if [ -n "$filename" ]; then
|
|
|
|
extract_initial_comment_block < "$filename" | collect_documentation
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
print_summary() {
|
|
|
|
local command="$1"
|
|
|
|
local summary usage help
|
|
|
|
eval "$(documentation_for "$command")"
|
|
|
|
|
|
|
|
if [ -n "$summary" ]; then
|
|
|
|
printf " %-9s %s\n" "$command" "$summary"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
print_summaries() {
|
|
|
|
for command; do
|
|
|
|
print_summary "$command"
|
|
|
|
done
|
|
|
|
}
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2013-01-18 03:41:41 -05:00
|
|
|
print_help() {
|
|
|
|
local command="$1"
|
|
|
|
local summary usage help
|
|
|
|
eval "$(documentation_for "$command")"
|
|
|
|
[ -n "$help" ] || help="$summary"
|
|
|
|
|
2017-06-23 18:12:09 -04:00
|
|
|
if [ -n "$usage" ] || [ -n "$summary" ]; then
|
2013-01-18 03:41:41 -05:00
|
|
|
if [ -n "$usage" ]; then
|
|
|
|
echo "$usage"
|
|
|
|
else
|
|
|
|
echo "Usage: pyenv ${command}"
|
|
|
|
fi
|
|
|
|
if [ -n "$help" ]; then
|
|
|
|
echo
|
|
|
|
echo "$help"
|
|
|
|
echo
|
|
|
|
fi
|
2012-08-31 02:23:41 -04:00
|
|
|
else
|
2013-01-18 03:41:41 -05:00
|
|
|
echo "Sorry, this command isn't documented yet." >&2
|
|
|
|
return 1
|
2012-08-31 02:23:41 -04:00
|
|
|
fi
|
2013-01-18 03:41:41 -05:00
|
|
|
}
|
2012-08-31 02:23:41 -04:00
|
|
|
|
2013-01-18 03:41:41 -05:00
|
|
|
print_usage() {
|
|
|
|
local command="$1"
|
|
|
|
local summary usage help
|
|
|
|
eval "$(documentation_for "$command")"
|
|
|
|
[ -z "$usage" ] || echo "$usage"
|
2012-08-31 02:23:41 -04:00
|
|
|
}
|
|
|
|
|
2013-01-18 03:41:41 -05:00
|
|
|
unset usage
|
|
|
|
if [ "$1" = "--usage" ]; then
|
|
|
|
usage="1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$1" ] || [ "$1" == "pyenv" ]; then
|
|
|
|
echo "Usage: pyenv <command> [<args>]"
|
|
|
|
[ -z "$usage" ] || exit
|
|
|
|
echo
|
|
|
|
echo "Some useful pyenv commands are:"
|
2020-01-22 18:12:29 -05:00
|
|
|
print_summaries $(exec pyenv-commands | sort -u)
|
2013-01-18 03:41:41 -05:00
|
|
|
echo
|
|
|
|
echo "See \`pyenv help <command>' for information on a specific command."
|
2017-03-05 23:31:25 -05:00
|
|
|
echo "For full documentation, see: https://github.com/pyenv/pyenv#readme"
|
2013-01-18 03:41:41 -05:00
|
|
|
else
|
|
|
|
command="$1"
|
|
|
|
if [ -n "$(command_path "$command")" ]; then
|
|
|
|
if [ -n "$usage" ]; then
|
|
|
|
print_usage "$command"
|
|
|
|
else
|
|
|
|
print_help "$command"
|
|
|
|
fi
|
2012-08-31 02:23:41 -04:00
|
|
|
else
|
2013-01-18 03:41:41 -05:00
|
|
|
echo "pyenv: no such command \`$command'" >&2
|
|
|
|
exit 1
|
2012-08-31 02:23:41 -04:00
|
|
|
fi
|
2013-01-18 03:41:41 -05:00
|
|
|
fi
|