On 19 July 2010 20:43, Mike Frysinger <[email protected]> wrote:
> On Monday, July 19, 2010 03:38:39 Ciaran McCreesh wrote:
>> On Sun, 18 Jul 2010 20:17:45 -0700 Alec Warner wrote:
>> > Can we do away with all the extra foo && return bullshit and just set
>> > a trap?
>> >
>> > trap "eshopts pop" RETURN
>> >
>> >  ?
>>
>> That strikes me as a horribly fragile way of doing things that's bound
>> to come back and screw things up at some point...
>
> nifty in theory, but i'm inclined to agree with Ciaran
> -mike

Is something like the below function too hideous (not massively
tested, but it seems to work)?  Usage is something like:

[dlever...@shiny-one ~] $ foo() { shopt -p extglob; }
[dlever...@shiny-one ~] $ eshopts_need foo -s extglob
[dlever...@shiny-one ~] $ shopt -p extglob
shopt -u extglob
[dlever...@shiny-one ~] $ foo
shopt -s extglob
[dlever...@shiny-one ~] $ shopt -p extglob
shopt -u extglob

eshopts_need() {
    [[ $# -ge 1 ]] || die "eshopts_need needs at least one argument"
    local func=$1
    shift
    local opts=( "$...@}" )
    if [[ $1 == -[su] ]] ; then
        eval "_eshopts_need_shopt_$(declare -f $func)"
        eval "$func() {
            local old=\$(shopt -p)
            $(declare -p opts)
            shopt \"\${op...@]}\"
            _eshopts_need_shopt_$func \"\...@\"
            local status=\$?
            eval \"\$old\"
            return \$status
        }"
    else
        eval "_eshopts_need_set_$(declare -f $func)"
        eval "$func() {
            local old=\$-
            $(declare -p opts)
            set \"\${op...@]}\"
            _eshopts_need_set_$func \"\...@\"
            local status=\$?
            set +\$-
            set -\$old
            return \$status
        }"
    fi
}

Reply via email to