Optiflag: The Magic Ruby Command-Line DSL Parser
Written on December 3, 2007 by ceefour
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

Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks
(Trackback URL)
December 3, 2007 at 10:27 pm
[...] Optiflag: The Magic Ruby Command-Line DSL Parser [...]
December 9, 2007 at 8:52 pm
Optiflag: The Magic Ruby Command-Line DSL Parser... Need to parse command line arguments for your Ruby program? OptiFlag comes to the ...