Class: OptParse2::Context
- Inherits:
-
Object
- Object
- OptParse2::Context
- Defined in:
- lib/optparse2/context.rb
Overview
A Context is in charge of all option parsing
Class Attribute Summary collapse
-
.current ⇒ Object
Returns the value of attribute current.
Instance Attribute Summary collapse
-
#already_parsed_options ⇒ Object
readonly
Returns the value of attribute already_parsed_options.
-
#deferred_options ⇒ Object
readonly
Returns the value of attribute deferred_options.
-
#non_options ⇒ Object
readonly
Returns the value of attribute non_options.
Class Method Summary collapse
Instance Method Summary collapse
-
#[]=(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).
- #add_non_option(non_option) ⇒ Object
- #handle_deferred! ⇒ Object
-
#initialize(into:, nonopt:) ⇒ Context
constructor
A new instance of Context.
- #key?(key) ⇒ Boolean
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
.current ⇒ Object
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_options ⇒ Object (readonly)
Returns the value of attribute already_parsed_options.
16 17 18 |
# File 'lib/optparse2/context.rb', line 16 def @already_parsed_options end |
#deferred_options ⇒ Object (readonly)
Returns the value of attribute deferred_options.
16 17 18 |
# File 'lib/optparse2/context.rb', line 16 def @deferred_options end |
#non_options ⇒ Object (readonly)
Returns the value of attribute non_options.
16 17 18 |
# File 'lib/optparse2/context.rb', line 16 def @non_options end |
Class Method Details
.with_context ⇒ Object
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
37 38 39 |
# File 'lib/optparse2/context.rb', line 37 def key?(key) @already_parsed_options.key?(key) end |