# Ruby script with flag

**URL:** <https://www.ruby-forum.com/t/ruby-script-with-flag/178315>\
**Category:** Ruby\
**Created:** [November 2, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/ruby-script-with-flag/178315 "2009-11-02T19:21:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jillyh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jillyh0](https://www.ruby-forum.com/u/jillyh0)\
**Post date:** [November 2, 2009, 7:21pm UTC](https://www.ruby-forum.com/t/ruby-script-with-flag/178315/1 "2009-11-02T19:21:25Z")

</div>

Hi,

I am trying to write a ruby script which accepts a flag. That is, I  
would like to execute the script by doing:  
./assignment2.rb --verbose OR  
./assignment2.rb

How would I got about making the script accept a flag?

Thanks,  
Jillian

---

<div class="post-metadata">

**Author:** ![jillyh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jillyh0](https://www.ruby-forum.com/u/jillyh0)\
**Post date:** [November 2, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/ruby-script-with-flag/178315/2 "2009-11-02T21:28:57Z")

</div>

Jillian K. wrote:

> Hi,
> 
> I am trying to write a ruby script which accepts a flag. That is, I  
> would like to execute the script by doing:  
> ./assignment2.rb --verbose OR  
> ./assignment2.rb
> 
> How would I got about making the script accept a flag?
> 
> Thanks,  
> Jillian

Every command-line argument that you pass into a Ruby script is  
available via the ARGV aray. Therefore, if you call a script with  
./assignment2.rb --verbose  
then ARGV[0] is “–verbose”. Because command-line-option-handling (what  
a word!) can be a bit cumbersome, you could use a library like  
OptionParser (in the stdlib as “optparse”).

Marvin

---

<div class="post-metadata">

**Author:** ![jillyh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jillyh0](https://www.ruby-forum.com/u/jillyh0)\
**Post date:** [November 2, 2009, 9:30pm UTC](https://www.ruby-forum.com/t/ruby-script-with-flag/178315/3 "2009-11-02T21:30:02Z")

</div>

Jillian K. wrote:

> Hi,
> 
> I am trying to write a ruby script which accepts a flag. That is, I  
> would like to execute the script by doing:  
> ./assignment2.rb --verbose OR  
> ./assignment2.rb
> 
> How would I got about making the script accept a flag?
> 
> Thanks,  
> Jillian

Command line options are stored in the ARGV array.

./assignment2.rb --verbose  
ARGV[0] # =\> “–verbose”

If no command line options are supplied, ARGV will be empty.  
Also look at `optparse` in the standard library for handling more  
complex command line options.

Rob

---

<div class="post-metadata">

**Author:** ![jillyh0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jillyh0](https://www.ruby-forum.com/u/jillyh0)\
**Post date:** [November 4, 2009, 11:23am UTC](https://www.ruby-forum.com/t/ruby-script-with-flag/178315/4 "2009-11-04T11:23:04Z")

</div>

2009/11/3 Jillian K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am trying to write a ruby script which accepts a flag. That is, I  
> would like to execute the script by doing:  
> ./assignment2.rb --verbose OR  
> ./assignment2.rb
> 
> How would I got about making the script accept a flag?

You would use one of the option parsing libraries around. The  
standard lib comes with OptionParser and GetoptLong. There are also  
other libraries and gems around that do option processing (and  
sometimes more). With OptionParser you could do

require ‘optparse’

$verbose = false

OptionParser.new do |opts|

# simple

opts.on ‘-v’, ‘–verbose’, ‘Print verbose messages’ do  
$verbose = true  
end

# alternative: with negation

opts.on ‘-v’, ‘–[no-]verbose’, ‘Print verbose messages’ do |flag|  
$verbose = flag  
end  
end.parse! ARGV

puts “verbose is on” if $verbose

Note that OptionParser also gives you some default options, for  
example “-h” and “–help”.

Kind regards

robert
