You are not logged in.

#1 2022-03-14 23:18:22

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Solved getops using 'OPTARG' unbound variable Q.

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

#2 2022-03-15 07:58:44

lambdarch
Member
Registered: 2021-01-10
Posts: 50

Re: Solved getops using 'OPTARG' unbound variable Q.

prog=${OPTARG:-}

?

Offline

#3 2022-03-15 16:09:44

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Solved getops using 'OPTARG' unbound variable Q.

If OPTARG is unset, why didn't I think of that, thanks.

Offline

#4 2022-03-15 22:46:06

ayekat
Member
Registered: 2011-01-17
Posts: 1,547
Website

Re: Solved getops using 'OPTARG' unbound variable Q.

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?


{,META,RE}PKGBUILDSpacman-hacks (includes makemetapkg and remakepkg) │ dotfiles

Offline

#5 2022-03-15 23:22:33

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,494

Re: Solved getops using 'OPTARG' unbound variable Q.

ayekat wrote:

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

Board footer

Powered by FluxBB