L
LDC - Jairo Eduardo Lopez
Hello all,
I'm currently writing a small script needed for my lab in which I need to
parse a few command line arguments in a UNIX like way.
My code goes something like this:
#####
#!/usr/bin/ruby -w
require 'optparse'
require 'ostruct'
opciones = OpenStruct.new
opts = OptionParser.new do |opts|
opts.on("-u USER", "--uid USER", "UID del usuario.") do |usr|
opciones.user = usr
end
opts.on("-h HOST", "--host HOST", String, "Especifica el servidor.
Puede ser IP o DNS.") do |h|
opciones.host = h
end
opts.on("-p PORT", "--port PORT", Integer, "Especifica el puerto del
servidor. Debe ser entero.") do |p|
opciones.port = p
end
end
begin
opts.parse!(ARGV)
rescue Exception => e
puts e, opts
exit!
end
#####
When I run the script I run into the problem that if I use various flags,
and forget to put in the required arguments, some flags are taken as
arguments. I've read that if I use the coding I've used all three
flags, the argument should be obligatory.
To better explain, I'll give some examples:
-----
/script.rb -h
missing argument: -h
-----
Works fine.
-----
/script.rb -p
missing argument: -p
-----
Works fine.
-----
/script.rb -h -p
-----
The script accepts -p as the argument for -h, which shouldn't happen as -p
is actually a flag.
I'm not sure what the problem is so I need help.
I'm running on a Debian Etch Box i686 with a Ruby 1.8.5 version.
Any help would be greatly appreciated. Thank you all.
I'm currently writing a small script needed for my lab in which I need to
parse a few command line arguments in a UNIX like way.
My code goes something like this:
#####
#!/usr/bin/ruby -w
require 'optparse'
require 'ostruct'
opciones = OpenStruct.new
opts = OptionParser.new do |opts|
opts.on("-u USER", "--uid USER", "UID del usuario.") do |usr|
opciones.user = usr
end
opts.on("-h HOST", "--host HOST", String, "Especifica el servidor.
Puede ser IP o DNS.") do |h|
opciones.host = h
end
opts.on("-p PORT", "--port PORT", Integer, "Especifica el puerto del
servidor. Debe ser entero.") do |p|
opciones.port = p
end
end
begin
opts.parse!(ARGV)
rescue Exception => e
puts e, opts
exit!
end
#####
When I run the script I run into the problem that if I use various flags,
and forget to put in the required arguments, some flags are taken as
arguments. I've read that if I use the coding I've used all three
flags, the argument should be obligatory.
To better explain, I'll give some examples:
-----
/script.rb -h
missing argument: -h
-----
Works fine.
-----
/script.rb -p
missing argument: -p
-----
Works fine.
-----
/script.rb -h -p
-----
The script accepts -p as the argument for -h, which shouldn't happen as -p
is actually a flag.
I'm not sure what the problem is so I need help.
I'm running on a Debian Etch Box i686 with a Ruby 1.8.5 version.
Any help would be greatly appreciated. Thank you all.