-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 09/08/2010 03:03 PM, Michał Górny wrote:
> Hello,
> 
> We already have a variety of use_*() functions with their specific
> uses. But what I miss is a single, simple and universal use_echo()
> function, behaving similarly to ?: operator in C. In other words, a
> simple function replacing monsters like:
>       $(use foo && echo bar || echo baz)
> 
> Such a function could be implemented inside eutils eclass. The synopsis
> could be:
>       use_echo use1 [val1 [use2 val2 [...]] [otherwise]]
> 
> The idea is to echo 'valN' if 'useN' flag applies, or 'otherwise' if
> none of them do. If called with a single arg, it would assume
> val1=use1. If called with an even number of args (i.e. no 'otherwise'),
> it would not output anything if none of the flags match.
> 
> For example, the code recalled above could be then rewritten as:
>       $(use_echo foo bar baz)
> 
> The multi-argument version could be used with one-of cases like:
>       GUI=$(use_echo gtk GTK2 qt4 QT4 NONE)
> (for games-emulation/mupen64plus).
> 
> The resulting function would certainly find use in packages using plain
> make. For an example, in net-misc/autoupnp (sunrise overlay) I use:
>       WANT_LIBNOTIFY=$(use libnotify && echo true || echo false)
> That would be much shorter as:
>       WANT_LIBNOTIFY=$(use_echo libnotify true false)
> 
> The function could be used to reimplement some of use_*() functions
> within other eclasses too.
> 

Just as a proof-of-concept, here's one implementation of such a
function, allowing for an arbitrary number of arguments:

use_echo() {
        while [[ $# -gt 1 ]]; do
                if use "$1"; then
                        echo "$2"
                        return
                fi
                shift 2
        done
        [[ $# -eq 1 ]] && echo "$1"
}

- -- 
Jonathan Callen
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBCAAGBQJMi8RcAAoJELHSF2kinlg4z6gP/12fOfcJYJK1CUhe/DClXj99
sujt9ekdiiPKepa3g1JC2X4T/IObdLnKgtpGhsFpj4nuJkYYA4WBJUEjdeZO3evx
8tIlTXagN1AAlQcVSWtVbxb6iMDDX6Lc0HWpOk2vlXHqZDW3trBV8K0kqmnxoQxl
IRy9DjTXDXFo2eKGxrH/vE93lksHTbW8bBIva+LuXaoPnOEUCEG6QAO9537Gd5Gi
8xWd0fsj3wDq0bQ/nx/p3Ak1FjdZOhR+KdCsL4fZVrP7U6+lrEru8EgztQ6x6/2g
U4stSwtuARSH7X8wY9cdxLfbFJJ3PL1a/8uF7NSPCzKv2cNWbXxnMIHw4yD3WEjC
wKEg6U4SYwMmhu3b+PdI6iA+sVnxbN+qGfGG7g8TjHBiebYv8AttU+6STyKC5TWK
I8FK5pC6u90zRJsiFfHiA6LSfy6CxQ3AOD9xoO+dcTOVmW8k5lgik0w5D7LQs5+a
5YbkXEjlC8V7WnqXQpUr/eynI3iTcTCHfMrr2OebJs4qUy+qMwcsH5OLcI75F8hH
FWGjd65EqAmEEiVusfsdfYCv+TX272cSI7aAKq1N2nM0iPD82IuKACLBcuKfECI0
DqPD7PHCkUROrXXc1HsZAcyuJqguoZzT8heYfHnElmH1zRya/QZ6qyylg0lPUiaM
j6L8P2Wq1BOoJj6R7a64
=ANyl
-----END PGP SIGNATURE-----

Reply via email to