# GetoptLong Accessing the values with out looping

**URL:** <https://www.ruby-forum.com/t/getoptlong-accessing-the-values-with-out-looping/60651>\
**Category:** Ruby\
**Created:** [May 16, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/getoptlong-accessing-the-values-with-out-looping/60651 "2006-05-16T15:23:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 16, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/getoptlong-accessing-the-values-with-out-looping/60651/1 "2006-05-16T15:23:39Z")

</div>

Simple question but for the life of me I can’t figure it out.  
I am converting some of my basic perl scripts that I use daily as an  
excersice in learning ruby.

I need to grab a date from the command line that will eventually be  
converted into a filename.

no I want to call my script like such …  
./myscript.rb -d 2006-08-06

I then want to validate what is being set in -d.  
Here is the code I have that works but it just seems excessive to have  
to  
loop through the options…

def getopts  
opts = GetoptLong.new([ ‘-d’, ‘–date’,  
GetoptLong::REQUIRED\_ARGUMENT])  
opts.each do |opt,arg|  
if opt =~ /-d|–date/ && arg =~ /(\d{4})-(\d{2})-(\d{2})/  
date = arg.gsub(’-’,’’)  
return date  
else  
puts “Date needs to be set to YYYY-MM-DD using -d or --date”  
exit  
end  
end  
end

I thought I would just be able to do…  
if opt[’-d’] =~ || opt[’–date’] =~   
`
end`

Thanks.  
Paul Kraus

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 16, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/getoptlong-accessing-the-values-with-out-looping/60651/2 "2006-05-16T16:07:58Z")

</div>

On Tue, 16 May 2006, Paul D. Kraus wrote:

> I then want to validate what is being set in -d.  
> puts “Date needs to be set to YYYY-MM-DD using -d or --date”  
> exit  
> end  
> end  
> end
> 
> I thought I would just be able to do…  
> if opt[’-d’] =~ || opt[’–date’] =~   
> `
> end`

it’s not that bad is it?

```
 harp:~ > cat a.rb
 require 'getoptlong'
 require 'time'

 def getdate
   gl = GetoptLong.new ['-d', '--date', 

```

GetoptLong::REQUIRED\_ARGUMENT]  
opts = {} and gl.each{|k,v| opts[k.delete(’-’)] = v}

```
   if((date = opts['d']))
     raise 'date needs to be set to YYYY-MM-DD using -d or --date' 

```

unless  
date =~ %r/^ \d{4} - \d{2} - \d{2} $/iox  
return Time.parse(date)  
end  
end

```
 p getdate

 harp:~ > ruby a.rb --date=2006-05-01
 Mon May 01 00:00:00 MDT 2006

```

regards.

-a

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 16, 2006, 4:14pm UTC](https://www.ruby-forum.com/t/getoptlong-accessing-the-values-with-out-looping/60651/3 "2006-05-16T16:14:07Z")

</div>

> ```
> opts = {} and gl.each{|k,v| opts[k.delete('-')] = v}
> 
> ```

Makes perfect sense in this example you are still looping through the  
options.  
All though this is better on the eyes.

I was hoping that the instance of getoptlong had some kind of method to  
get  
the value of one of the command line options.

Paul

---

<div class="post-metadata">

**Author:** ![Paul\_DSKraus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed655f/32.png) [@Paul\_DSKraus](https://www.ruby-forum.com/u/Paul_DSKraus)\
**Post date:** [May 17, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/getoptlong-accessing-the-values-with-out-looping/60651/4 "2006-05-17T17:06:21Z")

</div>

On May 16, 2006, at 8:22 AM, Paul D. Kraus wrote:

> no I want to call my script like such …  
> ./myscript.rb -d 2006-08-06

Have you tried CommandLine?

> if opt =~ /-d|–date/ && arg =~ /(\d{4})-(\d{2})-(\d{2})/  
> if opt[’-d’] =~ || opt[’–date’] =~   
> `
> end`
> 
> Thanks.  
> Paul Kraus

Jim F.
