summaryrefslogtreecommitdiff
path: root/dicewords
diff options
context:
space:
mode:
Diffstat (limited to 'dicewords')
-rwxr-xr-xdicewords39
1 files changed, 34 insertions, 5 deletions
diff --git a/dicewords b/dicewords
index 3a450fa..49d9230 100755
--- a/dicewords
+++ b/dicewords
@@ -1,11 +1,40 @@
-#!/bin/sh
+#!/bin/bash
+
+set -e
+set -u
+
+usage() {
+ echo "Usage: $0 [-d <dictionary] [count]"
+}
+
+dictionary=american-english
+
+while getopts "hd:" OPTION; do
+ case "$OPTION" in
+ h)
+ usage
+ exit 0
+ ;;
+ d)
+ dictionary="$OPTARG"
+ ;;
+ *)
+ usage >&2
+ exit 1
+ esac
+done
+shift $(($OPTIND - 1))
count=${1:-6}
-if [ "$count" = "-h" ]; then
- echo "Usage: $0 [count]"
+if ! [[ $dictionary == *"/"* ]]; then
+ dictionary=/usr/share/dict/"$dictionary"
fi
-egrep '^[a-z]{1,6}$' /usr/share/dict/american-english | shuf | head -n "$count" | tr '\n' '-' | sed -e 's/-$//'
-echo
+if ! [ -e "$dictionary" ]; then
+ echo >&2 "$dictionary does not exist"
+ exit 1
+fi
+egrep '^[a-z]{1,6}$' "$dictionary" | shuf | head -n "$count" | tr '\n' '-' | sed -e 's/-$//'
+echo