Skip to content

Instantly share code, notes, and snippets.

@achalddave
Last active May 7, 2020 03:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save achalddave/290f7fcad89a0d7c3719 to your computer and use it in GitHub Desktop.
Save achalddave/290f7fcad89a0d7c3719 to your computer and use it in GitHub Desktop.
#!/bin/sh
usage() {
cat << EOF
$0 -a <a1> <a2> <a3> [-b] <b1> [-c]
-a First flag; takes in 3 arguments
-b Second flag; takes in 1 argument
-c Third flag; takes in no arguments
EOF
}
is_flag() {
# Check if $1 is a flag; e.g. "-b"
[[ "$1" =~ -.* ]] && return 0 || return 1
}
# Note:
# For a, we fool getopts into thinking a doesn't take in an argument
# For b, we can just use getopts normal behavior to take in an argument
while getopts "ab:c" opt ; do
case "${opt}" in
a)
# This is the tricky part.
# $OPTIND has the index of the _next_ parameter; so "\${$((OPTIND))}"
# will give us, e.g., ${2}. Use eval to get the value in ${2}.
# The {} are needed in general for the possible case of multiple digits.
eval "a1=\${$((OPTIND))}"
eval "a2=\${$((OPTIND+1))}"
eval "a3=\${$((OPTIND+2))}"
# Note: We need to check that we're still in bounds, and that
# a1,a2,a3 aren't flags. e.g.
# ./getopts-multiple.sh -a 1 2 -b
# should error, and not set a3 to be -b.
if [ $((OPTIND+2)) -gt $# ] || is_flag "$a1" || is_flag "$a2" || is_flag "$a3"
then
usage
echo
echo "-a requires 3 arguments!"
exit
fi
echo "-a has arguments $a1, $a2, $a3"
# "shift" getopts' index
OPTIND=$((OPTIND+3))
;;
b)
# Can get the argument from getopts directly
echo "-b has argument $OPTARG"
;;
c)
# No arguments, life goes on
echo "-c"
;;
esac
done
@suchoudh
Copy link

suchoudh commented May 7, 2020

Wanted to share an observation when i am running this.

$ ./getopts-multiple.sh -a a a a
./getopts-multiple.sh : 14: ./getopts-multiple.sh : [[: not found
./getopts-multiple.sh : 14: ./getopts-multiple.sh : [[: not found
./getopts-multiple.sh : 14: ./getopts-multiple.sh : [[: not found
-a has arguments a, a, a
$
So it works wonderfully well.
(small error which can be improved )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment