-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplate.sh
More file actions
executable file
·77 lines (63 loc) · 1.06 KB
/
template.sh
File metadata and controls
executable file
·77 lines (63 loc) · 1.06 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
#!/usr/bin/env bash
help() {
echo "Usage: $(basename $0) [OPTIONS]
$0 is a template for future shell scripts.
Example: $0 -h
Options:
-h, --help Outputs the help message for this command.
"
}
parse_flag() {
local SHIFT=0
case $1 in
h|help)
help
exit 0
;;
f|file)
if [[ -n "$2" ]]; then
FILE="$2"
SHIFT=1
else
echo 'File not specified'
exit 1
fi
;;
*)
echo "Unrecognized argument: $1"
;;
esac
return $SHIFT
}
POSITIONAL_ARGS=()
if [[ $# -eq 0 ]]; then
help
exit 0
fi
while [[ $# -gt 0 ]]; do
if [[ $1 == --* ]]; then
if ! parse_flag "${1#--}" "$2"; then
shift
fi
shift
elif [[ $1 == -* ]]; then
OPTS="${1#-}"
SHIFT=0
if [[ $2 == -* ]]; then
VAL=""
else
VAL="$2"
fi
for (( i=0; i<${#OPTS}; i++ )); do
parse_flag "${OPTS:$i:1}" "$VAL"
(( SHIFT+=$? ))
done
if [[ $SHIFT -ne 0 ]]; then
shift
fi
shift
else
POSITIONAL_ARGS+=("$1")
shift
fi
done