I’ve been reading the documentation but I cant get it into my thick
head how I tell an option that it is required or optional? I guess all
opts are optional initially, but how do I say that an option is
required.
How do I gracefully tell the user that an option they provided is
non-existent. I dont want ruby to crash with a big bang and a lot of
error output.
At at last I would like to appologies if this are noobish questions. Im
new to ruby and Im struggling to get a hang of it. But I dont read ruby
code flawlessly and I dont always get the examples that the gems provide
me.
I’ve been reading the documentation but I cant get it into my thick
head how I tell an option that it is required or optional? I guess all
opts are optional initially, but how do I say that an option is
required.
I don’t think support for that is built into optparse - you’ll have to
set
flags from the options parsed and then throw an error if a certain flag
isn’t set.
How do I gracefully tell the user that an option they provided is
non-existent. I dont want ruby to crash with a big bang and a lot of
error output.
I don’t think support for that is built into optparse - you’ll have to set
flags from the options parsed and then throw an error if a certain flag
isn’t set.
For this it might be worth to look into CodeForPeople’s main gem (gem
install main). I’ve been using it in a couple of things lately and am
quite happy. You can specify if an option is required or not, and if
it has arguments which are required or not, etc. in a very simple
syntax.
Thanx a lot people for all the helpful answers. I will check this out
asap at work tomorrow.
What Ive seen so far the greatest strength of Ruby is not it’s clean
syntax, its nice OOP features or even the gem repository. Nope, for me
as a newbie its the wonderful community surounding Ruby thats make it
all worth trying to learn it.
I’ve been reading the documentation but I cant get it into my thick
head how I tell an option that it is required or optional? I guess all
opts are optional initially, but how do I say that an option is
required.
How do I gracefully tell the user that an option they provided is
non-existent. I dont want ruby to crash with a big bang and a lot of
error output.
At at last I would like to appologies if this are noobish questions. Im
new to ruby and Im struggling to get a hang of it. But I dont read ruby
code flawlessly and I dont always get the examples that the gems provide
me.
Try something like this:
require ‘optparse’
opts = OptionParser.new do |opts|
#value for option is required:
opts.on("-t=ARG") do |val|
puts “-t option entered with value=#{val}”
end
#value for option is optional:
opts.on("-y [=RANDOM_CHARS]") do |val|
puts “-y option entered with value=#{val}”
end
end
begin #option -t is required: #----------------------
found_opt_t = false
ARGV.each do |word|
if word[0, 2] == ‘-t’
found_opt_t = true
end
end
if not found_opt_t
puts “Option -t is required. Exiting…”
exit
end #----------------------
opts.parse!(ARGV)
rescue OptionParser::MissingArgument => e
puts e.to_str.capitalize.gsub(“argument:”, “value for:”)
puts “That option requires a value. Please try again.”
puts “Exiting…”
exit
end
Test run:
$ ruby r8test.rb -t 10 -y 15
-t option entered with value=10
-y option entered with value=15
$ ruby r8test.rb -t10
-t option entered with value=10
$ ruby r8test.rb -t
Missing value for: -t
That option requires a value. Please try again.
Exiting…