1 |
> [Looks like your mailer is broken. All the tabs in your patch have |
2 |
> been mangled and appear as spaces.] |
3 |
> |
4 |
>> + # reverse loop |
5 |
>> + set -- ${!var} |
6 |
>> + local i=${#} |
7 |
>> + while [[ ${i} > 0 ]] ; do |
8 |
>> + local arg="${!i}" |
9 |
> |
10 |
> Using the positional parameters looks needlessly complicated here. |
11 |
> Why not use an array, like this (untested): |
12 |
> |
13 |
> local -a flags=(${!var}) |
14 |
> for (( i=${#flags[@]}-1; i>=0; i-- )); do |
15 |
> |
16 |
> Below you can use ${flags[i]} instead of ${arg} then. |
17 |
|
18 |
Done. |
19 |
|
20 |
I've also changed comments and added examples section to docs. |
21 |
|
22 |
So this is what it looks like now: |
23 |
|
24 |
diff --git a/eclass/flag-o-matic.eclass b/eclass/flag-o-matic.eclass |
25 |
index e0b19e9..217d33b 100644 |
26 |
--- a/eclass/flag-o-matic.eclass |
27 |
+++ b/eclass/flag-o-matic.eclass |
28 |
@@ -534,18 +534,26 @@ strip-unsupported-flags() { |
29 |
# @USAGE: <flag> |
30 |
# @DESCRIPTION: |
31 |
# Find and echo the value for a particular flag. Accepts shell globs. |
32 |
+# |
33 |
+# Example: |
34 |
+# @CODE |
35 |
+# CFLAGS="-march=i686 -O1" |
36 |
+# get-flag -march # outputs "-march=i686" |
37 |
+# get-flag march # outputs "i686" |
38 |
+# get-flag '-O*' # outputs "-O1" |
39 |
+# @CODE |
40 |
get-flag() { |
41 |
- local f var findflag="$1" |
42 |
- |
43 |
- # this code looks a little flaky but seems to work for |
44 |
- # everything we want ... |
45 |
- # for example, if CFLAGS="-march=i686": |
46 |
- # `get-flag -march` == "-march=i686" |
47 |
- # `get-flag march` == "i686" |
48 |
+ local var pattern="${1}" |
49 |
for var in $(all-flag-vars) ; do |
50 |
- for f in ${!var} ; do |
51 |
- if [ "${f/${findflag}}" != "${f}" ] ; then |
52 |
- printf "%s\n" "${f/-${findflag}=}" |
53 |
+ local i flags=( ${!var} ) |
54 |
+ for (( i=${#flags[@]}-1; i>=0; i-- )) ; do |
55 |
+ local needle="-${pattern#-}" # force dash on |
56 |
+ local haystack="${flags[i]%%=*}" # we're comparing flags, not values |
57 |
+ if [[ ${haystack##${needle}} == '' ]] ; then |
58 |
+ # preserve only value if only flag name was provided |
59 |
+ local ret="${flags[i]#-${pattern}=}" |
60 |
+ # ${ret} might contain `-e` or `-n` which confuses echo |
61 |
+ printf '%s\n' "${ret}" |
62 |
return 0 |
63 |
fi |
64 |
done |