Getopts - What am I missing?

Hello

According to my knowledge and my re-searches on the forum, the getopts block in this code should work....

	bool2str() { # [options] $?
	# Returns a string from a BOOL input
	#
		swarm.protect "$FUNCNAME" "${@}" && exit 1
		#
		# Defaults
		#
			local caps #=false
			local CAPS #=false
			local mode #="yes"
			local out=""
			echo " ---------- $@ ----------- "
		#
		# Catching Arguments
		#
			# A ':' after a char indicates that this option requires an argument
			# Get access to the passed value of the argument by using $OPTARG
			while getopts "cCdoty" opt
			do
			 	case ${opt} in
				"c")	caps=true
						shift	;;
				"C")	CAPS=true
						shift	;;
				"d")	mode="done"
						shift
						echo " -- SHOULD -- get here"	;;
				"t")	mode="true"
						shift	;;
				"y")	mode="yes"
						shift	;;
				"o")	mode="on"
						shift	;;
			#	*)	echo opt $opt ; exit 99	;;
				esac
			done
			shift $(($OPTIND - 1))
		#
		# Display & Action
		# This also handles translations
		#
			case "${mode}" in
			"done")	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_DONE"	;;
				1)	out="$SWARM_MSG_BOOL2STR_FAIL"	;;
				esac
				;;
			"true")	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_TRUE"	;;
				1)	out="$SWARM_MSG_BOOL2STR_FALSE"	;;
				esac
				;;
			"on")	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_ON"	;;
				1)	out="$SWARM_MSG_BOOL2STR_OFF"	;;
				esac
				;;
			"yes"|*)	case "$1" in
				0)	out="$SWARM_MSG_BOOL2STR_YES"	;;
				1)	out="$SWARM_MSG_BOOL2STR_NO"	;;
				esac
				;;
			esac

			# Show the output
			if ${CAPS:-false}
			then	$PRINTF "${out^^}"
			elif ${caps:-false}
			then	$PRINTF "${out^}"
			else	$PRINTF "$out"
			fi
	}

However, once the function is sourced and I execute the code like:

set -x ; bool2str -d 1 ; set +x

All I get is this:

+ bool2str -d 1
+ swarm.protect bool2str -d 1
+ typeset arg ac=0
+ local fname=bool2str
+ shift
+ for arg in "$@"
+ ac=1
+ case "$arg" in
+ for arg in "$@"
+ ac=2
+ case "$arg" in
+ return 1
+ local caps
+ local CAPS
+ local mode
+ local out=
+ echo ' ---------- -d' '1 ----------- '
 ---------- -d 1 ----------- 
+ getopts cCdoty opt
+ shift 1
+ case "${mode}" in
+ case "$1" in
+ out=no
+ false
+ false
+ printf no
no+ set +x

Expected output:
'fail' instead of 'no' (yes/no is default, but should be changed by/with the passed option '-d' to 'done/fail')

NOTE:
Up to return 1 it's within the function swarm.protect which protects from code injection.

My biggest surprise is that it doesnt go into the "d" case - at all....
Not even when I changed the getopts case to "-d" rather than just "d".

Any ideas please?

Oh dang, guess I cant just copy paste the argument handling from the previous files...

Figured:

Guess this is this kind of an unspecified behaviour...
Welp, guess I'll need to work around (read:without) getopts within functions.... :frowning:

/solved

1 Like

Just came to mind - before this goes archive, if you look for a solution to this issue, here is mine:

for opt in "${@}"
do 	# Only parse for Arguments
	# if variable:'opt' starts with '-'
	[[ "-" == "${opt:0:1}" ]] && \
		case "${opt:1}" in
		"h" | "-help")
			echo "Usage: $FUNCNAME [options] arg1 arg2"
			exit 0
			;;
		"1" | "2" | "3")
			ROWS="${opt/-}"
			shift
			;;
		esac
done

Hope this helps and stay healthy