E
Eric Mahurin
I just put in a good example for:
http://rcrchive.net/rcr/show/317
It is a simple option parser that has option defaults and
converts the options to the right type:
# these klass.from_s methods are the meat of the RCR
def Float.from_s(s);s.to_f;end
def Integer.from_s(s);s.to_i;end
def Symbol.from_s(s);s.to_sym;end
def String.from_s(s);s.to_s;end
def Regexp.from_s(s,*other);new(s,*other);end
require 'time.rb'
def Time.from_s(s,*other);Time.parse(s,*other);end
def argv_options(options)
i =3D 0
while arg =3D ARGV
if arg[0]=3D=3D?-
arg.slice!(0)
ARGV.slice!(i)
break if arg=3D=3D"-" # -- terminates options
opt =3D arg.to_sym
default =3D options[opt]
if default
klass =3D default.class
# would need a big case statement w/o RCR 317
options[opt] =3D klass.from_s(ARGV.slice!(i))
elsif default.nil?
raise("unknown option -#{opt}")
else # default=3D=3Dfalse
options[opt] =3D true
end
else
i +=3D 1
end
end
options
end
ARGV.replace(%w(
-n 4
-multiplier 3.14
-q
-title foobar
-pattern fo+
-time 5:55PM
-method downcase
a b c
))
# option =3D> default (or false for a flag)
argv_options(
:n =3D> 1,
:multiplier =3D> 1.0,
:q =3D> false,
:title =3D> "hello world!",
attern =3D> /.*/,
:time =3D> Time.new,
:method =3D> :to_s
)
If you have an opinion about the usefulness of this RCR, go
vote and give a comment. I didn't have an example before to
show it in action.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20
http://rcrchive.net/rcr/show/317
It is a simple option parser that has option defaults and
converts the options to the right type:
# these klass.from_s methods are the meat of the RCR
def Float.from_s(s);s.to_f;end
def Integer.from_s(s);s.to_i;end
def Symbol.from_s(s);s.to_sym;end
def String.from_s(s);s.to_s;end
def Regexp.from_s(s,*other);new(s,*other);end
require 'time.rb'
def Time.from_s(s,*other);Time.parse(s,*other);end
def argv_options(options)
i =3D 0
while arg =3D ARGV
if arg[0]=3D=3D?-
arg.slice!(0)
ARGV.slice!(i)
break if arg=3D=3D"-" # -- terminates options
opt =3D arg.to_sym
default =3D options[opt]
if default
klass =3D default.class
# would need a big case statement w/o RCR 317
options[opt] =3D klass.from_s(ARGV.slice!(i))
elsif default.nil?
raise("unknown option -#{opt}")
else # default=3D=3Dfalse
options[opt] =3D true
end
else
i +=3D 1
end
end
options
end
ARGV.replace(%w(
-n 4
-multiplier 3.14
-q
-title foobar
-pattern fo+
-time 5:55PM
-method downcase
a b c
))
# option =3D> default (or false for a flag)
argv_options(
:n =3D> 1,
:multiplier =3D> 1.0,
:q =3D> false,
:title =3D> "hello world!",
attern =3D> /.*/,
:time =3D> Time.new,
:method =3D> :to_s
)
If you have an opinion about the usefulness of this RCR, go
vote and give a comment. I didn't have an example before to
show it in action.
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20