-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_with_options.bash
More file actions
executable file
·101 lines (87 loc) · 1.78 KB
/
template_with_options.bash
File metadata and controls
executable file
·101 lines (87 loc) · 1.78 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
#!/usr/bin/env bash
#
# A minimal Bash template.
set -o errtrace
set -o errexit
set -o nounset
set -o pipefail
# Show line numbers and function names when debugging
export PS4='+ ${LINENO}:${FUNCNAME[0]:-}() '
readonly ARGS=("$@")
readonly ARG_COUNT="$#"
readonly VERSION="0.0.1"
bool=1
positional_arg1=""
positional_arg2=""
usage() { printf "%s" "\
usage:
bash_template.bash [--help] [--version]
description:
This is a template meant for Bash scripts.
options:
-h, --help display this help message and exit
-v, --version display the version number and exit
"
exit 1
}
# Display the version number and exit.
version_info() {
echo "$VERSION"
exit 1
}
# Display the provided error message before exiting with the provided status
# (default: 1).
error() {
local message="$1"
local status="${2-1}" # default exit status: 1
echo "$message"
exit "$status"
}
# Returns 0 if the provided variable is an empty string ("").
is_null() {
local variable="$1"
[[ -z "$variable" ]]
}
# Parse user-provided arguments
parse_params() {
(( "$ARG_COUNT" < 2 )) &&
usage
for arg in "${ARGS[@]}"
do
case "$arg" in
-h | --help)
usage
;;
-v | --version)
version_info
;;
--bool)
bool=0
;;
--) # end of all options
break
;;
-*) # unknown option
echo "unknown option: $arg"
;;
*) # end of options
if is_null "$positional_arg1"
then
positional_arg1="$arg"
elif is_null "$positional_arg2"
then
positional_arg2="$arg"
else
echo "unknown positional argument: $arg"
fi
;;
esac
done
}
main() {
parse_params
echo "$bool"
echo "$positional_arg1"
echo "$positional_arg2"
}
main