Roland Mainz schrieb:
The following script tries to use two functions which each seperately
use "getopts" for argument parsing:
-- snip --
function modify_target_param2
{
        usage=":"
        usage="${usage}B:(bi-directional-authentication)"

        while getopts "${usage}" option
        do
                case ${option} in
                "B")
                        bi="${OPTARG}"
                        ;;
                "?")
                        (( OPTIND = ${OPTIND} + 1 ))
                        ;;
                esac
                
        done
}

function modify_target_param
{
set -o xtrace
        usage=":"
        usage="${usage}B:(bi-directional-authentication)"

        while getopts "${usage}" option
        do
                case ${option} in
                "B")
                        bi="${OPTARG}"
                        ;;
                "?")
                        (( OPTIND = ${OPTIND} + 1 ))
                        ;;
                esac
                ${mod} && modify_target_param2 -B enable target
        done
        return 0
}

mod=$1
[[ ${mod} == ~(Elr)(true|false) ]] || exit 10

modify_target_param -B enable target

print "#done"
exit 0
-- snip --

>...


Why is the "getopts" parsing of function "modify_target_param2"
affecting the parsing in function "modify_target_param" (AFAIK I would
expect that they can each have their independent "getopts" parsing) ?

They share the global variable OPTIND. You have to save and restore the actual value of OPTIND when using a nested getopt. The following example works:

#----------------------------------------------
function f
{
    typeset oldindex opt

    while getopts "a:b" opt
    do
        print "f opt=$opt OPTARG=$OPTARG"
        oldindex=$OPTIND
        g -x 777
        OPTIND=$oldindex
    done
}

function g
{
    typeset opt

    while getopts "x:y" opt
    do
        print "g opt=$opt OPTARG=$OPTARG"
    done
}

f -b -a 123
#----------------------------------------------

I also think explicitly incrementing OPTIND is wrong, as this is already done by getopts itself.

Bernd

--
Bernd Eggink
http://sudrala.de
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to