Forum: Ruby odd number list for hash error and command line parsers in g

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Talha O. (Guest)
on 2006-04-02 17:09
(Received via mailing list)

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
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
this schema. I do not understand why getoptlong package does not provide
convinent hash the options as getopt package provides. I do not see what
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 =
                  [ "--indir",  "-i",   GetoptLong::REQUIRED_ARGUMENT ],
                  [ "--outdir", "-o",   GetoptLong::REQUIRED_ARGUMENT ],
                  [ "--help",   "-h",   GetoptLong::NO_ARGUMENT ],
                  [ "--version","-v",   GetoptLong::NO_ARGUMENT ]

  opts.each_option do |name, arg|
    eval "$OPT_#{name.sub(/^-+/, {''}).gsub!(/-/, {'_'}).upcase!} =
ts (Guest)
on 2006-04-02 17:18
(Received via mailing list)
>>>>> "T" == Talha O. <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")'

 Same for #upcase!

moulon% ruby -e 'p "1".upcase!'

Guy Decoux
This topic is locked and can not be replied to.