Ruby optparse: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 2: | Line 2: | ||
It is builtin to the standard library. | It is builtin to the standard library. | ||
= Documentation = | |||
<blockquote> | |||
{| class="wikitable" | |||
|- | |||
| official docs || https://docs.ruby-lang.org/en/2.7.0/OptionParser.html | |||
|- | |||
|} | |||
</blockquote><!-- Documentation --> | |||
= Example = | |||
<blockquote> | |||
<source lang="ruby"> | <source lang="ruby"> | ||
require 'optparse' | require 'optparse' | ||
options = | options = {} | ||
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 } | ||
Line 15: | Line 26: | ||
exit(1) | exit(1) | ||
</source> | </source> | ||
</blockquote><!-- Example --> |
Latest revision as of 16:44, 29 October 2022
Optparse is a basic commandline parser for ruby.
It is builtin to the standard library.
Documentation
official docs https://docs.ruby-lang.org/en/2.7.0/OptionParser.html
Example
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)