use_echo() as a universal '?:' operator-like function
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. -- Best regards, Michał Górny <http://mgorny.alt.pl> <xmpp:mgorny@jabber.ru> |
use_echo() as a universal '?:' operator-like function
On 9/8/10 12:03 PM, Michał Górny wrote:
> 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) I'm not sure about the name use_echo, but some form of ternary operator other than "&& ||" (which is very hard to read for me and just error prone) would be very useful. Paweł |
use_echo() as a universal '?:' operator-like function
-----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 8tIlTXagN1AAlQcVSWtVbxb6iMDDX6Lc0HWpOk2vlXHqZDW3tr BV8K0kqmnxoQxl IRy9DjTXDXFo2eKGxrH/vE93lksHTbW8bBIva+LuXaoPnOEUCEG6QAO9537Gd5Gi 8xWd0fsj3wDq0bQ/nx/p3Ak1FjdZOhR+KdCsL4fZVrP7U6+lrEru8EgztQ6x6/2g U4stSwtuARSH7X8wY9cdxLfbFJJ3PL1a/8uF7NSPCzKv2cNWbXxnMIHw4yD3WEjC wKEg6U4SYwMmhu3b+PdI6iA+sVnxbN+qGfGG7g8TjHBiebYv8A ttU+6STyKC5TWK I8FK5pC6u90zRJsiFfHiA6LSfy6CxQ3AOD9xoO+dcTOVmW8k5l gik0w5D7LQs5+a 5YbkXEjlC8V7WnqXQpUr/eynI3iTcTCHfMrr2OebJs4qUy+qMwcsH5OLcI75F8hH FWGjd65EqAmEEiVusfsdfYCv+TX272cSI7aAKq1N2nM0iPD82I uKACLBcuKfECI0 DqPD7PHCkUROrXXc1HsZAcyuJqguoZzT8heYfHnElmH1zRya/QZ6qyylg0lPUiaM j6L8P2Wq1BOoJj6R7a64 =ANyl -----END PGP SIGNATURE----- |
use_echo() as a universal '?:' operator-like function
On 9/11/10 11:03 AM, Jonathan Callen wrote:
> 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" > } Looks good to me. If it doesn't get included in any eclass, I will just paste it to the chromium ebuilds. :) Paweł |
use_echo() as a universal '?:' operator-like function
2010/9/11 "Paweł Hajdan, Jr." <phajdan.jr@gentoo.org>
On 9/11/10 11:03 AM, Jonathan Callen wrote: > 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" > } Looks good to me. If it doesn't get included in any eclass, I will just paste it to the chromium ebuilds. :) PaweÅ‚ I don't count but sometimes I do still read ebuilds, the function proposed look a bit^W unreadable to me,Â*may I suggest to aggregate use and echo, separating them with a comma ",". The first element with an empty "use" always echo and return. See implementation and example below use_case() {Â*Â* Â* Â* Â*local uÂ*Â* Â* Â* Â*local c Â*Â* Â* Â* Â*while [[ $# -gt 0 ]]Â*Â* Â* Â* Â*doÂ*Â* Â* Â* Â* Â* Â* Â* Â*u=${1%%,*}Â*Â* Â* Â* Â* Â* Â* Â* Â*c=${1#*,}Â*Â* Â* Â* Â* Â* Â* Â* Â*if [[ ${u} == "" ]] || use $uÂ*Â* Â* Â* Â* Â* Â* Â* Â*then Â*Â* Â*Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â*echo ${c}Â*Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â* Â*breakÂ*Â* Â* Â* Â* Â* Â* Â* Â*fiÂ*Â* Â* Â* Â* Â* Â* Â* Â*shiftÂ*Â* Â* Â* Â*done} echo $(use_case useA,echoA useB,echoB ,echoC) |
use_echo() as a universal '?:' operator-like function
On Sat, 11 Sep 2010 20:26:17 +0200
Francesco R <vivo75@gmail.com> wrote: > echo $(use_case useA,echoA useB,echoB ,echoC) I would personally rather use: echo $(use_case useA,echoA useB,echoB echoC) but AFAICS your implementation should support both. PS I suggest using [[ -z ${u} ]] instead. -- Best regards, Michał Górny <http://mgorny.alt.pl> <xmpp:mgorny@jabber.ru> |
use_echo() as a universal '?:' operator-like function
On 09/11/2010 09:44 PM, Michał Górny wrote:
> On Sat, 11 Sep 2010 20:26:17 +0200 > Francesco R <vivo75@gmail.com> wrote: > >> echo $(use_case useA,echoA useB,echoB ,echoC) > > I would personally rather use: > echo $(use_case useA,echoA useB,echoB echoC) > > but AFAICS your implementation should support both. > > PS I suggest using [[ -z ${u} ]] instead. > How often do we need multiple arguments like that? We could just as well have multiple calls like with use_enable. I think we should keep consistency with use_enable (and it would also result in a simpler implementation). Regards, Petteri |
use_echo() as a universal '?:' operator-like function
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256 On 09/11/2010 02:03 PM, Jonathan Callen wrote: > On 09/08/2010 03:03 PM, MichaB Górny wrote: >> If called with a single arg, it would assume val1=use1. > > Just as a proof-of-concept, here's one implementation of such a > function, allowing for an arbitrary number of arguments: > Actually, I forgot to include the single-argument version (which really is the same as usev(), so it's not as useful as you might think): > use_echo() { if [[ $# -eq 1 ]]; then usev "$1" return fi > while [[ $# -gt 1 ]]; do > if use "$1"; then > echo "$2" > return > fi > shift 2 > done > [[ $# -eq 1 ]] && echo "$1" > } Consider this a vote in favor of `use_echo use1 val1 use2 val2 other` instead of `use_echo use1,val1 use2,val2 other` (I think this is more readable, myself). - -- Jonathan Callen -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.16 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQIcBAEBCAAGBQJMjD12AAoJELHSF2kinlg40EQP/Rd9ks2JU9HjtDc7Vgt3fZWg yN08UyhBhYxy4zS7Lijo+J33GyMBB/Qn5iEj5jkpgbol8BmETnC8aKTqc6LDOrnb tahVVsuZNIoX+l8dF53op+qYXwGKDFNqobU02+Yj9lpju7Mtpw WhQhHDjg7FtgTz RCc2F712Lw4ncMCT54wlUnJFPX9Xy0WuCMaxUzVG6K9tB5+hyW R1l3oP0kfYkmY+ 1kDD6uYt/MTH7c3koe9rIM0O1KGdOUJ+tszgmIKUhtlMNOHHIejTCPcZSdQ assR7 Ff2Lh+WKe0OqNcAEKaFFlXjMBNz/g/uGZbqGWh39kX21oH+G+QOoYaStYuO9nnHV NgpKRNL4eEr/UCvTt7DV52oJm9b6tK/6Z/J10ihhqf3vNBWeN5OMwguLW0iTu0/v /ja16VpdkdFQ6d/kbeQCPWvx4WW+gWuo/QcbhL9drC2VsBV+gJcrQhzECSdxAHeG CmJGrkAH8r6rBuNhzvkGMnNVsf+vwcB+1qGMiXltmboSkv56Ac WhTOmeZ9tzfCv4 FUDIX4bm7v51xqBfqb/SNLrOgz+Deo3GOtOk2HWHbGfYHsohBPHva4WLAOHwIFcO J4Fb5tNWf74DVKKJGmU5RctWGh5sDgM2mEZmDkTHEzv2R1/0Hp++jhZ5htYXpCRk Ni95j32etVEKNK1a6FvH =2cVl -----END PGP SIGNATURE----- |
| All times are GMT. The time now is 07:44 AM. |
VBulletin, Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.