Skip to content

Commit 43d6a61

Browse files
committed
Don't rely on getopts and add version flag (-v)
Getopts does not allow flags after the filter expression which breaks existing examples. The `getopt` command does but introduces an external dependency. This commit removes getopt but retains the features it has.
1 parent d601a96 commit 43d6a61

File tree

1 file changed

+71
-54
lines changed

1 file changed

+71
-54
lines changed

JSONPath.sh

Lines changed: 71 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# GLOBALS
55
# ---------------------------------------------------------------------------
66

7+
VERSION="0.0.21"
78
DEBUG=0
89
NOCASE=0
910
WHOLEWORD=0
@@ -119,8 +120,9 @@ usage() {
119120
# ---------------------------------------------------------------------------
120121

121122
echo
122-
echo "Usage: JSONPath.sh [-[hbjuipwnsSAT]] [-f FILE] [pattern]"
123+
echo "Usage: JSONPath.sh [-[vhbjuipwnsSAT]] [-f FILE] [pattern]"
123124
echo
125+
echo "-v - Print the version of this script."
124126
echo "-h - Print this help text."
125127
echo "-b - Brief. Only show values."
126128
echo "-j - JSON output."
@@ -130,8 +132,8 @@ usage() {
130132
echo "-w - Match whole words only (for filter script expression)."
131133
echo "-f FILE - Read a FILE instead of stdin."
132134
echo "-n - Do not print header."
133-
echo "-s - Normalize solidus."
134-
echo "-S - Print spaces around :'s."
135+
echo "-s - Normalize solidus, e.g. convert \"\/\" to \"/\"."
136+
echo "-S - Print spaces around colons, producing ' : '."
135137
echo "-A - Start array on same line as JSON member."
136138
echo "-T - Indent with tabs instead of 4 character spaces."
137139
echo "pattern - the JSONPath query. Defaults to '$.*' if not supplied."
@@ -143,62 +145,77 @@ parse_options() {
143145
# ---------------------------------------------------------------------------
144146

145147
set -- "$@"
146-
while getopts :hbjuipwf:nsSAT flag; do
147-
case "$flag" in
148-
h) usage
149-
exit 0
150-
;;
151-
b) BRIEF=1
152-
;;
153-
j) JSON=1
154-
;;
155-
u) FLATTEN=1
156-
;;
157-
i) NOCASE=1
158-
;;
159-
p) PASSTHROUGH=1
160-
;;
161-
w) WHOLEWORD=1
162-
;;
163-
f) FILE="$OPTARG"
164-
;;
165-
n) NO_HEAD=1
166-
;;
167-
s) NORMALIZE_SOLIDUS=1
168-
;;
169-
S) COLON_SPACE=1
170-
;;
171-
A) ARRAY_SAME_LINE=1
172-
;;
173-
T) TAB_INDENT=1
174-
;;
175-
\?) echo "$0: ERROR: invalid option: -$OPTARG" 1>&2
176-
usage
177-
exit 3
148+
149+
local arg ARGN=$#
150+
declare -a expanded_args
151+
152+
# Expand args like -abc to -a -b -c
153+
while [ "$ARGN" -ne 0 ]; do
154+
arg="$1"
155+
if [[ $arg == -[a-zA-Z][a-zA-Z]* ]]; then
156+
# Remove the leading dash
157+
arg="${arg#-}"
158+
# Split the remaining characters and add dashes
159+
for i in ${arg//[a-zA-Z]/ -&}; do
160+
expanded_args+=("$i")
161+
done
162+
else
163+
expanded_args+=("$arg")
164+
fi
165+
ARGN=$((ARGN-1))
166+
shift 1
167+
done
168+
169+
set -- "${expanded_args[@]}"
170+
ARGN=$#
171+
while [ "$ARGN" -ne 0 ]
172+
do
173+
case $1 in
174+
-h) usage
175+
exit 0
176+
;;
177+
-v) echo "Version: $VERSION"
178+
exit 0
179+
;;
180+
-f) shift
181+
[[ ! -e $1 ]] && {
182+
echo "ERROR: -f '$1' does not exist." 1>&2
183+
exit 1
184+
}
185+
FILE=$1
186+
;;
187+
-i) NOCASE=1
188+
;;
189+
-j) JSON=1
190+
;;
191+
-n) NO_HEAD=1
192+
;;
193+
-b) BRIEF=1
194+
;;
195+
-u) FLATTEN=1
196+
;;
197+
-p) PASSTHROUGH=1
198+
;;
199+
-w) WHOLEWORD=1
200+
;;
201+
-s) NORMALIZE_SOLIDUS=1
202+
;;
203+
-S) COLON_SPACE=1
178204
;;
179-
:) echo "$0: ERROR: option -$OPTARG requires an argument" 1>&2
180-
usage
181-
exit 3
205+
-A) ARRAY_SAME_LINE=1
182206
;;
183-
*) echo "$0: ERROR: unexpected value from getopts: $flag" 1>&2
184-
usage
185-
exit 3
207+
-T) TAB_INDENT=1
186208
;;
209+
-?*) usage
210+
echo "$0: ERROR: invalid option: $1" 1>&2
211+
exit 3
212+
;;
213+
?*) QUERY=$1
214+
;;
187215
esac
216+
shift 1
217+
ARGN=$((ARGN-1))
188218
done
189-
# remove the options
190-
#
191-
shift $(( OPTIND - 1 ));
192-
# parse optional patten
193-
case "$#" in
194-
0) QUERY='$.*'
195-
;;
196-
1) QUERY="$*"
197-
;;
198-
*) echo "$0: ERROR: expected 0 or 1 args, found: $#" 1>&2
199-
usage
200-
;;
201-
esac
202219
}
203220

204221
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)