summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xwords31
1 files changed, 31 insertions, 0 deletions
diff --git a/words b/words
new file mode 100755
index 0000000..4516a1a
--- /dev/null
+++ b/words
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+# @describe Generates random words of specified length from dictionary
+# @meta require-tools grep,tr,shuf
+# @flag -1 one word per line
+# @option -l --length=8, Length of each generated word (in regexp format)
+# @option -n --words=1 Number of words to generate
+# @arg pattern regular expression pattern
+# @env WORDS_FILE=/usr/share/dict/words Path to dictionary file
+
+_list_words() {
+ iconv -t ASCII//TRANSLIT "$WORDS_FILE" | # Fix accents (like ö, ü, é)
+ grep -xE "[a-zA-Z]{${argc_length?}}" |
+ tr '[:upper:]' '[:lower:]' |
+ grep -E "${argc_pattern:-}" |
+ shuf -n "${argc_words?}"
+}
+
+main() {
+ mapfile -t words < <(_list_words)
+
+ if [ -n "${argc_1:-}" ]; then
+ printf "%s\n" "${words[@]}"
+ else
+ printf "%s\n" "${words[*]}"
+ fi
+}
+
+eval "$(argc --argc-eval "$0" "$@")"