2011-08-12 05:33:45 -04:00
|
|
|
#!/usr/bin/env bash
|
2012-12-29 17:34:53 -05:00
|
|
|
#
|
|
|
|
# Summary: Display help for a command
|
|
|
|
#
|
|
|
|
# Usage: rbenv help [--usage] COMMAND
|
|
|
|
#
|
|
|
|
# Parses and displays help contents from a command's source file.
|
|
|
|
#
|
2012-12-29 22:50:38 -05:00
|
|
|
# A command is considered documented if it starts with a comment block
|
2012-12-29 23:05:04 -05:00
|
|
|
# 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-12-29 17:34:53 -05:00
|
|
|
|
2011-08-12 05:33:45 -04:00
|
|
|
set -e
|
2011-09-12 11:11:59 -04:00
|
|
|
[ -n "$RBENV_DEBUG" ] && set -x
|
2011-08-10 09:53:24 -04:00
|
|
|
|
2015-11-13 15:06:29 -05:00
|
|
|
# Provide rbenv completions
|
|
|
|
if [ "$1" = "--complete" ]; then
|
|
|
|
echo --usage
|
2015-11-20 09:15:06 -05:00
|
|
|
exec rbenv-commands
|
2015-11-13 15:06:29 -05:00
|
|
|
fi
|
|
|
|
|
2012-12-29 13:06:20 -05:00
|
|
|
command_path() {
|
|
|
|
local command="$1"
|
|
|
|
command -v rbenv-"$command" || command -v rbenv-sh-"$command" || true
|
2012-12-12 23:48:28 -05:00
|
|
|
}
|
|
|
|
|
2012-12-29 13:06:20 -05:00
|
|
|
extract_initial_comment_block() {
|
|
|
|
sed -ne "
|
|
|
|
/^#/ !{
|
|
|
|
q
|
|
|
|
}
|
|
|
|
|
|
|
|
s/^#$/# /
|
|
|
|
|
|
|
|
/^# / {
|
|
|
|
s/^# //
|
|
|
|
p
|
|
|
|
}
|
|
|
|
"
|
2012-12-12 23:48:28 -05:00
|
|
|
}
|
|
|
|
|
2012-12-29 13:06:20 -05:00
|
|
|
collect_documentation() {
|
2017-06-23 18:12:09 -04:00
|
|
|
# shellcheck disable=SC2016
|
2014-04-17 20:39:29 -04:00
|
|
|
$(type -p gawk awk | head -1) '
|
2012-12-29 13:06:20 -05:00
|
|
|
/^Summary:/ {
|
|
|
|
summary = substr($0, 10)
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
|
|
|
/^Usage:/ {
|
|
|
|
reading_usage = 1
|
|
|
|
usage = usage "\n" $0
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
|
|
|
/^( *$| )/ && reading_usage {
|
|
|
|
usage = usage "\n" $0
|
|
|
|
next
|
|
|
|
}
|
|
|
|
|
2012-12-29 16:48:28 -05:00
|
|
|
{
|
2012-12-29 13:06:20 -05:00
|
|
|
reading_usage = 0
|
|
|
|
help = help "\n" $0
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape(str) {
|
|
|
|
gsub(/[`\\$"]/, "\\\\&", str)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
function trim(str) {
|
2013-01-05 11:52:57 -05:00
|
|
|
sub(/^\n*/, "", str)
|
|
|
|
sub(/\n*$/, "", str)
|
2012-12-29 13:06:20 -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")"
|
2012-12-29 13:06:20 -05:00
|
|
|
if [ -n "$filename" ]; then
|
|
|
|
extract_initial_comment_block < "$filename" | collect_documentation
|
2012-12-12 23:48:28 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2012-12-29 13:06:20 -05:00
|
|
|
print_summary() {
|
|
|
|
local command="$1"
|
|
|
|
local summary usage help
|
|
|
|
eval "$(documentation_for "$command")"
|
2012-12-12 23:48:28 -05:00
|
|
|
|
2012-12-29 13:06:20 -05:00
|
|
|
if [ -n "$summary" ]; then
|
|
|
|
printf " %-9s %s\n" "$command" "$summary"
|
2012-12-12 23:48:28 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
print_summaries() {
|
2012-12-29 13:06:20 -05:00
|
|
|
for command; do
|
|
|
|
print_summary "$command"
|
2012-12-12 23:48:28 -05:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-12-29 13:06:20 -05:00
|
|
|
print_help() {
|
|
|
|
local command="$1"
|
|
|
|
local summary usage help
|
|
|
|
eval "$(documentation_for "$command")"
|
|
|
|
[ -n "$help" ] || help="$summary"
|
2011-08-10 10:23:43 -04:00
|
|
|
|
2017-06-23 18:12:09 -04:00
|
|
|
if [ -n "$usage" ] || [ -n "$summary" ]; then
|
2012-12-29 16:03:04 -05:00
|
|
|
if [ -n "$usage" ]; then
|
|
|
|
echo "$usage"
|
|
|
|
else
|
|
|
|
echo "Usage: rbenv ${command}"
|
|
|
|
fi
|
2012-12-29 13:06:20 -05:00
|
|
|
if [ -n "$help" ]; then
|
|
|
|
echo
|
|
|
|
echo "$help"
|
|
|
|
echo
|
|
|
|
fi
|
2011-08-10 10:23:43 -04:00
|
|
|
else
|
2012-12-29 13:06:20 -05:00
|
|
|
echo "Sorry, this command isn't documented yet." >&2
|
|
|
|
return 1
|
2011-08-10 10:23:43 -04:00
|
|
|
fi
|
2011-08-10 09:53:24 -04:00
|
|
|
}
|
|
|
|
|
2012-12-29 13:12:47 -05:00
|
|
|
print_usage() {
|
|
|
|
local command="$1"
|
|
|
|
local summary usage help
|
|
|
|
eval "$(documentation_for "$command")"
|
|
|
|
[ -z "$usage" ] || echo "$usage"
|
|
|
|
}
|
|
|
|
|
|
|
|
unset usage
|
|
|
|
if [ "$1" = "--usage" ]; then
|
|
|
|
usage="1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$1" ] || [ "$1" == "rbenv" ]; then
|
2012-12-29 13:06:20 -05:00
|
|
|
echo "Usage: rbenv <command> [<args>]"
|
2012-12-29 13:12:47 -05:00
|
|
|
[ -z "$usage" ] || exit
|
2012-12-29 13:06:20 -05:00
|
|
|
echo
|
|
|
|
echo "Some useful rbenv commands are:"
|
2013-01-03 11:39:48 -05:00
|
|
|
print_summaries commands local global shell install uninstall rehash version versions which whence
|
2012-12-29 13:06:20 -05:00
|
|
|
echo
|
|
|
|
echo "See \`rbenv help <command>' for information on a specific command."
|
2015-12-07 14:03:07 -05:00
|
|
|
echo "For full documentation, see: https://github.com/rbenv/rbenv#readme"
|
2012-12-29 13:06:20 -05:00
|
|
|
else
|
|
|
|
command="$1"
|
|
|
|
if [ -n "$(command_path "$command")" ]; then
|
2012-12-29 13:12:47 -05:00
|
|
|
if [ -n "$usage" ]; then
|
|
|
|
print_usage "$command"
|
|
|
|
else
|
|
|
|
print_help "$command"
|
|
|
|
fi
|
2011-09-28 11:59:02 -04:00
|
|
|
else
|
2012-12-29 13:06:20 -05:00
|
|
|
echo "rbenv: no such command \`$command'" >&2
|
2012-12-12 23:48:28 -05:00
|
|
|
exit 1
|
2011-09-28 11:59:02 -04:00
|
|
|
fi
|
2012-12-29 13:06:20 -05:00
|
|
|
fi
|