]> git.armaanb.net Git - bin.git/commitdiff
fortune: Improve argument parsing
authorArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 14:44:08 +0000 (09:44 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 14:44:08 +0000 (09:44 -0500)
fortune

diff --git a/fortune b/fortune
index 8d1ed43f441b63d798502811a19b1c7277982666..cd24a59c6cc0184ee71628eb3ab509dccd6cf3fe 100755 (executable)
--- a/fortune
+++ b/fortune
@@ -3,40 +3,41 @@
 
 usage()
 {
-    echo "fortune -- a simple awk/POSIX sh implementation of fortune"
-    echo ""
-    echo "\t-h, --help                   show this help message"
-    echo "\t-l, --list=\$FILE            list installed cookie files"
-    echo "\t-f=\$FILE, --file=\$FILE     specify path to cookie file"
-    echo "\t-c=\$FILE, --cookie=\$FILE   specify cookie file"
+  echo "Usage: fortune [OPTIONS]
+a simple POSIX sh/awk implementation of fortune
+
+  -h, --help                 show this help message
+  -l, --list                 list cookie files installed to /usr/share/fortune/
+  -f \$FILE, --file \$FILE     specify full path to cookie file
+  -c \$FILE, --cookie \$FILE   specify path to cookie file relative
+                             to /usr/share/fortune/
+"
 }
 
 FILE="$(find /usr/share/fortune/ -type f -not -name '*.dat')"
 while [ "$1" != "" ]; do
-  PARAM=`echo $1 | awk -F= '{print $1}'`
-  VALUE=`echo $1 | awk -F= '{print $2}'`
-  case $PARAM in
+  case $1 in
       -h | --help)
           usage
           exit
           ;;
       -f | --file)
-          FILE=$VALUE
+          FILE=$2
           ;;
       -c | --cookie)
-          FILE=/usr/share/fortune/$VALUE
+          FILE=/usr/share/fortune/$2
           ;;
       -l | --list)
         ls -1 /usr/share/fortune
         exit
         ;;
       *)
-          echo "ERROR: unknown parameter \"$PARAM\""
+          echo "ERROR: unknown parameter \"$2\""
           usage
           exit 1
           ;;
   esac
-  shift
+  shift 2
 done
 
 awk '