From 08661ecb1aaccc7077a03cf43011432c8db8ee17 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 10 Jul 2022 18:32:12 -0500 Subject: Add command-line options --- thesauracles | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/thesauracles b/thesauracles index 4812eb4..218cf88 100755 --- a/thesauracles +++ b/thesauracles @@ -63,31 +63,51 @@ query() { } random_word() { - n="$(wc -l <"$wf")" - ln="$((RANDOM % n + 1))" - sed -n ${ln}p "$wf" + sort -Ru "$wf" | head -n1 } main() { + degree=6 # the Kevin Bacon number ;) + talkative=true + + while getopts hqc:s:t:h: opt; do + case "$opt" in + h) usage ;; + q) talkative=false ;; + c) degree="$OPTARG" ;; + s) dict_server="$OPTARG" ;; + t) dict_database="$OPTARG" ;; + l) thesaurus_header_lines="$OPTARG" ;; + *) usage 1 ;; + esac + done + shift $((OPTIND - 1)) + word="$1" words=() hopn=0 - while [ "$hopn" -lt "$hops" ]; do + while [ "$hopn" -lt "$degree" ]; do + #printf '%s ' "$hopn" if query "$word"; then - echo "$word..." + $talkative && echo "$word..." >&2 words+=("$word") word="$(random_word)" : $((hopn++)) elif [ "$hopn" -eq 0 ]; then echo "Oops, don't know \"$word!\"" >&2 - exit 1 + exit 2 else - echo "hmm." + $talkative && echo "hmm." >&2 : $((hopn--)) word="${words[-1]}" fi done - echo "$word" | awk '{print "> " toupper($0) "!"}' + echo "$word" | + if $talkative; then + awk '{print "> " toupper($0) "!"}' + else + cat + fi } if [[ "$BASH_SOURCE" = "$0" ]]; then -- cgit 1.4.1-21-gabe81