DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Command Line Options Parsing In Ruby (example Snippet)
This is an example of how to use the command line options parser for shell scripts in ruby. It was ripped directly from a project in alpha development but should be readable enough to provide illumination to the uninitiated (and to cut/paste for me if I don't have my VoodooPad handy)
require 'optparse'
class GetOptions
attr_accessor :tiny, :javascripts, :stylesheets, :strip_javascripts, :verbose,
:hard_link_stuff, :migration, :svn_ignore
def initialize()
# Defaults
@tiny = true
@javascripts = true
@stylesheets = true
@strip_javascripts = true
@verbose = false
@hard_link_stuff = false
@migration = true
@svn_ignore = true
opts = OptionParser.new do |o|
o.set_summary_indent(' ')
o.banner = "Usage: #{File.basename($0)} [OPTIONS]"
o.define_head "Installs or updates the plugin installation"
o.separator ""
o.on("-j", "--no-javascripts",
"Do not install javascripts") do
@javascripts = false
end
o.on("-t", "--no-tiny-mce",
"Do not install TinyMCE") do
@tiny = false
end
o.on("-s", "--no-stylesheets",
"Do not install stylesheets") do
@tiny = false
end
o.on("-i", "--no-svn-ignore",
"Do not ignore files with subverson") do
@svn_ignore = false
end
o.on("-p", "--no-stripping",
"Do not strip javascripts") do
@strip_javascripts = false
end
o.on("-k", "--make-links",
"Creates hard links instead of copying javascripts and stylesheets") do
@hard_link_stuff = true
end
o.on("-m", "--no-migration",
"Do not make the migration even if one is needed") do
@migration = false
end
o.on("-v", "--verbose",
"Be verbose") do
@verbose = true
end
o.on_tail("-h", "--help", "Show this message") do
STDERR.puts o
exit!(255)
end
o.separator ""
end
opts.parse!(ARGV)
end
end





