From: Eric Mahurin Date: 2005-09-14T01:53:00+09:00 Subject: Re: yet another simple command-line option parser Thanks for the input Jim. Comments below. I'm also putting this in the RCR comments. --- Jim Freeze wrote: > That's pretty interesting Eric, to grab the type off the > default. > I think I'll add that to CommandLine::OptionParser. > > However, I'm still not sure if I like the #from_s form, > but I can see the utility of it. For the common cases, > I can use a simple case statement: > > case default > when Float then Float(arg) > when Fixnum then Integer(arg) > end > > But, as you can see with even these simple cases, there > are big issues and big questions to answer. > 1. Fixnum does not match Integer No problem. Fixnum inherits from Integer. Do you care whether Fixnum.from_s returns a Fixnum or a Bignum? It could return either just like many of the other Fixnum instance methods. > 2. Do we use to_i or Integer(#) - Integer raises and to_i > does not > 3. Do we use Float or to_f - Float raises and to_f does not Good point. This RCR should to specify this. I would think it best if an exception occur if the full string doesn't parse the the target type. I'll change the implementation to use the methods that raise exceptions. > Then, there are the tougher cases like > > require 'parsedate' > case default > when Time then Time.gm(*ParseDate.parsedate(arg)) I haven't dealt with dates and times to know what all the options are. I threw this in at the last minute. If you don't like the klass.from_s method, you could define your own derived class (or override that klass.from_s): require 'parsedate' class MyTime < Time def self.from_s(s) gm(*ParseDate.parsedate(s)) end end and then make the default be a MyTime instead of a Time. > when Fixnum arg.to_i # what if yield bignum answered above > end > > where we have to use a help class and helper method (not new) > to get > the object we want. Or if the conversion method > returns something other than what we requested, like > Bignum instead of Fixnum. > > Sadly, the #from_s RCR doesn't seem to address any of these > issues. > > > > ARGV.replace(%w( > > -n 4 > > -multiplier 3.14 > > -q > > -title foobar > > -pattern fo+ > > -time 5:55PM > > -method downcase > > a b c > > )) > > # option => default (or false for a flag) > > argv_options( > > :n => 1, > > :multiplier => 1.0, > > :q => false, > > :title => "hello world!", > > :pattern => /.*/, > > :time => Time.new, > > :method => :to_s > > ) > > -- > Jim Freeze > > ______________________________________________________ Yahoo! for Good Donate to the Hurricane Katrina relief effort. http://store.yahoo.com/redcross-donate3/