I am doing printf '%s\n' "$@" and calling the function using
pfm "-d DIR" "--directory=DIR"
I get errors, namely:
bash: invalid option -- 'd'
bash: invalid option -- ' '
bash: invalid option -- 'D'
bash: invalid option -- 'I'
bash: invalid option -- 'R'
bash: unrecognized option '--directory=DIR'
Here is the relevant code
printfm ()
{
# Process command line options
shortopts="hVw::"
longopts="help,version,warning:"
opts=$(getopt -o "$shortopts" -l "$longopts" \
-n "$(basename $0)" -- "$@")
if [ $? -eq 0 ]; then
eval "set -- ${opts}"
while [ $# -gt 0 ]; do
case "$1" in
-V|version)
printf "V01 Jul 2021 Wk27"
printf ""
;;
-h|-\?|--help)
help=1
printf "Prints two text strings on two lines.\n"
printf "\$@ TEXT Sentences to print en new lines.\n"
shift
local -r f=0
break
;;
# ......................................................
-w|--warning)
case "$2" in
"1") local -r warn="first"; shift 2 ;;
*) local -r warn="all"; shift 2 ;;
esac
local -r f=1
;;
--)
shift; break ;;
esac
done
else
shorthelp=1 # getopt returned (and reported) an error.
fi
red=$(tput setaf 9)
rgl=$(tput sgr0)
local f=1
if (( f == 1 )); then
# print normal multi-line text
[[ ! -z warn ]] && printf '%s\n' "$@"
# print multi-line warnings
if [[ -n warn && "$warn" == "first" ]]; then
printf '%s\n' ${red}"$1"${rgl} # first line red
printf '%s\n' "${@:2}" # remaining, uncoloured
elif [[ -n warn && "$warn" == "all" ]]; then
printf '%s\n' ${red}"$@"${rgl} # all lines red
fi
fi
return 0
}
alias pfm=printfm