-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.sh
More file actions
122 lines (117 loc) · 3.77 KB
/
usage.sh
File metadata and controls
122 lines (117 loc) · 3.77 KB
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
#===============================================================================
# NAME
# usage.sh - Function to print documentation like this one
#
# SYNOPSIS
# . usage.sh
# _help [EXIT_CODE]
# _usage [EXIT_CODE]
# _version
#
# DESCRIPTION
# Prints comments of the form "# This is a comment" at the start of the
# sourcing file to standard error, trimming the two first characters.
# Stops printing when it encounters either a line starting with two
# hashes (see below) or a line without a comment.
#
# Skips the shebang line if there is one.
#
# Returns from the sourcing file with the given exit code (default 0).
#
# BUGS
# https://github.com/dracorp/shell-includes/issues
#
# COPYRIGHT AND LICENSE
# Copyright (C) 2010-2013 Victor Engmark
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
#===============================================================================
_help() {
local header line retval
local pre_header=''
local lib_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
if [[ ! -r "$0" ]]; then
printf '%s\n' "Wrong usage of library ${lib_path}! Use it in a shell script." >&2
return 1
fi
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
'#!'*) # Shebang line
;;
'#='*) # comment from header
;;
''|'##'*|'#='*|[!#]*) # End of comments
retval=$1
if [[ ! $retval =~ ^-?[0-9] ]]; then
retval=0
fi
exit $retval
;;
*) # Comment line
line=${line:2} # Remove comment prefix
if [[ "$1" = usage ]]; then
# print only usage
if [[ "${line}" =~ ^[A-Z\s]+$ ]]; then
header=${line}
fi
if [[ "$header" = SYNOPSIS ]]; then
if [[ "$line" = SYNOPSIS ]]; then
printf '%s' 'Usage:' >&2
else
printf '%s\n' "${line}" >&2
retval=$2
if [[ ! $retval =~ ^-?[0-9] ]]; then
retval=0
fi
exit $retval
fi
else
pre_header=$header
fi
else
printf '%s\n' "${line}" >&2
fi
;;
esac
done < "$0"
}
_usage() {
_help usage $1
}
_version() {
if [[ -z "$PROGRAM_VERSION" ]]; then
local PROGRAM_VERSION=undef
fi
if [[ -n "$PROGRAM_NAME" ]]; then
printf '%s\n' "$PROGRAM_NAME version $PROGRAM_VERSION"
if [[ ! -r "$0" ]]; then
return 0
else
exit 0
fi
else
if [[ ! -r "$0" ]]; then
return 1
else
exit 1
fi
fi
}
usage() {
_usage "$@"
}
version() {
_version "$@"
}