|
|
@ -1,99 +1,162 @@ |
|
|
|
#!/usr/bin/env bash |
|
|
|
# |
|
|
|
# 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 |
|
|
|
|
|
|
|
print_set_version() { |
|
|
|
echo "<version> should be a string matching a Python version known by pyenv." |
|
|
|
command_path() { |
|
|
|
local command="$1" |
|
|
|
command -v pyenv-"$command" || command -v pyenv-sh-"$command" || true |
|
|
|
} |
|
|
|
|
|
|
|
extract_initial_comment_block() { |
|
|
|
sed -ne " |
|
|
|
/^#/ !{ |
|
|
|
q |
|
|
|
} |
|
|
|
|
|
|
|
s/^#$/# / |
|
|
|
|
|
|
|
/^# / { |
|
|
|
s/^# // |
|
|
|
p |
|
|
|
} |
|
|
|
" |
|
|
|
} |
|
|
|
|
|
|
|
collect_documentation() { |
|
|
|
awk ' |
|
|
|
/^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) { |
|
|
|
gsub(/^\n*/, "", str) |
|
|
|
gsub(/\n*$/, "", str) |
|
|
|
return str |
|
|
|
} |
|
|
|
|
|
|
|
END { |
|
|
|
if (usage || summary) { |
|
|
|
print "summary=\"" escape(summary) "\"" |
|
|
|
print "usage=\"" escape(trim(usage)) "\"" |
|
|
|
print "help=\"" escape(trim(help)) "\"" |
|
|
|
} |
|
|
|
} |
|
|
|
' |
|
|
|
} |
|
|
|
|
|
|
|
documentation_for() { |
|
|
|
local filename="$(command_path "$1")" |
|
|
|
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 |
|
|
|
} |
|
|
|
|
|
|
|
local versions="$(pyenv-versions --bare)" |
|
|
|
if [ -z "$versions" ]; then |
|
|
|
echo "There are currently no Python versions installed for pyenv." |
|
|
|
print_help() { |
|
|
|
local command="$1" |
|
|
|
local summary usage help |
|
|
|
eval "$(documentation_for "$command")" |
|
|
|
[ -n "$help" ] || help="$summary" |
|
|
|
|
|
|
|
if [ -n "$usage" -o -n "$summary" ]; then |
|
|
|
if [ -n "$usage" ]; then |
|
|
|
echo "$usage" |
|
|
|
else |
|
|
|
echo "Usage: pyenv ${command}" |
|
|
|
fi |
|
|
|
if [ -n "$help" ]; then |
|
|
|
echo |
|
|
|
echo "$help" |
|
|
|
echo |
|
|
|
fi |
|
|
|
else |
|
|
|
echo "The currently installed Python versions are:" |
|
|
|
echo "$versions" | sed 's/^/ /' |
|
|
|
echo "Sorry, this command isn't documented yet." >&2 |
|
|
|
return 1 |
|
|
|
fi |
|
|
|
} |
|
|
|
|
|
|
|
echo |
|
|
|
echo "The special version string 'system' will use your default system Python" |
|
|
|
print_usage() { |
|
|
|
local command="$1" |
|
|
|
local summary usage help |
|
|
|
eval "$(documentation_for "$command")" |
|
|
|
[ -z "$usage" ] || echo "$usage" |
|
|
|
} |
|
|
|
|
|
|
|
case "$1" in |
|
|
|
"") echo "usage: pyenv <command> [<args>] |
|
|
|
|
|
|
|
Some useful pyenv commands are: |
|
|
|
commands List all pyenv commands |
|
|
|
rehash Rehash pyenv shims (run this after installing binaries) |
|
|
|
global Set or show the global Python version |
|
|
|
local Set or show the local directory-specific Python version |
|
|
|
shell Set or show the shell-specific Python version |
|
|
|
version Show the current Python version |
|
|
|
versions List all Python versions known by pyenv |
|
|
|
which Show the full path for the given Python command |
|
|
|
whence List all Python versions with the given command |
|
|
|
|
|
|
|
See 'pyenv help <command>' for information on a specific command. |
|
|
|
For full documentation, see: https://github.com/yyuu/pyenv#readme" |
|
|
|
;; |
|
|
|
commands) echo "usage: pyenv commands |
|
|
|
pyenv commands --sh |
|
|
|
pyenv commands --no-sh |
|
|
|
|
|
|
|
List all pyenv commands." |
|
|
|
;; |
|
|
|
global) echo "usage: pyenv global <version> |
|
|
|
|
|
|
|
Sets the global Python version. You can override the global version at |
|
|
|
any time by setting a directory-specific version with \`pyenv local' |
|
|
|
or by setting the PYENV_VERSION environment variable. |
|
|
|
|
|
|
|
$(print_set_version)" |
|
|
|
;; |
|
|
|
local) echo "usage: pyenv local <version> |
|
|
|
pyenv local --unset |
|
|
|
|
|
|
|
Sets the local directory-specific Python version by writing the version |
|
|
|
name to a file named '.pyenv-version'. |
|
|
|
|
|
|
|
When you run a Python command, pyenv will look for an '.pyenv-version' |
|
|
|
file in the current directory and each parent directory. If no such |
|
|
|
file is found in the tree, pyenv will use the global Python version |
|
|
|
specified with \`pyenv global', or the version specified in the |
|
|
|
PYENV_VERSION environment variable. |
|
|
|
|
|
|
|
$(print_set_version)" |
|
|
|
;; |
|
|
|
shell) echo "usage: pyenv shell <version> |
|
|
|
pyenv shell --unset |
|
|
|
|
|
|
|
Sets a shell-specific Python version by setting the 'PYENV_VERSION' |
|
|
|
environment variable in your shell. This version overrides both |
|
|
|
project-specific versions and the global version. |
|
|
|
|
|
|
|
$(print_set_version)" |
|
|
|
;; |
|
|
|
versions) echo "usage: pyenv versions |
|
|
|
pyenv versions --bare |
|
|
|
|
|
|
|
Lists all Python versions known by pyenv." |
|
|
|
;; |
|
|
|
which) echo "usage: pyenv which <command> |
|
|
|
|
|
|
|
Displays the full path to the binary that pyenv will execute when you |
|
|
|
run the given command." |
|
|
|
;; |
|
|
|
whence) echo "usage: pyenv whence <command> |
|
|
|
|
|
|
|
Lists all Python versions with the given command installed." |
|
|
|
;; |
|
|
|
*) |
|
|
|
command_path="$(command -v "pyenv-$1" || true)" |
|
|
|
if [ -n "$command_path" ]; then |
|
|
|
echo "Sorry, the \`$1' command isn't documented yet." |
|
|
|
echo |
|
|
|
echo "You can view the command's source here:" |
|
|
|
echo "$command_path" |
|
|
|
echo |
|
|
|
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:" |
|
|
|
print_summaries commands local global shell install uninstall rehash version versions which whence |
|
|
|
echo |
|
|
|
echo "See \`pyenv help <command>' for information on a specific command." |
|
|
|
echo "For full documentation, see: https://github.com/yyuu/pyenv#readme" |
|
|
|
else |
|
|
|
command="$1" |
|
|
|
if [ -n "$(command_path "$command")" ]; then |
|
|
|
if [ -n "$usage" ]; then |
|
|
|
print_usage "$command" |
|
|
|
else |
|
|
|
print_help "$command" |
|
|
|
fi |
|
|
|
else |
|
|
|
echo "pyenv: no such command \`$1'" |
|
|
|
echo "pyenv: no such command \`$command'" >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
esac |
|
|
|
fi |