Skip to content

Instantly share code, notes, and snippets.

@ambakshi
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambakshi/4992b41cc7d7f8048c9b to your computer and use it in GitHub Desktop.
Save ambakshi/4992b41cc7d7f8048c9b to your computer and use it in GitHub Desktop.
Use git-rev-parse to do the dirty work for you
#!/bin/bash
OPTS_SPEC="\
$(basename $0 .sh) subcommand <options>
subcommands:
data - download a dataset
--
h,help show help
d,debug debug output
v,verbose verbose output
f,file= input file
options for data
dataset= specify dataset to download (eg, 20140219_26816) (default: latest)
g,game-tag= specify game-tag (default: GAME_TAG env or hc)
"
cmd_data () {
echo "Hi from the data command"
echo "INPUT_FILE=$INPUT_FILE"
echo "DATASET=$DATASET"
echo "GAME_TAG=$GAME_TAG"
}
parse_args () {
if [ $# -eq 0 ]; then
set -- -h
fi
eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@" || echo exit $?)"
cmd=
while [ $# -gt 0 ]; do
cmd="$1"
shift
case "$cmd" in
-f|--file) export INPUT_FILE="$1"; shift ;;
-v|--verbose) export V=1;;
-d|--debug) export DEBUG=1 ;;
--dataset) export DATASET="$1"; shift ;;
-g|--game-tag) export GAME_TAG="$1"; shift ;;
--) cmd="$1"; shift; break ;;
-*) echo "Unknown option $cmd" >&2; exit 1 ;;
esac
done
if type -t "cmd_$cmd" > /dev/null; then
"cmd_$cmd"
else
echo "Unknown subcommand: $cmd" >&2
exit 1
fi
}
main () {
parse_args "$@"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment