implement outputting values of keys not in tables

This commit is contained in:
Emma Tebibyte 2022-11-21 23:10:24 -05:00
parent ecf151ea40
commit 1d06d94058
2 changed files with 26 additions and 18 deletions

10
README.md Normal file
View File

@ -0,0 +1,10 @@
`tomcat` a [TOML](https://toml.io) parser in POSIX shell
## Installation
### From Source
Clone this repository and move the `tomcat` binary wherever your operating
system stores locally-installed binaries. This is usually `/usr/local/bin` or
`$HOME/.local/bin` for your user. Make sure the installation location is in your
`$PATH`.

34
tomcat
View File

@ -2,18 +2,13 @@
set -e set -e
ARG="$(printf "%s\n" "$1" | sed -n '/-/p')" argv1="$1"
argv2="$2"
# check usage # check usage
if ! test -n "$1"; then if ! test -n "$1"; then
printf "Usage: %s [OPTIONS] [TABLE.KEY[INDEX]] [FILE]\n" "$0" 1>&2 printf "Usage: %s [OPTIONS] [TABLE.KEY[INDEX]] [FILE]\n" "$0" 1>&2
exit 64 # sysexits(3) EX_USAGE exit 64 # sysexits(3) EX_USAGE
elif test -n "$ARG"; then
argv1="$2"
argv2="$3"
else
argv1="$1"
argv2="$2"
fi fi
# test if input has a period # test if input has a period
@ -22,10 +17,10 @@ if test -n "$(printf "%s\n" "$argv1" | sed -n '/.*\..*/p')"; then
TABLE=$(printf "%s\n" "$argv1" | sed -n 's/\..*//p') TABLE=$(printf "%s\n" "$argv1" | sed -n 's/\..*//p')
# cut out everything before the first period for KEY # cut out everything before the first period for KEY
KEY=$(printf "%s\n" "$argv1" | sed -n 's/^[^.]\+\.//p') KEY=$(printf "%s\n" "$argv1" | sed -n 's/^[^.]*\.//p')
else else
# set key to input if there is no table specified # assume the argument is a table
KEY=$argv1 TABLE="$argv1"
fi fi
# remove array index from KEY # remove array index from KEY
@ -42,10 +37,18 @@ else
TOML=$(printf "%s\n" "$argv2" | sed 's/[^"#]#\+.*//g') TOML=$(printf "%s\n" "$argv2" | sed 's/[^"#]#\+.*//g')
fi fi
# set VAL to the parsed TOML if test -n "$TABLE"; then
VAL=$(printf "%s\n" "$TOML" |\ KEYS=$(printf "%s\n" "$TOML" |\
# output only lines between TABLE and the next table # output only lines between TABLE and the next table
awk "/^\[$TABLE\]/{flag=1; next} /^\[/{flag=0} flag" - |\ awk "/^\[$TABLE\]/{flag=1; next} /^\[/{flag=0} flag" - )
else
KEYS=$(printf "%s\n" "$TOML" |\
# output only lines before the first table
awk '1;/^\[/{exit}' - | sed -n '/^[^[]/p')
fi
# set VAL to the parsed KEYS list
VAL=$(printf "%s\n" "$KEYS" |\
# remove the key from the string and delineate arrays, removing # remove the key from the string and delineate arrays, removing
# brackets, trailing commas, and leading spaces # brackets, trailing commas, and leading spaces
sed -n "s/$KEY *= *//p" | sed 's/", "/ /g' | tr -d '[]"' |\ sed -n "s/$KEY *= *//p" | sed 's/", "/ /g' | tr -d '[]"' |\
@ -57,11 +60,6 @@ if test -n "$ARR"; then
printf "%s\n" "$VAL" | sed 's/ /\n/g' | head -n "$ARR" |\ printf "%s\n" "$VAL" | sed 's/ /\n/g' | head -n "$ARR" |\
tail -n 1 tail -n 1
else else
if test -n "$ARG"; then
if [ "$1" = "-c" ] || [ "$1" = "--count" ]; then
VAL="$(printf "%s\n" "$VAL" | sed 's/ /\n/g' | wc -l)"
fi
fi
printf "%s\n" "$VAL" printf "%s\n" "$VAL"
fi fi