-
Notifications
You must be signed in to change notification settings - Fork 2
/
auto_usage.sh
51 lines (43 loc) · 849 Bytes
/
auto_usage.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
#
# usage helpers
#
#
# show "usage" text as parsed from shell script source
# lines beginning with "## " are taken from output
#
# note, the original "top-level" source script is sourced
# BASH_SOURCE is needed for this
bashfoo_require log
auto_usage()
{
local idx=$((${#BASH_SOURCE[*]} -1))
local source="${BASH_SOURCE[$(($idx))]}"
# tbd rewrite as one-shot awk
cat $source | egrep '^##( |$)' | cut -c4- >&2
}
# exit with message and
# usage text
fail_on_bad_usage()
{
log_error "$*"
auto_usage
exit 1
}
# if "$1" is generic help option
# --help
# -h
# then invoke auto_usage and exit
#
# usage example
# while [ -n "$1" ] ; do
# maybe_show_auto_help
# (...)
# shift
# done
maybe_show_auto_help()
{
if [ "$1" = "--help" -o "$1" = "-h" ] ; then
auto_usage
exit 0
fi
}