Class: OptionBinder
Defined Under Namespace
Modules: Arguable, Switch Classes: MissingArguments, TooManyArguments
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #argument(*opts, &handler) ⇒ Object (also: #arg)
- #assigned?(v) ⇒ Boolean
- #assigned_variables ⇒ Object
- #bound?(v) ⇒ Boolean
- #bound_defaults ⇒ Object
- #bound_variables ⇒ Object
- #default?(v) ⇒ Boolean
-
#initialize(parser: nil, target: nil, bind: nil) {|_self| ... } ⇒ OptionBinder
constructor
A new instance of OptionBinder.
- #option(*opts, &handler) ⇒ Object (also: #opt)
- #parse(argv) ⇒ Object
- #parse!(argv) ⇒ Object
- #usage(*args) ⇒ Object (also: #use)
Constructor Details
#initialize(parser: nil, target: nil, bind: nil) {|_self| ... } ⇒ OptionBinder
Returns a new instance of OptionBinder.
9 10 11 12 13 14 |
# File 'lib/optbind.rb', line 9 def initialize(parser: nil, target: nil, bind: nil) target, bind = TOPLEVEL_BINDING, :to_local_variables if target == nil && bind == nil @parser = resolve_parser(parser) @target, @reader, @writer = target, *resolve_binding(target, bind) yield self if block_given? end |
Instance Attribute Details
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
7 8 9 |
# File 'lib/optbind.rb', line 7 def parser @parser end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
7 8 9 |
# File 'lib/optbind.rb', line 7 def target @target end |
Instance Method Details
#argument(*opts, &handler) ⇒ Object Also known as: arg
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/optbind.rb', line 94 def argument(*opts, &handler) opts, handler, bound, variable, default = *several_variants(*opts, &handler) (@argument_parser ||= OptionParser.new).on(*(opts << "--#{(@argument_definitions || []).size}")) do |r| raise OptionParser::InvalidArgument if opts.include?(:REQUIRED) && (r.nil? || r.respond_to?(:empty?) && r.empty?) handle! handler, r, bound, variable, default end (@argument_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable } (@bound_variables_with_defaults ||= {})[variable] = default if bound self end |
#assigned?(v) ⇒ Boolean
133 134 135 136 |
# File 'lib/optbind.rb', line 133 def assigned?(v) return nil unless bound? v (@assigned_variables_with_values || {}).key? v.to_sym end |
#assigned_variables ⇒ Object
118 119 120 121 |
# File 'lib/optbind.rb', line 118 def assigned_variables return {} unless @assigned_variables_with_values @assigned_variables_with_values.dup end |
#bound?(v) ⇒ Boolean
129 130 131 |
# File 'lib/optbind.rb', line 129 def bound?(v) (@bound_variables_with_defaults || {}).key? v.to_sym end |
#bound_defaults ⇒ Object
109 110 111 |
# File 'lib/optbind.rb', line 109 def bound_defaults @bound_variables_with_defaults ? @bound_variables_with_defaults.dup : {} end |
#bound_variables ⇒ Object
113 114 115 116 |
# File 'lib/optbind.rb', line 113 def bound_variables return {} unless @bound_variables_with_defaults Hash[@bound_variables_with_defaults.keys.map { |v| [v, @reader.call(v)] }] end |
#default?(v) ⇒ Boolean
123 124 125 126 127 |
# File 'lib/optbind.rb', line 123 def default?(v) v = v.to_sym return nil unless bound? v (@bound_variables_with_defaults[v] || {}) == @reader.call(v) end |
#option(*opts, &handler) ⇒ Object Also known as: opt
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/optbind.rb', line 79 def option(*opts, &handler) opts, handler, bound, variable, default = *several_variants(*opts, &handler) @parser.on(*opts) do |r| raise OptionParser::InvalidArgument if opts.include?(:REQUIRED) && (r.nil? || r.respond_to?(:empty?) && r.empty?) handle! handler, r, bound, variable, default end (@option_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable } (@bound_variables_with_defaults ||= {})[variable] = default if bound self end |
#parse(argv) ⇒ Object
50 51 52 |
# File 'lib/optbind.rb', line 50 def parse(argv) parse!(argv.dup) and argv end |
#parse!(argv) ⇒ Object
54 55 56 |
# File 'lib/optbind.rb', line 54 def parse!(argv) parse_args! @parser.parse! argv end |
#usage(*args) ⇒ Object Also known as: use
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/optbind.rb', line 64 def usage(*args) line = (args * ' ') << "\n" if @parser. =~ /\Ausage:.+\n\n/i @parser. = "usage: #{program} #{line}" @parser.separator "\n" else @parser. += " or: #{program} #{line}" end self end |