From f588d6dbe7d74399ebca888e42aaa3420ddc47dd Mon Sep 17 00:00:00 2001 From: Landon Curt Noll Date: Mon, 21 Apr 2025 01:17:35 -0700 Subject: [PATCH] add -4, update -T, fix shellcheck warnings 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". --- JSONPath.sh | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/JSONPath.sh b/JSONPath.sh index f9f2033..d427f6a 100755 --- a/JSONPath.sh +++ b/JSONPath.sh @@ -4,7 +4,7 @@ # GLOBALS # --------------------------------------------------------------------------- -VERSION="0.0.21" +VERSION="0.0.22" DEBUG=0 NOCASE=0 WHOLEWORD=0 @@ -19,6 +19,7 @@ FLATTEN=0 COLON_SPACE=0 CONDENSED=0 TAB_INDENT=0 +INDENT_SIZE=2 STDINFILE=/var/tmp/JSONPath.$$.stdin STDINFILE2=/var/tmp/JSONPath.$$.stdin2 PASSFILE=/var/tmp/JSONPath.$$.pass1 @@ -120,22 +121,23 @@ usage() { # --------------------------------------------------------------------------- echo - echo "Usage: JSONPath.sh [-[vhbjuipwnsSAT]] [-f FILE] [pattern]" + echo "Usage: JSONPath.sh [-[vhbjuipwnsSTc4]] [-f FILE] [pattern]" echo echo "-v - Print the version of this script." echo "-h - Print this help text." echo "-b - Brief. Only show values." + echo "-j - Output in JSON format." + echo "-u - Strip unnecessary leading path elements." echo "-i - Case insensitive." echo "-p - Pass-through to the JSON parser." echo "-w - Match whole words only (for filter script expression)." - echo "-f FILE - Read a FILE instead of stdin." echo "-n - Do not print header." - echo "-T - Indent with tabs instead of 4 character spaces." - echo "-u - Strip unnecessary leading path elements." - echo "-j - Output in JSON format." echo "-s - JSON output: Normalize solidus, e.g. convert \"\/\" to \"/\"." echo "-S - JSON output: Print spaces around colons, producing ' : '." + echo "-T - Indent with tabs instead of character spaces." echo "-c - JSON output: Condensed output." + echo "-4 - indent using 4 spaces instead of 2." + echo "-f FILE - Read a FILE instead of stdin." echo "pattern - the JSONPath query. Defaults to '$.*' if not supplied." echo } @@ -150,7 +152,7 @@ parse_options() { declare -a expanded_args # Expand args like -abc to -a -b -c - while [ "$ARGN" -ne 0 ]; do + while [[ "$ARGN" -ne 0 ]]; do arg="$1" if [[ $arg == -[a-zA-Z][a-zA-Z]* ]]; then # Remove the leading dash @@ -168,8 +170,9 @@ parse_options() { set -- "${expanded_args[@]}" ARGN=$# - while [ "$ARGN" -ne 0 ] + while [[ "$ARGN" -ne 0 ]] do + # shellcheck disable=2249 case $1 in -h) usage exit 0 @@ -206,6 +209,8 @@ parse_options() { ;; -T) TAB_INDENT=1 ;; + -4) INDENT_SIZE=4 + ;; -?*) usage echo "$0: ERROR: invalid option: $1" 1>&2 exit 3 @@ -675,7 +680,7 @@ brief() { if [[ $TAB_INDENT == 1 ]]; then # TODO should not be using another external tool # Only gawk, grep and sed are allowed - unexpand -t 4 + unexpand -t "$INDENT_SIZE" else cat fi @@ -752,7 +757,7 @@ json() { tab=$(echo -e "\t") - [[ $CONDENSED -eq 0 ]] && { nl='\n'; spc=' '; tabsize=2; tsc=2; } + [[ $CONDENSED -eq 0 ]] && { nl='\n'; spc=' '; tabsize=2; tsc="$INDENT_SIZE"; } [[ $COLON_SPACE -eq 1 ]] && cs=" " if [[ $JSON -eq 0 ]]; then @@ -776,6 +781,8 @@ json() { indent=$((indent-1)) printf "%b%*s}" "$nl" "$((indent*tabsize))" "" ;; + *) + ;; esac done if [[ -n ${comma} ]]; then @@ -793,6 +800,8 @@ json() { OBJECT) printf "%*s%s%s:%s" "$((indent*tabsize))" "" "${curpath[num_same+i]}" "$cs" "$spc" ;; + *) + ;; esac done fi @@ -821,10 +830,12 @@ json() { indent=$((indent+1)) tsc=0 ;; + *) + ;; esac done fi - + [[ ${num_dropped} -eq 0 && ${num_changed} -eq 0 && ${num_new} -eq 0 && -n ${comma} ]] && printf "%s%b" "${comma}" "$nl"; comma=; @@ -851,6 +862,8 @@ json() { OBJECT) printf "%*s}%b" "$((i*tabsize))" "" "$nl" ;; + *) + ;; esac done fi