Class: OptParse2::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/optparse2/context.rb

Overview

A Context is in charge of all option parsing

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(into:, nonopt:) ⇒ Context

Returns a new instance of Context.



18
19
20
21
22
23
24
25
# File 'lib/optparse2/context.rb', line 18

def initialize(into:, nonopt:)
  @into = into
  @nonopt = nonopt

  @already_parsed_options = {}
  @non_options = []
  @deferred_options = {}
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



5
6
7
# File 'lib/optparse2/context.rb', line 5

def current
  @current
end

Instance Attribute Details

#already_parsed_optionsObject (readonly)

Returns the value of attribute already_parsed_options.



16
17
18
# File 'lib/optparse2/context.rb', line 16

def already_parsed_options
  @already_parsed_options
end

#deferred_optionsObject (readonly)

Returns the value of attribute deferred_options.



16
17
18
# File 'lib/optparse2/context.rb', line 16

def deferred_options
  @deferred_options
end

#non_optionsObject (readonly)

Returns the value of attribute non_options.



16
17
18
# File 'lib/optparse2/context.rb', line 16

def non_options
  @non_options
end

Class Method Details

.with_contextObject



7
8
9
10
11
12
13
# File 'lib/optparse2/context.rb', line 7

def with_context(...)
  context = new(...)
  old, self.current = current, context
  yield context
ensure
  self.current = old
end

Instance Method Details

#[]=(key, value) ⇒ Object

Directly assigns ‘value` to `key`, both in the internal list of parsed options, as well as in the `into:` option (if one is provided).

Note that if ‘value` is `DONT_ASSIGN` nothing happens



45
46
47
48
49
# File 'lib/optparse2/context.rb', line 45

def []=(key, value)
  return if DONT_ASSIGN.equal?(value)
  @already_parsed_options[key] = value
  @into[key] = value if @into
end

#add_non_option(non_option) ⇒ Object



27
28
29
# File 'lib/optparse2/context.rb', line 27

def add_non_option(non_option)
  @non_options << non_option
end

#handle_deferred!Object



31
32
33
34
35
# File 'lib/optparse2/context.rb', line 31

def handle_deferred!
  @deferred_options.each do |key, deferred|
    self[key] = deferred[:proc].call(deferred[:data]) if deferred[:proc]
  end
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/optparse2/context.rb', line 37

def key?(key)
  @already_parsed_options.key?(key)
end