-
-
Save wention/005446d56cb96a5c621f to your computer and use it in GitHub Desktop.
Command line option parser templete for bash
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/bash | |
PROGNAME=$(basename $0) | |
VERSION="1.0.0" | |
usage() { | |
echo "Usage: $PROGNAME [OPTIONS] [FILE]" | |
echo " This script is ~." | |
echo | |
echo "Options:" | |
echo " -h, --help" | |
echo " --version" | |
echo " -a, --long-a ARG" | |
echo " -b, --long-b [ARG]" | |
echo " -c, --check" | |
echo | |
exit 1 | |
} | |
for OPT in "$@" | |
do | |
case "$OPT" in | |
'-h'|'--help' ) | |
usage | |
exit 1 | |
;; | |
'--version' ) | |
echo $VERSION | |
exit 1 | |
;; | |
'-a'|'--long-a' ) | |
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then | |
echo "$PROGNAME: option requires an argument -- $1" 1>&2 | |
exit 1 | |
fi | |
ARG_A="$2" | |
shift 2 | |
;; | |
'-b'|'--long-b' ) | |
if [[ -z "$2" ]] || [[ "$2" =~ ^-+ ]]; then | |
shift | |
else | |
shift 2 | |
fi | |
;; | |
'-c'|'--check' ) | |
shift 1 | |
;; | |
'--'|'-' ) | |
shift 1 | |
param+=( "$@" ) | |
break | |
;; | |
-*) | |
echo "$PROGNAME: illegal option -- '$(echo $1 | sed 's/^-*//')'" 1>&2 | |
exit 1 | |
;; | |
*) | |
if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then | |
#param=( ${param[@]} "$1" ) | |
param+=( "$1" ) | |
shift 1 | |
fi | |
;; | |
esac | |
done | |
if [ -z $param ]; then | |
echo "$PROGNAME: too few arguments" 1>&2 | |
echo "Try '$PROGNAME --help' for more information." 1>&2 | |
exit 1 | |
fi | |
for ((i=0; i<${#param[@]}; i++)); do | |
echo ${param[i]} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment