Class: OptParse2
- Inherits:
-
OptParse
- Object
- OptParse
- OptParse2
- Defined in:
- lib/optparse2.rb,
lib/optparse2.rb,
lib/optparse2/fixes.rb,
lib/optparse2/version.rb
Overview
require_relative “optparse2/helpers”
Defined Under Namespace
Modules: Globals, Helpers, Pathname
Constant Summary collapse
- VERSION =
"0.2.2"
Instance Method Summary collapse
- #def_head_option ⇒ Object
-
#define(*opts, &block) ⇒ Object
(also: #def_option)
Provide support for passing keyword arguments into
make_switch. - #define_head(*opts, &block) ⇒ Object
-
#initialize ⇒ OptParse2
constructor
A new instance of OptParse2.
-
#make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil) ⇒ Object
Update
make_switchto support OptParse2’s keyword arguments. - #on ⇒ Object
- #on_head ⇒ Object
- #on_tail ⇒ Object
- #order!(argv = default_argv, into: nil, **keywords, &nonopt) ⇒ Object
-
#summary(msg) ⇒ Object
Provide a “summary” field, which just puts the message at the end of the usage.
Constructor Details
#initialize ⇒ OptParse2
Returns a new instance of OptParse2.
12 13 14 15 |
# File 'lib/optparse2.rb', line 12 def initialize(...) @defaults = Set[] super end |
Instance Method Details
#def_head_option ⇒ Object
8 |
# File 'lib/optparse2/fixes.rb', line 8 def define_head(*opts, **, &block) top.prepend(*(sw = make_switch(opts, block, **))); sw[0] end |
#define(*opts, &block) ⇒ Object Also known as: def_option
Provide support for passing keyword arguments into make_switch
3 |
# File 'lib/optparse2/fixes.rb', line 3 def define(*opts, **, &block) top.append(*(sw = make_switch(opts, block, **))); sw[0] end |
#define_head(*opts, &block) ⇒ Object
7 |
# File 'lib/optparse2/fixes.rb', line 7 def define_head(*opts, **, &block) top.prepend(*(sw = make_switch(opts, block, **))); sw[0] end |
#make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil) ⇒ Object
Update make_switch to support OptParse2’s keyword arguments
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/optparse2.rb', line 61 def make_switch(opts, block, hidden: false, key: nil, default: nodefault=true, default_description: nil) sw, *rest = super(opts, block) sw.extend Helpers if key sw.switch_name = key sw.set_block_name_because_of_switch_name end sw.set_hidden if hidden if (not_style = rest[2]) not_style.extend Helpers not_style.switch_name = key if key not_style.set_hidden if hidden end if nodefault && default_description != nil raise ArgumentError, "default: not supplied, but default_description: given" elsif not nodefault sw.set_default(default, default_description) @defaults << sw end [sw, *rest] end |
#on ⇒ Object
5 |
# File 'lib/optparse2/fixes.rb', line 5 def on(...) define(...); self end |
#on_head ⇒ Object
9 |
# File 'lib/optparse2/fixes.rb', line 9 def on_head(...) define_head(...); self end |
#on_tail ⇒ Object
13 |
# File 'lib/optparse2/fixes.rb', line 13 def on_tail(...) define_tail(...); self end |
#order!(argv = default_argv, into: nil, **keywords, &nonopt) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/optparse2.rb', line 92 def order!(argv = default_argv, into: nil, **keywords, &nonopt) if into.nil? && !@defaults.empty? raise "cannot call `order!` without an `into:` if there are default values" end already_done = {} already_done.define_singleton_method(:[]=) do |key, value| super(key, value) into[key] = value end result = super(argv, into: already_done, **keywords, &nonopt) @defaults.each do |sw| key = sw.switch_name.to_sym next if already_done.key? key into[key] = sw.default() end result end |
#summary(msg) ⇒ Object
Provide a “summary” field, which just puts the message at the end of the usage
88 89 90 |
# File 'lib/optparse2.rb', line 88 def summary(msg) on_tail("\n" + msg) end |