Ruby optparse

From wikinotes
Revision as of 16:42, 12 October 2021 by Will (talk | contribs)

Optparse is a basic commandline parser for ruby.
It is builtin to the standard library.

require 'optparse'

options = {}
OptionParser.new do |opt|
  opt.on("-i", "--input FILE", "the input file") { |val| options[:input] = val }
  opt.on("-o", "--output FILE", "the output file") { |val| options[:output] = val }
end.parse!
return options if options.input && options.output

STDERR.puts "[ERROR] requires -i/-o"
exit(1)