Ruby optparse: Difference between revisions

From wikinotes
No edit summary
 
No edit summary
Line 5: Line 5:
require 'optparse'
require 'optparse'


options = OpenStruct.new
options = {}
OptionsParser.new do |opt|
OptionParser.new do |opt|
   opt.on("-i", "--input FILE", "the input file") { |val| options[:input] = val }
   opt.on("-i", "--input FILE", "the input file") { |val| options[:input] = val }
   opt.on("-o", "--output FILE", "the output file") { |val| options[:output] = val }
   opt.on("-o", "--output FILE", "the output file") { |val| options[:output] = val }

Revision as of 16:42, 12 October 2021

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)