# Command Line Arguments problem

**URL:** <https://www.ruby-forum.com/t/command-line-arguments-problem/188498>\
**Category:** Ruby\
**Created:** [May 12, 2010, 11:16pm UTC](https://www.ruby-forum.com/t/command-line-arguments-problem/188498 "2010-05-12T23:16:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@dlucci](https://www.ruby-forum.com/u/dlucci)\
**Post date:** [May 12, 2010, 11:16pm UTC](https://www.ruby-forum.com/t/command-line-arguments-problem/188498/1 "2010-05-12T23:16:15Z")

</div>

Greetings,  
I am working on a project and I added command line options. Today, I  
took the fateful step in adding the capability to do multiple options in  
the program. For some reason, the code, which worked perfectly before  
this, now spews out the error:  
undefined method “-” for nil:NilClass (NoMethodError)  
The code in question is:  
dateArr[0] = DateTime.parse(ARGV[1])  
Now, this error pops up because ARGV[1] is nil. But if I run the  
program :  
ruby Hercules.rb -c 2010-04-15  
ARGV[1] should be 2010-04-15. What is the problem here?

Thank you,  
dlucci

---

<div class="post-metadata">

**Author:** ![dlucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@dlucci](https://www.ruby-forum.com/u/dlucci)\
**Post date:** [May 12, 2010, 11:25pm UTC](https://www.ruby-forum.com/t/command-line-arguments-problem/188498/2 "2010-05-12T23:25:03Z")

</div>

On Wed, May 12, 2010 at 11:16 PM, Derril L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ruby Hercules.rb -c 2010-04-15  
> ARGV[1] should be 2010-04-15. What is the problem here?

It must be something else, cause this works for me:

require ‘date’

puts DateTime.parse(ARGV[1])

and calling this as

ruby test.rb -c 2010-04-15

prints the date.

Can you show the full code that errors?

Jesus.

---

<div class="post-metadata">

**Author:** ![dlucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@dlucci](https://www.ruby-forum.com/u/dlucci)\
**Post date:** [May 12, 2010, 11:26pm UTC](https://www.ruby-forum.com/t/command-line-arguments-problem/188498/3 "2010-05-12T23:26:14Z")

</div>

Hi Derril,

When working with command-line options, you’ll have a much better time  
working with OptionParser – it’s part of the Ruby standard library:

[http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html)

Mat

---

<div class="post-metadata">

**Author:** ![dlucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@dlucci](https://www.ruby-forum.com/u/dlucci)\
**Post date:** [May 12, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/command-line-arguments-problem/188498/4 "2010-05-12T23:32:30Z")

</div>

Certainly.

opts is an array of Getoptlong type and holds the options and their  
requirements.

opts.each do |opt, arg|  
case opt  
when “–date”, “-d”  
dateArr[0] = DateTime.parse(ARGV[1])  
.  
.  
.

That is more of the code in question. Hope if helps.

Cheers,  
dlucci

---

<div class="post-metadata">

**Author:** ![dlucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@dlucci](https://www.ruby-forum.com/u/dlucci)\
**Post date:** [May 13, 2010, 12:40pm UTC](https://www.ruby-forum.com/t/command-line-arguments-problem/188498/5 "2010-05-13T12:40:55Z")

</div>

Derril L. wrote:

> That is more of the code in question. Hope if helps.

No, but it will help if you can provide a complete, standalone program  
which demonstrates the problem. Either write a program from scratch just  
to demonstrate the problem, or keep trimming out code from your existing  
program until you can no longer demonstrate it.

---

<div class="post-metadata">

**Author:** ![dlucci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@dlucci](https://www.ruby-forum.com/u/dlucci)\
**Post date:** [May 14, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/command-line-arguments-problem/188498/6 "2010-05-14T20:48:23Z")

</div>

Figured it out. Turns out, I was checking if ARGV’s size was 0, then  
trying it. Clearly, if ARGV was 0, there are no options or arguments.  
Thus, dateArr[0] is nil and cannot be used for equations. Thanks for  
all your help!

Cheers,  
dlucci
