1. Doug Hellmann
  2. virtualenvwrapper

Commits

Axel H.  committed f40ffce

Added support for getopts with fallback on getopt.

  • Participants
  • Parent commits a20f320
  • Branches master

Comments (0)

Files changed (1)

File virtualenvwrapper.sh

View file
  • Ignore whitespace
 #    (cd "$WORKON_HOME"; find -L . -depth 3 -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
 }
 
+
+# test if a given command exists
+command_exists () {
+    command -v "$1" &> /dev/null ;
+}
+
 _lsvirtualenv_usage () {
     echo "lsvirtualenv [-blh]"
     echo "  -b -- brief mode"
 #
 # Usage: lsvirtualenv [-l]
 lsvirtualenv () {
-    typeset -a args
-    args=($(getopt blh "$@"))
-    if [ $? != 0 ]
+    
+    typeset long_mode=true
+    if command_exists getopts
     then
-        _lsvirtualenv_usage
-        return 1
+		# Use getopts when possible
+    	OPTIND=1
+		while getopts ":blh" opt "$@"
+		do
+			case "$opt" in
+				l) long_mode=true;;
+				b) long_mode=false;;
+				h)  _lsvirtualenv_usage;
+					return 1;;
+				?) echo "Invalid option: -$OPTARG" >&2;
+					_lsvirtualenv_usage;
+					return 1;;
+			esac
+		done
+    else
+    	# fallback on getopt for other shell
+	    typeset -a args
+	    args=($(getopt blh "$@"))
+	    if [ $? != 0 ]
+	    then
+	        _lsvirtualenv_usage
+	        return 1
+	    fi
+	    for opt in $args
+	    do
+	        case "$opt" in
+	            -l) long_mode=true;;
+	            -b) long_mode=false;;
+	            -h) _lsvirtualenv_usage;
+	                return 1;;
+	        esac
+	    done
     fi
-    typeset long_mode=true
-    for opt in $args
-    do
-        case "$opt" in
-            -l) long_mode=true;;
-            -b) long_mode=false;;
-            -h) _lsvirtualenv_usage;
-                return 1;;
-        esac
-    done
 
     if $long_mode
     then