Skip to content

Instantly share code, notes, and snippets.

@babarot
Created May 31, 2014 10:05
Show Gist options
  • Save babarot/3699322c612711181251 to your computer and use it in GitHub Desktop.
Save babarot/3699322c612711181251 to your computer and use it in GitHub Desktop.
Command line option parser templete for bash
#!/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