Skip to content

Instantly share code, notes, and snippets.

@parkercoates
Forked from agateau/cmdline.sh
Last active May 4, 2017 19:16
Show Gist options
  • Save parkercoates/3f66effeb9691b7046b901ff4030bfcd to your computer and use it in GitHub Desktop.
Save parkercoates/3f66effeb9691b7046b901ff4030bfcd to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
PROGNAME=$(basename $0)
die() {
echo "$PROGNAME: $*" >&2
exit 1
}
usage() {
if [ "$*" != "" ] ; then
echo "Error: $*"
fi
cat << EOF
Usage: $PROGNAME [OPTION ...] [foo] [bar]
<Program description>.
Options:
-h, --help display this usage message and exit
-d, --delete delete things
-o, --output [FILE] write output to file
EOF
exit 1
}
foo=""
bar=""
delete=0
output="-"
args=()
while [ $# -gt 0 ] ; do
case "$1" in
-h|--help)
usage
;;
-d|--delete)
delete=1
;;
-o|--output)
output="$2"
shift
;;
-*=*)
flag=$(echo "$1" | sed -E 's/(-[^=]+)=.+/\1/')
value=$(echo "$1" | sed -E 's/-[^=]+=(.+)/\1/')
shift
set -- "$flag" "$value" $@
continue
;;
-*)
usage "Unknown option '$1'"
;;
*)
args=("${args[@]}" "$1")
;;
esac
shift
done
cat <<EOF
foo=$foo
bar=$bar
delete=$delete
output=$output
EOF
for arg in "${args[@]}" ; do
echo "'$arg'"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment