track/track

170 lines
3.6 KiB
Plaintext
Raw Normal View History

2018-05-29 21:36:02 +00:00
#!/bin/sh
# This file defines track - a minimalist data tracker
# Copyright (C) 2018 tomasino@sdf.org
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------
version="v0.0.1"
2018-05-29 21:36:02 +00:00
show_help () {
cat > /dev/stdout << END
track [options] [metric] [value]
USAGE:
track weight 150 Log 150lbs for today's weight
track -n 10 calories Show last 10 days calorie values
track mood Show last value logged for mood
OPTIONS:
-a Show all values of metric
-n N Show N values of metric
-x Remove metric file
-h Show this help
-v Show current version info
-d Debug mode
END
}
arg_metric=""
arg_value=""
flag_options="hvdan:x:s"
flag_debug=0
flag_help=0
flag_listall=0
flag_listnum=0
flag_remove=0
flag_version=0
flag_shortlist=0
die () {
msg="$1"
code="$2"
# exit code defaults to 1
if printf "%s" "$code" | grep -q '^[0-9]+$'; then
code=1
fi
# output message to stdout or stderr based on code
if [ ! -z "$msg" ]; then
if [ "$code" -eq 0 ]; then
printf "%s\\n" "$msg"
else
printf "%s\\n" "$msg" >&2
fi
fi
exit "$code"
}
parse_input () {
if ! parsed=$(getopt $flag_options "$@"); then
die "Invalid input" 2
fi
eval set -- "$parsed"
while true; do
case "$1" in
-h)
flag_help=1
shift
;;
-v)
flag_version=1
shift
;;
-d)
flag_debug=1
shift
;;
-a)
flag_listall=1
shift
;;
-n)
flag_listnum=1
shift
arg_listnum="$1"
shift
;;
-x)
flag_remove=1
shift
arg_remove="$1"
shift
;;
-s)
flag_shortlist=1
shift
;;
--)
shift
break
;;
*)
die "Internal error: $1" 3
;;
esac
done
for arg in "$@"; do
argc=$(printf "%s" "$arg" | tr '[:upper:]' '[:lower:]')
if [ "$arg_metric" = "" ]; then
arg_metric="$argc"
elif [ "$arg_value" = "" ]; then
arg_value="$argc"
fi
done
}
main () {
parse_input "$@"
if [ $flag_shortlist -gt 0 ]; then
out="-v -h -d -a -n -x"
die "${out}" 0
fi
if [ $flag_version -gt 0 ]; then
printf "%s\\n" "$version"
fi
if [ $flag_help -gt 0 ]; then
show_help
fi
if [ $flag_debug -gt 0 ]; then
# set -x
printf "arg_metric=%s\n" "$arg_metric"
printf "arg_value=%s\n" "$arg_value"
printf "arg_listnum=%s\n" "$arg_listnum"
printf "arg_remove=%s\n" "$arg_remove"
printf "flag_debug=%s\n" "$flag_debug"
printf "flag_help=%s\n" "$flag_help"
printf "flag_listall=%s\n" "$flag_listall"
printf "flag_listnum=%s\n" "$flag_listnum"
printf "flag_remove=%s\n" "$flag_remove"
printf "flag_version=%s\n" "$flag_version"
printf "flag_shortlist=%s\n" "$flag_shortlist"
fi
}
main "$@"