-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.bash
More file actions
executable file
·145 lines (135 loc) · 3.87 KB
/
Copy pathtemplate.bash
File metadata and controls
executable file
·145 lines (135 loc) · 3.87 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/env bash
#
# TODO: name.sh: brief explanation
#
# TODO: details (e.g., a paragraph or two)
#
# Note:
# - See bash-cheatsheet.md for commonly used bash snippets.
# - This was inspired by TODO.
#
# TODO:
# - Review Bash "Pro Tips" in bash-cheatsheet.md.
# - Customize this script by addressing TODO's (and then remove them).
# - Mention stuff to be addressed ... TODO.
#
# Set bash regular and/or verbose tracing
# - xtrace shows arg expansion (and often is sufficient)
# - verbose shows source commands as is (but usually is superfluous w/ xtrace)
#
if [ "${DEBUG_LEVEL:-0}" -ge 4 ]; then
echo "$0 $*"
fi
if [[ "${TRACE:-0}" == "1" ]]; then
set -o xtrace
fi
if [[ "${VERBOSE:-0}" == "1" ]]; then
set -o verbose
fi
if [[ "${STRICT:-0}" == "1" ]]; then
set -euo pipefail
fi
# Display command-line usage
function usage() {
local script
script=$(basename "$0")
echo ""
## TODO: update options list to match actual options
echo "Usage: $script [--TODO-option] [--verbose] [--trace] [--help] [-- | -]"
echo ""
echo "Examples:"
echo ""
## TODO: example 1
echo "$script example-arg-1"
echo ""
## TODO: example 2
echo "$script --TODO-option example-arg-2"
echo ""
echo "Notes:"
echo "- The -- option uses defaults and avoids the usage statement."
echo "- Use DEBUG_SCRIPT=1 to show getopt processing."
## TODO: add more notes
echo ""
}
# Initialize options
# TODO: add/remove variables to match actual options below
show_usage=0
trace=0
verbose=0
## TODO: add script-specific option variables (examples):
## fubar=0
## level=0
DEBUG_SCRIPT=$([ "${DEBUG_SCRIPT:-0}" -eq 1 ] && echo true || echo false)
orig_argc=$#
# Parse options with getopt
# Note:
# - getopt unravels combined short options (e.g., "-tv" -> "-t -v")
# - options taking a value argument have a colon appended (e.g., "level:").
# - see examples/chatgpt-get-long-options-parsing.bash for more background.
# TODO: update -o and --long specs to match your options
TEMP=$(getopt -o htv --long help,trace,verbose -n "$0" -- "$@")
status=$?
if [ $status != 0 ]; then
echo "Error: getopt failed (status=$status); terminating." >&2
exit 1
fi
$DEBUG_SCRIPT && echo "TEMP=$TEMP"
# Reassign $1, $2, etc. to the normalized getopt output.
# Note: quotes around "$TEMP" are essential.
eval set -- "$TEMP"
# Process each option
while true; do
$DEBUG_SCRIPT && echo "\$1=$1"
case "$1" in
-h|--help)
show_usage=1
shift
;;
## TODO: add script-specific options (see commented examples below)
## -f|--fubar)
## fubar=1
## shift
## ;;
## -l|--level)
## level="$2"
## shift 2
## ;;
-t|--trace)
trace=1
shift
;;
-v|--verbose)
verbose=1
shift
;;
--)
shift
break
;;
*)
echo "Internal error: unexpected option: $1" >&2
exit 1
;;
esac
done
# Apply trace and verbose now that they've been parsed
# note: This is the preferred way to trace the script proper
if [ "$trace" == "1" ]; then
set -o xtrace
fi
if [ "$verbose" == "1" ]; then
set -o verbose
fi
# Show usage if --help or if no arguments were given at all (orig_argc=0).
# Note: passing -- explicitly skips the no-arg check (orig_argc > 0).
# TODO: replace orig_argc check with [ "$#" -eq 0 ] if positional args are required
if [ "$show_usage" == "1" ] || [ "$orig_argc" -eq 0 ]; then
## TODO: remove the following line (only relevant when running template.bash directly)
if [[ "$(basename "$0")" == "template.bash" ]]; then echo "Warning: not intended for standalone usage"; fi
usage
exit
fi
# TODO: assign positional arguments (delete if not planned)
## arg1="$1"
## arg2="${2:-}"
# TODO: do whatever