1 |
On Monday, July 19, 2010 16:07:56 David Leverton wrote: |
2 |
> On 19 July 2010 20:43, Mike Frysinger <vapier@g.o> wrote: |
3 |
> > On Monday, July 19, 2010 03:38:39 Ciaran McCreesh wrote: |
4 |
> >> On Sun, 18 Jul 2010 20:17:45 -0700 Alec Warner wrote: |
5 |
> >> > Can we do away with all the extra foo && return bullshit and just set |
6 |
> >> > a trap? |
7 |
> >> > |
8 |
> >> > trap "eshopts pop" RETURN |
9 |
> >> > |
10 |
> >> > ? |
11 |
> >> |
12 |
> >> That strikes me as a horribly fragile way of doing things that's bound |
13 |
> >> to come back and screw things up at some point... |
14 |
> > |
15 |
> > nifty in theory, but i'm inclined to agree with Ciaran |
16 |
> |
17 |
> Is something like the below function too hideous (not massively |
18 |
> tested, but it seems to work)? Usage is something like: |
19 |
> |
20 |
> [dleverton@shiny-one ~] $ foo() { shopt -p extglob; } |
21 |
> [dleverton@shiny-one ~] $ eshopts_need foo -s extglob |
22 |
|
23 |
i imagine this might be useful in some scenarios, but i think the more common |
24 |
usage is to enable things inline. otherwise, the exported API would need to |
25 |
be wrapped internally like: |
26 |
get_all_version_components() { |
27 |
eshopts_need _get_all_version_components -s extglob |
28 |
} |
29 |
|
30 |
and at this point, it's hard to say this is better than just doing: |
31 |
get_all_version_components() { |
32 |
eshopts_push -s extglob |
33 |
_get_all_version_components |
34 |
eshopts_pop |
35 |
} |
36 |
|
37 |
... or the existing code we have now. although, the method we have now also |
38 |
allows for disabling of shopts before calling `die`. not sure if that's |
39 |
important, but i think it's better to disable before all exit/termination |
40 |
points. |
41 |
|
42 |
so unless their is a consumer now we can point to in the tree, i'm inclined to |
43 |
leave this alone. |
44 |
|
45 |
> eshopts_need() { |
46 |
> [[ $# -ge 1 ]] || die "eshopts_need needs at least one argument" |
47 |
> local func=$1 |
48 |
> shift |
49 |
> local opts=( "${@}" ) |
50 |
> if [[ $1 == -[su] ]] ; then |
51 |
> eval "_eshopts_need_shopt_$(declare -f $func)" |
52 |
> eval "$func() { |
53 |
> local old=\$(shopt -p) |
54 |
> $(declare -p opts) |
55 |
> shopt \"\${opts[@]}\" |
56 |
> _eshopts_need_shopt_$func \"\$@\" |
57 |
> local status=\$? |
58 |
> eval \"\$old\" |
59 |
> return \$status |
60 |
> }" |
61 |
|
62 |
there are already eshopts_push/pop funcs that accomodate more things than the |
63 |
usage here, but i imagine you did it this way just to make testing in your |
64 |
shell easier |
65 |
-mike |