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

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

“T” == Talha O. [email protected] 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