Commits

Axel H. committed 76447d6

Added support for getopts with fallback on getopt.

Comments (0)

Files changed (1)

virtualenvwrapper.sh

 #    (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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.