# Argc \*argv\[\] in Ruby

**URL:** <https://rubytalk.org/t/argc-argv-in-ruby/21453>\
**Category:** ruby-talk\
**Created:** [10 October 2005 22:26 UTC](https://rubytalk.org/t/argc-argv-in-ruby/21453 "2005-10-10T22:26:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaW](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@RaW](https://rubytalk.org/u/RaW)\
**Post date:** [10 October 2005 22:26 UTC](https://rubytalk.org/t/argc-argv-in-ruby/21453/1 "2005-10-10T22:26:52Z")

</div>

Hello,  
Does Ruby have something like C has argc and \*argv[]?  
I need to run ruby script with a command line parameters.

Thanks in advance for any helpfull answer.

> **···**
>
> --  
> RaW

---

<div class="post-metadata">

**Author:** ![Chris\_Eskow](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@Chris\_Eskow](https://rubytalk.org/u/Chris_Eskow)\
**Post date:** [10 October 2005 22:30 UTC](https://rubytalk.org/t/argc-argv-in-ruby/21453/2 "2005-10-10T22:30:57Z")

</div>

ARGV holds an array of command line parameters. There's no argc, but you can  
just use ARVG.length.

---

<div class="post-metadata">

**Author:** ![Chris\_Eskow](https://avatars.discourse-cdn.com/v4/letter/c/e0b2c6/32.png) [@Chris\_Eskow](https://rubytalk.org/u/Chris_Eskow)\
**Post date:** [10 October 2005 22:34 UTC](https://rubytalk.org/t/argc-argv-in-ruby/21453/3 "2005-10-10T22:34:43Z")

</div>

Oh, one more thing: Unlike argv in C/C++, in Ruby ARVG[0] will contain the  
first argument passed in to the program, not the program name. If you want  
the name of script, use the global variable $0.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [11 October 2005 07:31 UTC](https://rubytalk.org/t/argc-argv-in-ruby/21453/4 "2005-10-11T07:31:52Z")

</div>

Chris Eskow wrote:

> Oh, one more thing: Unlike argv in C/C++, in Ruby ARVG[0] will  
> contain the first argument passed in to the program, not the program  
> name. If you want the name of script, use the global variable $0.

Adding to that there's also ARGF which is a single IO like object that  
reads from all command line named files:

# poor man's cat  
# this works even without any arguments by  
# reading from STDIN  
ARGF.each\_line {|line| puts line}

And while we're at it, there are also two libs for option processing:

[http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html)  
[http://www.ruby-doc.org/stdlib/libdoc/getoptlong/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/getoptlong/rdoc/index.html)

I recommend OptionParser as it seems more flexible and also has very nice  
help output generation.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert
