Subscribe Favorite

Optiflag: The Magic Ruby Command-Line DSL Parser

Written on December 3, 2007 by Eka Riyanti

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.

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.

If you enjoyed this post Subscribe to our feed

2 Comments on “Optiflag: The Magic Ruby Command-Line DSL Parser”

  1. » The Links » roarin’ reporter |

    […] Optiflag: The Magic Ruby Command-Line DSL Parser […]

  2. PlugIM.com |

    Optiflag: The Magic Ruby Command-Line DSL Parser…

    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….

Leave a Reply