pyenv/libexec/rbenv-help
Leo Cassarani 1d687ac734 Fix incorrect formatting of rbenv-help output under MAWK
In systems that use the MAWK interpreter (the default AWK installed with
Ubuntu), the output of `rbenv help <command>` would have no line breaks.
The issue is fixed by changing `gsub` to `sub` in the snippet of awk
commands that are used to extract documentation comments.

I suspect the bug is something to do with the way the '^' and '$'
characters are interpreted by different AWK interpreters (per-line vs
per-string anchors).

If I understand correctly, the purpose of trim() is to remove all line
breaks from the start and end of each sections of a command's
documentation, in which case `sub` should serve the same purpose.
2013-01-05 16:55:19 +00:00

162 lines
3.2 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Summary: Display help for a command
#
# Usage: rbenv 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 "$RBENV_DEBUG" ] && set -x
command_path() {
local command="$1"
command -v rbenv-"$command" || command -v rbenv-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) {
sub(/^\n*/, "", str)
sub(/\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
}
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: rbenv ${command}"
fi
if [ -n "$help" ]; then
echo
echo "$help"
echo
fi
else
echo "Sorry, this command isn't documented yet." >&2
return 1
fi
}
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
echo "Usage: rbenv <command> [<args>]"
[ -z "$usage" ] || exit
echo
echo "Some useful rbenv commands are:"
print_summaries commands local global shell install uninstall rehash version versions which whence
echo
echo "See \`rbenv help <command>' for information on a specific command."
echo "For full documentation, see: https://github.com/sstephenson/rbenv#readme"
else
command="$1"
if [ -n "$(command_path "$command")" ]; then
if [ -n "$usage" ]; then
print_usage "$command"
else
print_help "$command"
fi
else
echo "rbenv: no such command \`$command'" >&2
exit 1
fi
fi