Skip to content

Instantly share code, notes, and snippets.

@fud
Created September 20, 2016 05:15
Show Gist options
  • Select an option

  • Save fud/0c295ee8e1b4d317782a16c98c1caba0 to your computer and use it in GitHub Desktop.

Select an option

Save fud/0c295ee8e1b4d317782a16c98c1caba0 to your computer and use it in GitHub Desktop.
Shell command line arguments.
#!/usr/bin/env bash
prog=$(basename ${0})
# Defaults
verbose=0
env="staging"
function help() {
printf "Usage: %s [-h/--help] [-e/--env <env>] <url>\n" "$prog"
exit 0
}
# Parse Options
while :; do
case $1 in
-h|-\?|--help)
help
exit
;;
-e|--env)
if [ -n "$2" ]; then
env=$2
shift 2
continue
else
printf "ERROR: -e/--env requires a non-empty option argument.\n" >&2
exit 1
fi
;;
-v|--verbose)
verbose=$((verbose + 1))
;;
--)
shift
break
;;
-?*)
printf "WARN: Unknown option (ignored): %s\n" "$1" >&2
;;
*)
break
esac
command shift
done
if (( $verbose > 0 )); then
echo "env=${env}"
echo "url='${@}'"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment