I am working with OptionParser for the first time.
What I would like to know, is how I can make OptionParser handle arguments that are not prefixed with a certain flagname. I want to be able to write a statement like:
myscript.rb -d someoption -b someotheroption filename
where filename
is the name of the file I want to work on. It is not prefixed by any option flag. How can I parse commands like the one above with OptionParser, and get a reference to filename
?
OptionParser
specifically handles options - that is, things starting with dashes. After it parses, the remaining arguments are left in ARGV
. You can check for your filename there and exit with an error if it's missing.
With a slight modification on their minimal example,
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: example.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
end.parse!
p options
p ARGV
p "Where is my hat?!" if ARGV.length == 0
You get this:
$ ruby parse.rb
{}
[]
"Where is my hat?!"
$ ruby parse.rb hat
{}
["hat"]
$ ruby parse.rb -v hat
{:verbose=>true}
["hat"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With