Module: OptParse2::Helpers
- Defined in:
- lib/optparse2/switch-helpers.rb
Overview
Helpers is a mixin that contains methods to modify how the original ‘Switch` works
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #default ⇒ Object
- #default? ⇒ Boolean
-
#default_bypass? ⇒ Boolean
Calls the default proc to figure out what the default value is for this switch.
- #default_description ⇒ Object
- #desc ⇒ Object
-
#set_default(value, description, bypass) ⇒ Object
Default values of switches are used when the switch is never passed in.
-
#set_hidden ⇒ Object
Mark the switch as hidden (so it won’t show up in the usage).
- #set_multiple(multiple) ⇒ Object
-
#set_switch_name_possibly_block_value(val) ⇒ Object
Same as ‘switch_name`, except it also will set the block to just return the original switch name as a symbol.
Instance Attribute Details
#switch_name ⇒ Object
14 15 16 |
# File 'lib/optparse2/switch-helpers.rb', line 14 def switch_name defined?(@switch_name) ? @switch_name : super end |
Class Method Details
.summarize ⇒ Object
7 |
# File 'lib/optparse2/switch-helpers.rb', line 7 def self.summarize(*) end |
Instance Method Details
#default ⇒ Object
160 |
# File 'lib/optparse2/switch-helpers.rb', line 160 def default = @default_proc&.call(switch_name) |
#default? ⇒ Boolean
159 |
# File 'lib/optparse2/switch-helpers.rb', line 159 def default? = defined?(@default_proc) |
#default_bypass? ⇒ Boolean
Calls the default proc to figure out what the default value is for this switch
158 |
# File 'lib/optparse2/switch-helpers.rb', line 158 def default_bypass? = @default_bypass |
#default_description ⇒ Object
162 |
# File 'lib/optparse2/switch-helpers.rb', line 162 def default_description = @default_description || default.inspect |
#desc ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/optparse2/switch-helpers.rb', line 163 def desc return super unless defined? @default_proc x = super x << '' if x.empty? x[-1] += " [default: #{default_description}]" x end |
#set_default(value, description, bypass) ⇒ Object
Default values of switches are used when the switch is never passed in. If the ‘value` that’s provided doesn’t respond to ‘.call`, it’s converted to a proc. If ‘bypass` is truthy, then the default value is never passed to the block’s proc (if any)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/optparse2/switch-helpers.rb', line 142 def set_default(value, description, bypass) if @arg.nil? && value != true && !bypass raise ArgumentError, "Cannot supply a non-true default value to a flag which takes no arguments", caller(4) end if defined? value.call @default_proc = value else @default_proc = proc { |_switch_name| value } end @default_description = description @default_bypass = bypass end |
#set_hidden ⇒ Object
Mark the switch as hidden (so it won’t show up in the usage)
6 7 8 |
# File 'lib/optparse2/switch-helpers.rb', line 6 def set_hidden def self.summarize(*) end end |
#set_multiple(multiple) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/optparse2/switch-helpers.rb', line 18 def set_multiple(multiple) old_block = @block sw = switch_name.to_sym case multiple in :first! @block = ->(arg, **nil) do ctx = OptParse2::Context.current if ctx..key? sw OptParse2::DONT_ASSIGN else ctx.[sw] = {} old_block ? old_block.call(arg) : arg end end in :first @block = ->(arg, **nil) do ctx = OptParse2::Context.current if ctx..key? sw OptParse2::DONT_ASSIGN else ctx.[sw] = { proc: old_block ? proc{ old_block.call(arg) } : proc { arg } } OptParse2::DONT_ASSIGN end end in :last!, nil # don't do anything, this is the default behaviour in :last @block = ->(arg, **nil) do ctx = OptParse2::Context.current ctx.[sw] = { proc: old_block ? proc { old_block.call(arg) } : proc { arg } } OptParse2::DONT_ASSIGN end in :raise @block = ->(arg) do ctx = OptParse2::Context.current if ctx..key? sw or ctx..key? sw raise OptParse2::ParseError, "encountered repeated option" end old_block ? old_block.call(arg) : arg end in :count @block = ->(amnt, **nil) do ctx = OptParse2::Context.current ctx.[sw] ||= { proc: old_block ? proc { |data| old_block.call(data) } : proc { |data| data }, data: 0 } if amnt == true || amnt.nil? ctx.[sw][:data] += 1 else ctx.[sw][:data] = amnt end OptParse2::DONT_ASSIGN end in :count! @block = ->(amnt, *a, **k, &b) do ctx = OptParse2::Context.current ctx.[sw] ||= { data: 0 } if amnt == true || amnt.nil? ctx.[sw][:data] += 1 else ctx.[sw][:data] = amnt end new_amnt = old_block ? old_block.call(ctx.[sw][:data], *a, **k, &b) : ctx.[sw][:data] ctx.[sw][:data] = new_amnt unless OptParse2::DONT_ASSIGN.equal? new_amnt new_amnt end in :collect | [:collect, _] transform = Array(multiple)[1] @block = ->(arg, **nil) do ctx = OptParse2::Context.current ctx.[sw] ||= { proc: proc { |data| old_block ? old_block.call(data) : data }, data: [] } ctx.[sw][:data] << (transform ? transform.(arg) : arg) OptParse2::DONT_ASSIGN end else raise ArgumentError, "invalid multiple type: #{multiple}", caller(2) end end |
#set_switch_name_possibly_block_value(val) ⇒ Object
Same as ‘switch_name`, except it also will set the block to just return the original switch name as a symbol. Useful for group switches which don’t actually have blocks:
op.on '--interactive', key: :mode
op.on '--force', key: :mode
instead of:
op.on '--interactive', key: :mode do :interactive end
op.on '--force', key: :mode do :force end
without this method, passing ‘–interactive` would just set `:mode` to `true`.
This only happens if no block exists, and the argument does not take an arg.
130 131 132 133 134 135 136 137 |
# File 'lib/optparse2/switch-helpers.rb', line 130 def set_switch_name_possibly_block_value(val) if @block.nil? && @arg.nil? old_switch_name = switch_name.to_sym @block = proc { old_switch_name } end self.switch_name = val end |