# Odd number list for hash error and command line parsers in g

**URL:** <https://www.ruby-forum.com/t/odd-number-list-for-hash-error-and-command-line-parsers-in-g/56668>\
**Category:** Ruby\
**Created:** [April 2, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/odd-number-list-for-hash-error-and-command-line-parsers-in-g/56668 "2006-04-02T15:09:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [April 2, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/odd-number-list-for-hash-error-and-command-line-parsers-in-g/56668/1 "2006-04-02T15:09:13Z")

</div>

Hi,

I am trying to use getoptlong package. From the documentation I have  
borrowed the following code fragment with a little modification.

I receive “odd number list for hash” error at eval statement between  
somewhere sub and gsub. I could not understand the reason. The code  
tries to  
generate the $OPT\_HELP, $OPT\_INDIR etc. variables.

Any help will be much appreciated.

I really hate to be forced to iterate over the options to get the values  
of  
options in Getoptlong package. Receiving options in hash and making  
necessary checks as getopt package provides is more convinient for me.  
According to me, It is easier to check mutual exclusions, dependencies  
in  
this schema. I do not understand why getoptlong package does not provide  
a  
convinent hash the options as getopt package provides. I do not see what  
is  
improved over Getopt other than longer options.

I have played getopt-declare package about a day. Theory is good but  
implementation I believe lacks certain features that I find irritating.  
I am  
still in search of good user friendly command line / option parser. I  
would like to learn what you prefer.

opts = GetoptLong.new()  
opts.set\_options(  
[“–indir”, “-i”, GetoptLong::REQUIRED\_ARGUMENT],  
[“–outdir”, “-o”, GetoptLong::REQUIRED\_ARGUMENT],  
[“–help”, “-h”, GetoptLong::NO\_ARGUMENT],  
[“–version”,"-v", GetoptLong::NO\_ARGUMENT]  
)

begin  
opts.each\_option do |name, arg|  
eval “$OPT\_#{name.sub(/^-+/, {’’}).gsub!(/-/, {’\_’}).upcase!} =  
‘#{arg}’”  
end  
rescue  
exit(1)  
end

---

<div class="post-metadata">

**Author:** ![Talha\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c2a13f/32.png) [@Talha\_O](https://www.ruby-forum.com/u/Talha_O)\
**Post date:** [April 2, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/odd-number-list-for-hash-error-and-command-line-parsers-in-g/56668/2 "2006-04-02T15:18:31Z")

</div>

> > > > > “T” == Talha O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

T\> eval "$OPT\_#{name.sub(/^-+/, {‘’}).gsub!(/-/, {‘\_’}).upcase!} =3D

In your eval you have

.sub(/^-+/, {‘’})  
^^^^  
.gsub!(/-/, {‘\_’})  
^^^^^

ruby think that it’s an hash with only one element : remove {}

Now you use #gsub! which can return nil if the substitution fail

moulon% ruby -e ‘p “a”.gsub!(/b/, “b”)’  
nil  
moulon%

Same for #upcase!

moulon% ruby -e ‘p “1”.upcase!’  
nil  
moulon%

Guy Decoux
