Gentoo Archives: gentoo-dev

From: Jonathan Callen <abcd@g.o>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] Re: use_echo() as a universal '?:' operator-like function
Date: Sat, 11 Sep 2010 18:03:29
Message-Id: 4C8BC45C.2070603@gentoo.org
In Reply to: [gentoo-dev] use_echo() as a universal '?:' operator-like function by "Michał Górny"
1 -----BEGIN PGP SIGNED MESSAGE-----
2 Hash: SHA256
3
4 On 09/08/2010 03:03 PM, Michał Górny wrote:
5 > Hello,
6 >
7 > We already have a variety of use_*() functions with their specific
8 > uses. But what I miss is a single, simple and universal use_echo()
9 > function, behaving similarly to ?: operator in C. In other words, a
10 > simple function replacing monsters like:
11 > $(use foo && echo bar || echo baz)
12 >
13 > Such a function could be implemented inside eutils eclass. The synopsis
14 > could be:
15 > use_echo use1 [val1 [use2 val2 [...]] [otherwise]]
16 >
17 > The idea is to echo 'valN' if 'useN' flag applies, or 'otherwise' if
18 > none of them do. If called with a single arg, it would assume
19 > val1=use1. If called with an even number of args (i.e. no 'otherwise'),
20 > it would not output anything if none of the flags match.
21 >
22 > For example, the code recalled above could be then rewritten as:
23 > $(use_echo foo bar baz)
24 >
25 > The multi-argument version could be used with one-of cases like:
26 > GUI=$(use_echo gtk GTK2 qt4 QT4 NONE)
27 > (for games-emulation/mupen64plus).
28 >
29 > The resulting function would certainly find use in packages using plain
30 > make. For an example, in net-misc/autoupnp (sunrise overlay) I use:
31 > WANT_LIBNOTIFY=$(use libnotify && echo true || echo false)
32 > That would be much shorter as:
33 > WANT_LIBNOTIFY=$(use_echo libnotify true false)
34 >
35 > The function could be used to reimplement some of use_*() functions
36 > within other eclasses too.
37 >
38
39 Just as a proof-of-concept, here's one implementation of such a
40 function, allowing for an arbitrary number of arguments:
41
42 use_echo() {
43 while [[ $# -gt 1 ]]; do
44 if use "$1"; then
45 echo "$2"
46 return
47 fi
48 shift 2
49 done
50 [[ $# -eq 1 ]] && echo "$1"
51 }
52
53 - --
54 Jonathan Callen
55 -----BEGIN PGP SIGNATURE-----
56 Version: GnuPG v2.0.16 (GNU/Linux)
57 Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
58
59 iQIcBAEBCAAGBQJMi8RcAAoJELHSF2kinlg4z6gP/12fOfcJYJK1CUhe/DClXj99
60 sujt9ekdiiPKepa3g1JC2X4T/IObdLnKgtpGhsFpj4nuJkYYA4WBJUEjdeZO3evx
61 8tIlTXagN1AAlQcVSWtVbxb6iMDDX6Lc0HWpOk2vlXHqZDW3trBV8K0kqmnxoQxl
62 IRy9DjTXDXFo2eKGxrH/vE93lksHTbW8bBIva+LuXaoPnOEUCEG6QAO9537Gd5Gi
63 8xWd0fsj3wDq0bQ/nx/p3Ak1FjdZOhR+KdCsL4fZVrP7U6+lrEru8EgztQ6x6/2g
64 U4stSwtuARSH7X8wY9cdxLfbFJJ3PL1a/8uF7NSPCzKv2cNWbXxnMIHw4yD3WEjC
65 wKEg6U4SYwMmhu3b+PdI6iA+sVnxbN+qGfGG7g8TjHBiebYv8AttU+6STyKC5TWK
66 I8FK5pC6u90zRJsiFfHiA6LSfy6CxQ3AOD9xoO+dcTOVmW8k5lgik0w5D7LQs5+a
67 5YbkXEjlC8V7WnqXQpUr/eynI3iTcTCHfMrr2OebJs4qUy+qMwcsH5OLcI75F8hH
68 FWGjd65EqAmEEiVusfsdfYCv+TX272cSI7aAKq1N2nM0iPD82IuKACLBcuKfECI0
69 DqPD7PHCkUROrXXc1HsZAcyuJqguoZzT8heYfHnElmH1zRya/QZ6qyylg0lPUiaM
70 j6L8P2Wq1BOoJj6R7a64
71 =ANyl
72 -----END PGP SIGNATURE-----

Replies