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

Instance Attribute Details

#switch_nameObject



14
15
16
# File 'lib/optparse2/switch-helpers.rb', line 14

def switch_name
  defined?(@switch_name) ? @switch_name : super
end

Class Method Details

.summarizeObject



7
# File 'lib/optparse2/switch-helpers.rb', line 7

def self.summarize(*) end

Instance Method Details

#defaultObject



175
# File 'lib/optparse2/switch-helpers.rb', line 175

def default = @default_proc&.call(switch_name)

#default?Boolean

Returns:

  • (Boolean)


174
# File 'lib/optparse2/switch-helpers.rb', line 174

def default? = defined?(@default_proc)

#default_bypass?Boolean

Calls the default proc to figure out what the default value is for this switch

Returns:

  • (Boolean)


173
# File 'lib/optparse2/switch-helpers.rb', line 173

def default_bypass? = @default_bypass

#default_descriptionObject



177
# File 'lib/optparse2/switch-helpers.rb', line 177

def default_description = @default_description || default.inspect

#descObject



178
179
180
181
182
183
184
# File 'lib/optparse2/switch-helpers.rb', line 178

def desc
  return super unless defined? @default_proc
  x = super
  x << '' if x.empty?
  x[-1] += " [default: #{default_description}]"
  x
end

#required?Boolean

Returns:

  • (Boolean)


148
# File 'lib/optparse2/switch-helpers.rb', line 148

def required? = @required

#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)



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/optparse2/switch-helpers.rb', line 153

def set_default(value, description, bypass)
  if required?
    raise ArgumentError, 'cannot supply a default value for a required argument'
  end

  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_hiddenObject

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.deferred_options.key? sw
        OptParse2::DONT_ASSIGN
      else
        ctx.deferred_options[sw] = {}
        old_block ? old_block.call(arg) : arg
      end
    end
  in :first
    @block = ->(arg, **nil) do
      ctx = OptParse2::Context.current
      if ctx.deferred_options.key? sw
        OptParse2::DONT_ASSIGN
      else
        ctx.deferred_options[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.deferred_options[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.already_parsed_options.key? sw or ctx.deferred_options.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.deferred_options[sw] ||= {
        proc: old_block ? proc { |data| old_block.call(data) } : proc { |data| data },
        data: 0
      }

      if amnt == true || amnt.nil?
        ctx.deferred_options[sw][:data] += 1
      else
        ctx.deferred_options[sw][:data] = amnt
      end

      OptParse2::DONT_ASSIGN
    end

  in :count!
    @block = ->(amnt, *a, **k, &b) do
      ctx = OptParse2::Context.current

      ctx.deferred_options[sw] ||= { data: 0 }

      if amnt == true || amnt.nil?
        ctx.deferred_options[sw][:data] += 1
      else
        ctx.deferred_options[sw][:data] = amnt
      end

      new_amnt = old_block ? old_block.call(ctx.deferred_options[sw][:data], *a, **k, &b) : ctx.deferred_options[sw][:data]
      ctx.deferred_options[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.deferred_options[sw] ||= {
        proc: proc { |data| old_block ? old_block.call(data) : data },
        data: []
      }

      ctx.deferred_options[sw][:data] << (transform ? transform.(arg) : arg)
      OptParse2::DONT_ASSIGN
    end

  else
    raise ArgumentError, "invalid multiple type: #{multiple}", caller(2)
  end
end

#set_required(required) ⇒ Object

Required arguments must be supplied by the user. They conflict with ‘default` options, and cause an error if both required and default are specified



141
142
143
144
145
146
# File 'lib/optparse2/switch-helpers.rb', line 141

def set_required(required)
  if default?
    raise ArgumentError, 'cannot require an argument with a default value'
  end
  @required = required
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