sshroot: handle unknown args correctly
authorAlex Dehnert <adehnert@mit.edu>
Sat, 15 Dec 2012 11:51:05 +0000 (06:51 -0500)
committerAlex Dehnert <adehnert@mit.edu>
Sat, 15 Dec 2012 11:51:05 +0000 (06:51 -0500)
sshroot

diff --git a/sshroot b/sshroot
index f4d8996ffc2044745e1ae8d0553715657c6c9a13..845a04ac249c4c9cefca2027a8bdbad14f25ce39 100755 (executable)
--- a/sshroot
+++ b/sshroot
@@ -8,12 +8,26 @@ hostopt=""
 [ -z "$hostopt" ] && hostopt="${HOSTNAME}"
 [ -z "$hostopt" ] && hostopt=`uname -n 2>/dev/null || echo unknown`
 
-while getopts "H:k:l:" opt; do
+function usage
+{
+    echo "Usage: $0 [-H hostname] [-k keysuffix] [-l lifetime] command"
+    echo
+    echo "Available commands:"
+    echo "    add"
+    echo "    list"
+    echo "    destroy"
+    echo "    shell"
+    echo "    ssh [args]"
+    echo "    [cmd]"
+}
+
+while getopts ":H:k:l:h" opt; do
     case "$opt" in
     H)  hostopt="$OPTARG";;
     k)  keysuffix="$OPTARG";;
     l)  lifetime="$OPTARG";;
-    \?) die "$0 [-H hostname] [-k keysuffix] [-l lifetime]"
+    h)  usage; exit 0;;
+    \?) usage >&2; exit 1;;
     esac
 done
 shift $(($OPTIND - 1))
@@ -65,13 +79,7 @@ case "$command" in
         ;;
     *)
         if [ -z "$command" ]; then
-            echo "Usage: $0 init" >&2
-            echo "       $0 add" >&2
-            echo "       $0 list" >&2
-            echo "       $0 destroy" >&2
-            echo "       $0 shell" >&2
-            echo "       $0 ssh [args]" >&2
-            echo "       $0 [cmd]" >&2
+            usage >&2
         else
             echo Executing: "$command" "$@"
             with-agent "$command" "$@"