From 441afcb48a14d32ee3087fe1a88fc6b1a42fdaaa Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:52:46 -0500 Subject: [PATCH] Add cli arg-parsing and usage message --- mkdeck.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/mkdeck.sh b/mkdeck.sh index 5108945..9cf6649 100755 --- a/mkdeck.sh +++ b/mkdeck.sh @@ -6,9 +6,38 @@ # Date: #――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― +FRENCH_CARDS="♣♚ ♣♛ ♣♝ ♠♚ ♠♛ ♠♝ ♢♔ ♢♕ ♢♗ ♡♔ ♡♕ ♡♗ 🃏 🃏" FRENCH_SUITS="♣ ♢ ♡ ♠" FRENCH_SUIT_CARDS="A 2 3 4 5 6 7 8 9 10" -FRENCH_ADDITIONAL_CARDS="♣♚ ♣♛ ♣♝ ♠♚ ♠♛ ♠♝ ♢♔ ♢♕ ♢♗ ♡♔ ♡♕ ♡♗ 🃏 🃏" + + +usage() { + echo "usage: $(basename "$0") [-h] [-d DECK_PREFIX] DIRECTORY" + echo + echo "$(basename "$0") creates a directory representing a card-deck." + echo 'That is, it creates empty files representing the decks’ cards,' + echo 'with the files’ names corresponding to their card in the deck.' + echo + echo ' -d specify the deck-prefix to use' + echo ' -h print this message and exit' + echo + echo 'A deck-prefix is the name of the deck, and determines the' + echo 'variables that will be used to determine the deck’s cards.' + echo 'For instance, if the deck-prefix is FRENCH — the deck' + echo 'hard-coded echo "into $(basename "$0") — then the variables' + echo 'FRENCH_CARDS, FRENCH_SUITS, and FRENCH_SUIT_CARDS will be used.' + echo + echo '$NAME_CARDS contains a space-delimited list of strings of raw' + echo 'card-names, like: “♣♚ ♢♔ 🃏”' + echo '$NAME_SUITS contains a list of suits, like: “♣ ♡”' + echo '$NAME_SUIT_CARDS containts a list of cards each suit has, for' + echo 'example: “1 2 3 4”' + echo + echo 'You can define a deck simply using $NAME_CARDS, or you can use' + echo '$NAME_SUITS and $NAME_SUITS_CARDS to help reduce repetiton on' + echo 'your part. Substitute “NAME” with your preferred deck-name.' +} + shell_sanitize() { local str="$1" @@ -19,7 +48,7 @@ shell_sanitize() { suit_cards() { local name="$(shell_sanitize "$1")" - local suits=n"$(eval "echo \$${name}_SUITS")" + local suits="$(eval "echo \$${name}_SUITS")" local suit_cards="$(eval "echo \$${name}_SUIT_CARDS")" for suit in $suits; do @@ -28,7 +57,7 @@ suit_cards() { done done - eval "echo \$${name}_ADDITIONAL_CARDS" + eval "echo \$${name}_CARDS" } @@ -52,7 +81,7 @@ mkdeck_cards() { if test -z "$cards"; then 1>&2 echo "No cards found for deck $deck_name. Check your environment variables:" - 1>&2 echo "${deck_name}_SUITS, ${deck_name}_SUIT_CARDS, and ${deck_name}_ADDITIONAL_CARDS." + 1>&2 echo "${deck_name}_CARDS, ${deck_name}_SUITS, and ${deck_name}_SUIT_CARDS." exit 3 fi @@ -70,4 +99,26 @@ mkdeck() { } -mkdeck "$1" "FRENCH" +DECK="$FRENCH" +while getopts 'hd:' arg; do + case $arg in + h) + usage + exit 0 + ;; + d) + DECK="$OPTARG" + ;; + esac +done + +shift $((OPTIND-1)) + +CARD_DIR="$1" + +if test -z "$CARD_DIR"; then + usage 1>&2 + exit 4 +fi + +mkdeck "$CARD_DIR" "$DECK"