Created
April 11, 2016 14:00
-
-
Save gibatronic/8b316efaece6429b333ad37d045682a3 to your computer and use it in GitHub Desktop.
bash snippet to parse arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
NAME=${NAME:-unknown} | |
main() { | |
while [ "$#" != 0 ]; do | |
case "$1" in | |
-h|--help) usage ;; | |
-n|--name) shift; set_name "$1" ;; | |
*) work $1; exit ;; | |
esac | |
shift | |
done | |
} | |
set_name() { | |
NAME="$1" | |
} | |
usage() { | |
echo | |
echo ' Usage:' | |
echo ' options [options] <command>' | |
echo | |
echo ' Options:' | |
echo ' -h --help print usage information' | |
echo ' -n --name your username' | |
echo | |
} | |
work() { | |
echo "NAME: "'"'"${NAME}"'"' | |
echo $1 | |
} | |
[ "$#" = 0 ] && usage || main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment