-
Notifications
You must be signed in to change notification settings - Fork 0
/
xopts
127 lines (120 loc) · 3.1 KB
/
xopts
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
#
# ANALYSIS:
# - not possible to set global variables when calling get_opts
# using command substitution, to return result
# - not possible to set positional parameters in a function
# where \${@} refers to is own parameter array
#
# RESULT:
# - use get_opts when only setting global variables
# - use raw when arguments on tail are expected
# ------------------------------------------------
#
# Use when expecting tail arguments, as well as options
#
declare -a in_args=("${@}")
declare -a out_args=()
declare -i i=0
while (( i < ${#in_args[@]} )); do
declare arg="${in_args[$i]}"
case ${arg} in
-h|--help) show_help "${SYNTAX}" ;;
-m|--message)
(( i = i + 1 ))
MESSAGE="${in_args[$i]}"
echo "in_args[$i]: ${in_args[$i]}" >&2
echo "MESSAGE: ${MESSAGE}" >&2
;;
*)
(( ${#out_args[@]} > 0 )) \
&& out_args=("${out_args[@]}" "${arg}") \
|| out_args=("${arg}")
;;
esac
(( i++ ))
done
(( ${#out_args[@]} > 0 )) \
&& set -- "${out_args[@]}"
# ------------------------------------------------
#
# Use when only setting global variables
#
get_opts() {
local -a in_args=("${@}") out_args=()
local -i i=0
while (( i < ${#in_args[@]} )); do
local arg="${in_args[i]}"
case ${arg} in
-h|--help) get_help ;;
-l|--like)
(( $i++ ))
LIKE="${in_args[i]}"
;;
-s|--show)
(( $i++ ))
SHOW="${in_args[i]}"
;;
*) out_args+=("${arg}") ;;
esac
(( i++ ))
done
FILES=("${out_args[@]}")
}
# ------------------------------------------------
# Dead Simple Positional Parameter Parse
# ------------------------------------------------
#
__get_opts() {
while (( $# > 0 )); do
local arg="${1}"; shift;
case ${arg} in
--help) { usage; exit 0; } ;;
--list) LIST=${TRUE} ;;
--less*)
LESS=${TRUE}
[[ ${arg} =~ '=' ]] \
|| error "No script file was provided as argument" 2 \
&& ITEM="${arg#*=}"
;;
--show*)
SHOW=${TRUE}
[[ ${arg} =~ '=' ]] \
|| error "No script file was provided as argument" 3 \
&& ITEM="${arg#*=}"
;;
--term*)
GOT_TERM=${TRUE}
[[ ${arg} =~ '=' ]] \
|| error "No term was provided as argument" 4 \
&& TERM="${arg#*=}"
;;
*) error "Unknown option: ${arg}" 1 ;;
esac
done
return 0
}
get_opts() {
while (( $# > 0 )); do
local arg="${1}"; shift;
case ${arg} in
--help) { usage; exit 0; } ;;
--log-value) LOG_VALUE=${TRUE} ;;
--log) LOG_DATA=${TRUE} ;;
--display) DISPLAY_DATA=${TRUE} ;;
--inform) INFORM=${TRUE} ;;
--quotes*)
SPEAK_DATA=${TRUE}
[[ ${arg} =~ '=' ]] && VOICE="${arg#*=}"
;;
--price) # one and done, get the rest
__get_price "${@}"
exit 0 ;;
--btc) # one and done, get the rest
__btc_value "${@}"
exit 0 ;;
--export) EXPORT_DATA=${TRUE} ;;
*) die "Unknown option: ${arg}" ;;
esac
done
return 0
}