RSS
3 Dec 2007

Optiflag: The Magic Ruby Command-Line DSL Parser

Author: ceefour | Filed under: HTML, Reviews, Ruby, Tips, Tools

Need to parse command line arguments for your Ruby program? OptiFlag comes to the rescue!

This image is shows a typical OptiFlag scenario. You will note that there is a natural indentation.

terminology Optiflag: The Magic Ruby Command Line DSL Parser

It’s really an easy way for getting command line in options into your program.

Rather than use the ‘module’ DSL syntax, we just re-use an existing class. Any method accessor will be used verbatim as a switch. OptiFlag will crawl up the inheritance hierarchy up to (but not including) object and use all accessors as standard ‘flag’s.

Let’s see the Example:

require 'optiflag'

# Title: SUPER EASY alternative method for getting command line options into your program.
# Description:  Rather than use the 'module' DSL syntax, we just re-use an existing class.  Any method accessor will be used verbatim as a switch.  OptiFlag will crawl up the inheritance hierarchy up to (but not including) object and use all accessors as standard 'flag's.
class Person
  attr_accessor  :name,:ssn
end
class Employee < Person
  attr_accessor  :username,:password
end

daniel = Employee.new

OptiFlag.using_object(daniel)

puts <<-EOF
     Name: #{ daniel.name}
 Password: #{ daniel.password}
 Username: #{ daniel.username}
      SSN: #{ daniel.ssn}
EOF

#h# ruby example_8.rb
#h# ruby example_8.rb -username doeklund -password AHA -ssn 1234562342 -name "Daniel Eklund"

For further details visit Optiflag Rubyforge project page.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

blog comments powered by Disqus