How does Ruby's OptionParser work? -
"minimal example" of optionparser http://ruby-doc.org/stdlib-2.1.5/libdoc/optparse/rdoc/optionparser.html:
require 'optparse' options = {} optionparser.new |opts| opts.banner = "usage: example.rb [options]" opts.on("-v", "--[no-]verbose", "run verbosely") |v| options[:verbose] = v end end.parse! p options p argv
main questions:
- what content of
opts
there? new optionparser instance, or of/-\w/
or/--\w+/
-looking things passed script? corollary,do
block loop or not? - what
parse!
do? why called on wholedo
block?
also wondering:
- what
optionparser#banner
method? in context see text? - in context see third parameter passed optionparser in example, little description of flag's effect?
- how can create custom error message if script run unknown option?
opts
new instance ofoptionparser
. block supplied.new
run line:yield self if block_given?
parse!
same thingparse
destructive, meaning remove used switchesargv
. called on entiredo ... end
block because value returned newoptionparser
instance.banner
gets heading of summary, can setopts.banner = "foo"
the description shown when displayed (
-h
flag):usage: example.rb [options] -v, --[no-]verbose run verbosely
you rescue
optionparser::invalidoption
exception:parser = optionparser.new ... begin parser.parse! rescue optionparser::invalidoption puts 'invalid args!' end
Comments
Post a Comment