Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 953
Qqwy Avatar asked Aug 14 '15 14:08

Qqwy


1 Answers

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"]
like image 132
Kristján Avatar answered Nov 13 '22 18:11

Kristján