-
Notifications
You must be signed in to change notification settings - Fork 1
/
help.sh
151 lines (136 loc) · 4.17 KB
/
help.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/local/bin/bash
# help.sh -- self-contained on-line help for bash scripts
#
# (C) Copyright 2013, Dario Hamidi <[email protected]>
#
# help.sh is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# help.sh is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with help.sh. If not, see <http://www.gnu.org/licenses/>.
#
# To see help.sh in action try the following in the directory of
# help.sh:
#
## summary of all public commands (i.e. not idented)
#
# (source help.sh; help help.sh)
#
## summary of all documented commands
#
# (source help.sh; HELP_BOL=no; help help.sh)
#
## detailed description of a single command
#
# (source help.sh; help help.sh help)
#
# Provide your script with a help function:
#
# function help {
# (source help.sh; help $0 $@)
# }
#@help
#+FILE [COMMAND] -- briefly describe commands extracted from FILE
#
# This function parses special comments in the shell script FILE in
# order to display a summary of all interactive commands FILE supports.
#
# If COMMAND is given and names a existing command in FILE, a longer
# description of just COMMAND is displayed.
#
# The behavior of `help' can be influenced with these environment
# variables:
#
# - HELP_COL (Default: 25)
#
# the column where the command summary should start
#
# - HELP_BOL (Default: "yes")
#
# if set to "yes", then only comments starting in column 0 are taken
# into account when parsing a command's documentation.
function help {
#@indent-to
#+WORD COL -- out spaces to position cursor after WORD in column COL
function indent-to {
local word=$1
declare -i col=$2
for ((i = ${#word}; i < col; i++))
do
echo -n " "
done
}
#@warn
#+MSG -- outputs MSG on standard error, prefixed with the script name
function warn {
local msg=$1
echo "$0: $msg" >&2
}
#@fatal
#+MSG -- warn MSG, then exit with exit code 1
function fatal {
local msg=$1
warn "$msg"
exit 1
}
local file=$1
local command=$2
# command -> long description
declare -A desc=()
# command -> summary
declare -A summary=()
# does FILE exist?
[[ -n "$file" && -e "$file" ]] || fatal "'$file' does not exist"
# parse FILE
local cur # the command currently being parsed
local OLDIFS="$IFS"
local IFS="$OLDIFS"
if [[ "${HELP_BOL:-yes}" == "yes" ]]
then
# preserve whitespace
IFS=''
fi
while read line
do
# a line beginning with `#@name' introduces command `name'
# the first `#' after the slash anchors the pattern at the beginning of the string
if [[ "$line" != "${line/##@}" ]]
then
cur="${line:2}" # strip the leading `#@'
# when parsing a command, `#+' defined the summary
elif [[ -n "$cur" && ( "$line" != "${line/##+}" ) ]]
then
summary["$cur"]="${line:2}" # strip the leading `#+'
# a line beginning with `function' ends the command documentation
elif [[ "$line" != "${line/#function}" ]]
then
cur=""
# inside a command definition we add all lines beginning with `#'
elif [[ -n "$cur" && ( "$line" != "${line/##}" ) ]]
then
description["$cur"]="${description[$cur]}${line##\#}\n"
fi
done <"$file"
IFS="$OLDIFS"
# should we output only one detailed description?
if [[ -n "$command" ]]
then
echo "$command -- ${summary[$command]}"
echo -e "${description[$command]}"
else
# output a short summary for each command
for word in ${!summary[@]}
do
echo -n "$word"
indent-to "$word" ${HELP_COL:-25}
echo "${summary[$word]}"
done
fi
}