pyenv/libexec/pyenv-help

171 lines
3.3 KiB
Text
Raw Normal View History

#!/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.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
# Provide pyenv completions
2015-11-13 15:06:29 -05:00
if [ "$1" = "--complete" ]; then
echo --usage
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"
command -v pyenv-"$command" || command -v pyenv-sh-"$command" || true
2013-01-18 03:41:41 -05:00
}
extract_initial_comment_block() {
sed -ne "
/^#/ !{
q
}
s/^#$/# /
/^# / {
s/^# //
p
}
"
}
collect_documentation() {
2017-06-23 18:12:09 -04:00
# shellcheck disable=SC2016
$(type -p gawk awk | head -1) '
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) {
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
}
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
else
2013-01-18 03:41:41 -05:00
echo "Sorry, this command isn't documented yet." >&2
return 1
fi
2013-01-18 03:41:41 -05: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"
}
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:"
change help message from hard coding to dynamic (#1421) ## before ```Usage: pyenv <command> [<args>] Some useful pyenv commands are: commands List all available pyenv commands local Set or show the local application-specific Python version global Set or show the global Python version shell Set or show the shell-specific Python version install Install a Python version using python-build uninstall Uninstall a specific Python version rehash Rehash pyenv shims (run this after installing executables) version Show the current Python version and its origin versions List all Python versions available to pyenv which Display the full path to an executable whence List all Python versions that contain the given executable See `pyenv help <command>' for information on a specific command. For full documentation, see: https://github.com/pyenv/pyenv#readme ``` ## after ``` Usage: pyenv <command> [<args>] Some useful pyenv commands are: commands List all available pyenv commands local Set or show the local application-specific Python version --version Display the version of pyenv commands List all available pyenv commands exec Run an executable with the selected Python version global Set or show the global Python version help Display help for a command hooks List hook scripts for a given pyenv command init Configure the shell environment for pyenv install Install a Python version using python-build local Set or show the local application-specific Python version prefix Display prefix for a Python version rehash Rehash pyenv shims (run this after installing executables) root Display the root directory where versions and shims are kept shell Set or show the shell-specific Python version shims List existing pyenv shims uninstall Uninstall a specific Python version update update pyenv and plugins version Show the current Python version and its origin version-file Detect the file that sets the current pyenv version version-name Show the current Python version version-origin Explain how the current Python version is set versions List all Python versions available to pyenv whence List all Python versions that contain the given executable which Display the full path to an executable See `pyenv help <command>' for information on a specific command. For full documentation, see: https://github.com/pyenv/pyenv#readme ```
2019-10-16 11:47:11 -04:00
print_summaries commands $(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."
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
else
2013-01-18 03:41:41 -05:00
echo "pyenv: no such command \`$command'" >&2
exit 1
fi
2013-01-18 03:41:41 -05:00
fi