FunOnRails

A Ruby and Rails Blog

Friday, November 27, 2009

Passing commandline parameter (arguments) to ruby file using optparser

by Sandip Ransing 0 comments



Share this post:
Design Float
StumbleUpon
Facebook

Ruby file accepts from command prompt in the form of array.
Passing parameters
ruby input.rb TOI DH TimesNew
Accessing parameters
 # input.rb
 p ARGV
 # => ["TOI", "DH", "TimesNew"]
 p ARGV[0]
 # => "TOI"
 p ARGV[1]
 # => "DH"
Optparser : parses commandline options in more effective way using OptParser class. and we can access options as hashed parameters.

Passing parameters
ruby input.rb -P"The Times of India" --category"article"
Accessing parameters
 
 # input.rb
 require 'optparser'
 
 options = ARGV.getopts("P:", 'category')

 p options
 # => { 'P' => 'The Times of India', :category => 'article' }

 p options['P']
 # => "The Times of India"

blog comments powered by Disqus

Sandip Ransing
Sandip Ransing is ruby on rails developer based in Pune, India and works at Josh Software Pvt. Ltd .
san2821@gmail.com

Subscribe feeds via e-mail
Subscribe in your preferred RSS reader

Advertise on this site Sponsored links

Categories

Which do you like most ?

Blog Archive