Skip to content

Instantly share code, notes, and snippets.

@dstokes
Last active August 29, 2015 14:16
Show Gist options
  • Save dstokes/d0204bd933c3f44c3e9c to your computer and use it in GitHub Desktop.
Save dstokes/d0204bd933c3f44c3e9c to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Parse command flags and positional arguments regardless
# of argument order
#
# placeholder for postional arguments
PARAMS=""
# iterate through all command-line arguments
while (( "$#" )); do
case "$1" in
#
# Custom flag processing
#
# -r|--role)
# # do stuff
# shift 2 # flag with value
# ;;
#
# Standard processing
#
--) # end option parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1"
exit 1
;;
*) # save postitional args
PARAMS="$PARAMS $1"
shift
;;
esac
done
# reimport position arguments as $1 $2 etc
eval set -- "$PARAMS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment