Skip to content

Commit 3f79e10

Browse files
authored
add -4, update -T, fix shellcheck warnings (#18)
Added `-4` to change indenting from 2 spaces to 4. Now `-T` will `unexpand(1)` tabs using 2 or 4 spaces according to use of `-4` or not. Added missing "c", removed unused "A" in 1st line of usage. Order usage option lines according to 1st line of usage. Removed trailing whitespace. Added missing "*)" final case switch options. Fixed a number of shellcheck warnings. Changed VERSION from "0.0.21" to "0.0.22".
1 parent 2734246 commit 3f79e10

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

JSONPath.sh

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

7-
VERSION="0.0.21"
7+
VERSION="0.0.22"
88
DEBUG=0
99
NOCASE=0
1010
WHOLEWORD=0
@@ -19,6 +19,7 @@ FLATTEN=0
1919
COLON_SPACE=0
2020
CONDENSED=0
2121
TAB_INDENT=0
22+
INDENT_SIZE=2
2223
STDINFILE=/var/tmp/JSONPath.$$.stdin
2324
STDINFILE2=/var/tmp/JSONPath.$$.stdin2
2425
PASSFILE=/var/tmp/JSONPath.$$.pass1
@@ -120,22 +121,23 @@ usage() {
120121
# ---------------------------------------------------------------------------
121122

122123
echo
123-
echo "Usage: JSONPath.sh [-[vhbjuipwnsSAT]] [-f FILE] [pattern]"
124+
echo "Usage: JSONPath.sh [-[vhbjuipwnsSTc4]] [-f FILE] [pattern]"
124125
echo
125126
echo "-v - Print the version of this script."
126127
echo "-h - Print this help text."
127128
echo "-b - Brief. Only show values."
129+
echo "-j - Output in JSON format."
130+
echo "-u - Strip unnecessary leading path elements."
128131
echo "-i - Case insensitive."
129132
echo "-p - Pass-through to the JSON parser."
130133
echo "-w - Match whole words only (for filter script expression)."
131-
echo "-f FILE - Read a FILE instead of stdin."
132134
echo "-n - Do not print header."
133-
echo "-T - Indent with tabs instead of 4 character spaces."
134-
echo "-u - Strip unnecessary leading path elements."
135-
echo "-j - Output in JSON format."
136135
echo "-s - JSON output: Normalize solidus, e.g. convert \"\/\" to \"/\"."
137136
echo "-S - JSON output: Print spaces around colons, producing ' : '."
137+
echo "-T - Indent with tabs instead of character spaces."
138138
echo "-c - JSON output: Condensed output."
139+
echo "-4 - indent using 4 spaces instead of 2."
140+
echo "-f FILE - Read a FILE instead of stdin."
139141
echo "pattern - the JSONPath query. Defaults to '$.*' if not supplied."
140142
echo
141143
}
@@ -150,7 +152,7 @@ parse_options() {
150152
declare -a expanded_args
151153

152154
# Expand args like -abc to -a -b -c
153-
while [ "$ARGN" -ne 0 ]; do
155+
while [[ "$ARGN" -ne 0 ]]; do
154156
arg="$1"
155157
if [[ $arg == -[a-zA-Z][a-zA-Z]* ]]; then
156158
# Remove the leading dash
@@ -168,8 +170,9 @@ parse_options() {
168170

169171
set -- "${expanded_args[@]}"
170172
ARGN=$#
171-
while [ "$ARGN" -ne 0 ]
173+
while [[ "$ARGN" -ne 0 ]]
172174
do
175+
# shellcheck disable=2249
173176
case $1 in
174177
-h) usage
175178
exit 0
@@ -206,6 +209,8 @@ parse_options() {
206209
;;
207210
-T) TAB_INDENT=1
208211
;;
212+
-4) INDENT_SIZE=4
213+
;;
209214
-?*) usage
210215
echo "$0: ERROR: invalid option: $1" 1>&2
211216
exit 3
@@ -675,7 +680,7 @@ brief() {
675680
if [[ $TAB_INDENT == 1 ]]; then
676681
# TODO should not be using another external tool
677682
# Only gawk, grep and sed are allowed
678-
unexpand -t 4
683+
unexpand -t "$INDENT_SIZE"
679684
else
680685
cat
681686
fi
@@ -752,7 +757,7 @@ json() {
752757

753758
tab=$(echo -e "\t")
754759

755-
[[ $CONDENSED -eq 0 ]] && { nl='\n'; spc=' '; tabsize=2; tsc=2; }
760+
[[ $CONDENSED -eq 0 ]] && { nl='\n'; spc=' '; tabsize=2; tsc="$INDENT_SIZE"; }
756761
[[ $COLON_SPACE -eq 1 ]] && cs=" "
757762

758763
if [[ $JSON -eq 0 ]]; then
@@ -776,6 +781,8 @@ json() {
776781
indent=$((indent-1))
777782
printf "%b%*s}" "$nl" "$((indent*tabsize))" ""
778783
;;
784+
*)
785+
;;
779786
esac
780787
done
781788
if [[ -n ${comma} ]]; then
@@ -793,6 +800,8 @@ json() {
793800
OBJECT)
794801
printf "%*s%s%s:%s" "$((indent*tabsize))" "" "${curpath[num_same+i]}" "$cs" "$spc"
795802
;;
803+
*)
804+
;;
796805
esac
797806
done
798807
fi
@@ -821,10 +830,12 @@ json() {
821830
indent=$((indent+1))
822831
tsc=0
823832
;;
833+
*)
834+
;;
824835
esac
825836
done
826837
fi
827-
838+
828839
[[ ${num_dropped} -eq 0 && ${num_changed} -eq 0
829840
&& ${num_new} -eq 0 && -n ${comma} ]] &&
830841
printf "%s%b" "${comma}" "$nl"; comma=;
@@ -851,6 +862,8 @@ json() {
851862
OBJECT)
852863
printf "%*s}%b" "$((i*tabsize))" "" "$nl"
853864
;;
865+
*)
866+
;;
854867
esac
855868
done
856869
fi

0 commit comments

Comments
 (0)