1 |
On 16 May 2016 14:17, rindeal wrote: |
2 |
> So this is what it looks like now: |
3 |
|
4 |
still missing tests. see eclass/tests/flag-o-matic.sh. |
5 |
|
6 |
> - local f var findflag="$1" |
7 |
> - |
8 |
> - # this code looks a little flaky but seems to work for |
9 |
> - # everything we want ... |
10 |
> - # for example, if CFLAGS="-march=i686": |
11 |
> - # `get-flag -march` == "-march=i686" |
12 |
> - # `get-flag march` == "i686" |
13 |
> + local var pattern="${1}" |
14 |
|
15 |
drop the braces for builtin vars |
16 |
|
17 |
> for var in $(all-flag-vars) ; do |
18 |
> - for f in ${!var} ; do |
19 |
> - if [ "${f/${findflag}}" != "${f}" ] ; then |
20 |
> - printf "%s\n" "${f/-${findflag}=}" |
21 |
> + local i flags=( ${!var} ) |
22 |
> + for (( i=${#flags[@]}-1; i>=0; i-- )) ; do |
23 |
|
24 |
omitting spaces doesn't make code faster, it just makes it unreadable. |
25 |
for (( i = ${#flags[@]} - 1; i >= 0; --i )) ; do |
26 |
|
27 |
stick a comment above this for loop explaining why we walk backwards. |
28 |
|
29 |
> + local needle="-${pattern#-}" # force dash on |
30 |
|
31 |
avoid inline comments and make them complete sentences. |
32 |
# Make sure we anchor to the leading dash. |
33 |
local needle="-${pattern#-}" |
34 |
|
35 |
> + local haystack="${flags[i]%%=*}" # we're comparing flags, not values |
36 |
|
37 |
it's a bit easier to read if you unpack the flag explicitly. |
38 |
local flag=${flags[i]} |
39 |
|
40 |
> + if [[ ${haystack##${needle}} == '' ]] ; then |
41 |
|
42 |
use a -z test instead of comparing to '' |
43 |
-mike |