Is there an elegant way to turn optparser.on() into a one-liner?

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

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

On Fri, Apr 26, 2013 at 11:40 PM, larry fast [email protected]
wrote:

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

Not sure if this will help, but take a look at how methadone wraps up
OptionParser?

On Sat, Apr 27, 2013 at 6:40 AM, larry fast [email protected]
wrote:

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