Last active
December 10, 2024 06:29
-
-
Save fujimaki-k/e52ec3b53dbdf3bf705ecc43b5f20282 to your computer and use it in GitHub Desktop.
Parse command line arguments.
This file contains hidden or 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
#!/bin/sh | |
# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: | |
set -Ceu | |
VERSION="1.0.0" | |
NAME="Honoka" | |
usage() { | |
cat << EOF | |
usage $(basename "$0") [-h] [-n NAME] | |
optional_arguments: | |
-n, --name your name. (default: $NAME) | |
-h, --help show this help message and exit. | |
-v, --version show version number. | |
EOF | |
} | |
for OPTION in $@ | |
do | |
case "$OPTION" in | |
-n|--name) | |
NAME="$2" | |
shift 2 | |
;; | |
--name=*) | |
NAME="${1#*=}" | |
NAME="${NAME#[\'\"]}" | |
NAME="${NAME%[\'\"]}" | |
shift 1 | |
;; | |
-v|--version) | |
echo "$VERSION" | |
exit 0 | |
;; | |
-h|--help) | |
usage | |
exit 0 | |
;; | |
esac | |
done | |
echo "Hello, $NAME." | |
# Local variables: | |
# tab-width: 4 | |
# c-basic-offset: 4 | |
# c-hanging-comment-ender-p: nil | |
# End: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment