Qqwy
Qqwy

Reputation: 5649

Ruby OptionParser: how to handle arguments without a prefix (like a required filename)

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?

Upvotes: 9

Views: 2470

Answers (1)

Kristján
Kristján

Reputation: 18843

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"]

Upvotes: 7

Related Questions