On 16 May 2016 14:17, rindeal wrote:
> So this is what it looks like now:

still missing tests.  see eclass/tests/flag-o-matic.sh.

> -   local f var findflag="$1"
> -
> -   # this code looks a little flaky but seems to work for
> -   # everything we want ...
> -   # for example, if CFLAGS="-march=i686":
> -   # `get-flag -march` == "-march=i686"
> -   # `get-flag march` == "i686"
> +   local var pattern="${1}"

drop the braces for builtin vars

>     for var in $(all-flag-vars) ; do
> -       for f in ${!var} ; do
> -           if [ "${f/${findflag}}" != "${f}" ] ; then
> -               printf "%s\n" "${f/-${findflag}=}"
> +       local i flags=( ${!var} )
> +       for (( i=${#flags[@]}-1; i>=0; i-- )) ; do

omitting spaces doesn't make code faster, it just makes it unreadable.
        for (( i = ${#flags[@]} - 1; i >= 0; --i )) ; do

stick a comment above this for loop explaining why we walk backwards.

> +           local needle="-${pattern#-}" # force dash on

avoid inline comments and make them complete sentences.
        # Make sure we anchor to the leading dash.
        local needle="-${pattern#-}"

> +           local haystack="${flags[i]%%=*}" # we're comparing flags, not 
> values

it's a bit easier to read if you unpack the flag explicitly.
        local flag=${flags[i]}

> +           if [[ ${haystack##${needle}} == '' ]] ; then

use a -z test instead of comparing to ''
-mike

Attachment: signature.asc
Description: Digital signature

Reply via email to