You are not logged in.
I have a script using getops and 'OPTARG.
The script catches unbound variables 'set -euo nounset'
Most options in getops need OPTARG but some don't.
How do I not get 'OPTARG: unbound variable' if this getops option don't need it?
The point is I can set OPTARG once like I show below or set it for every option.
If set for every option there are no problems.
If set once I will be pestered with an 'unbound variable.
My question, how can I use 'nounset' but exclude 'OPTARG'
Or is the only way set 'prog=$OPTARG' for every option that needs it?
The next piece is an example:
if (( ${#@} > 0 )); then
while getopts 'a:b:c:d:ef' flag; do
prog="$OPTARG" <== set this OPTARG once is preferred!!
case "$flag" in
a)
clone
;;
b)
clone
bld_inst
;;
c)
pull
bld_inst
;;
d)
pull
force='on'
bld_inst
;;
e)
force='on'
bld_inst
;;
f)
search
;;
*)
printf '%s\n' "Add program name"
exit 1
;;
esac
done
else
printf '%s\n' "Wrong option run script without argument for a small help" \
exit 1
fi
Last edited by qinohe (2022-03-15 16:10:16)
Offline
prog=${OPTARG:-}
?
aka Tamaranch: https://gitlab.xfce.org/Tamaranch
Offline
If OPTARG is unset, why didn't I think of that, thanks.
Offline
That would cause $prog to be empty if options are passed as `-f foobar -e`, which is probably not what you want.
But does $prog actually need to be an argument to each of those functions? It looks to me more like $prog is meant to be a positional/mandatory argument that comes after the options.
And really it looks like you're trying to build yet another™ AUR helper, and these options are really just different modes of operation—why not simply treat them as subcommands rather than options?
Offline
That would cause $prog to be empty if options are passed as `-f foobar -e`, which is probably not what you want.
True.
But does $prog actually need to be an argument to each of those functions? It looks to me more like $prog is meant to be a positional/mandatory argument that comes after the options.
Yes I went a different path I now use 'prog="${BASH_ARGV[0]}"' to get the last argument. Options as different functions, done!
And really it looks like you're trying to build yet another™ AUR helper, and these options are really just different modes of operation—why not simply treat them as subcommands rather than options?
Aurhelper, yes:-) well problem solved with above variable, thanks for the input, appreciated.
Offline