From: Robert Klemme Date: 2013-04-27T17:41:53+09:00 Subject: Re: Is there an elegant way to turn optparser.on() into a one-liner? --14dae94ede378c932d04db539f76 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Apr 27, 2013 at 6:40 AM, larry fast wrote: > I'm looking at my code for optparser and it's ugly. For 90% of my > options all I really want is a one-liner but the standard form for > opts.on() is... > opts.on( '-f', '--file FILE', 'output file' ) do |file| > @params.file = file > end > > Is there any way to clean that down to a one-liner like: > @params.file = opts.on_str( '-f', '--file FILE', 'output file' > ).value > That cannot work because #on_str is executed at _option definition time_ while the block is executed at _option parse time_. I've tried adding a new method to optparse, but I can't figure out how > to pass the list-of-args in. > To me it seems you haven't fully understood how OptionParser works. OptionParser.new creates a parser definition which is executed during one of the methods of the #parse family. Btw. that's the same for other command line option parsing libs - and it cannot be differently because you need the complete option spec before you can start parsing a command line. What's the big deal about having option definitions on multiple lines? And why don't you just use {} instead of "do .. end"? opts.on('-f', '--file FILE', 'output file') {|f| @params.file = f} Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --14dae94ede378c932d04db539f76 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Sat, Apr 27, 2013 at 6:40 AM, larry fast <<= a href=3D"mailto:lists@ruby-forum.com" target=3D"_blank">lists@ruby-forum.c= om> wrote:
I'm looking at my code for optparser and it's ugly= . =A0For 90% of my
options all I really want is a one-liner but the standard form for
opts.on() is...
=A0 =A0 =A0 opts.on( '-f', '--file FILE', 'output file&= #39; ) do |file|
=A0 =A0 =A0 =A0 @params.file =3D file
=A0 =A0 =A0 end

Is there any way to clean that down to a one-liner like:
=A0 =A0@params.file =3D opts.on_str( '-f', '--file FILE', &= #39;output file'
).value

That cannot work because = #on_str is executed at _option definition time_ while the block is executed= at _option parse time_.

I've tried adding a new method to optparse, but I can't figure out = how
to pass the list-of-args in.

To me it seems you haven't fully understood= how OptionParser works. =A0OptionParser.new creates a parser definition wh= ich is executed during one of the methods of the #parse family. =A0Btw. tha= t's the same for other command line option parsing libs - and it cannot= be differently because you need the complete option spec before you can st= art parsing a command line.

What's the big deal about ha= ving option definitions on multiple lines? =A0And why don't you just us= e {} instead of "do .. end"?

=
opts.on('-f', '--file FILE', = 9;output file') {|f| @params.file =3D f}

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.= com/
--14dae94ede378c932d04db539f76--