Class: OptionBinder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/optbind.rb

Defined Under Namespace

Modules: Arguable, Switch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser: nil, target: nil, bind: nil) {|_self| ... } ⇒ OptionBinder

Returns a new instance of OptionBinder.

Yields:

  • (_self)

Yield Parameters:

  • _self (OptionBinder)

    the object that the method was called on



7
8
9
10
11
12
# File 'lib/optbind.rb', line 7

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

#parserObject (readonly)

Returns the value of attribute parser.



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

def parser
  @parser
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#argument(*opts, &handler) ⇒ Object Also known as: arg



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/optbind.rb', line 86

def argument(*opts, &handler)
  opts, handler, bound, variable, default = *several_variants(*opts, &handler)

  opts.each do |opt|
    (opts << :MULTIPLE) and break if opt.to_s =~ /<\S+>\.{3}/
  end

  (@argument_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable }
  (@bound_variables_with_defaults ||= {})[variable] = default if bound
  self
end

#assigned?(v) ⇒ Boolean

Returns:



124
125
126
127
# File 'lib/optbind.rb', line 124

def assigned?(v)
  return nil unless bound? v
  (@assigned_variables_with_values || {}).has_key? v.to_sym
end

#assigned_variablesObject



109
110
111
112
# File 'lib/optbind.rb', line 109

def assigned_variables
  return {} unless @assigned_variables_with_values
  @assigned_variables_with_values.dup
end

#bound?(v) ⇒ Boolean

Returns:



120
121
122
# File 'lib/optbind.rb', line 120

def bound?(v)
  (@bound_variables_with_defaults || {}).has_key? v.to_sym
end

#bound_defaultsObject



100
101
102
# File 'lib/optbind.rb', line 100

def bound_defaults
  @bound_variables_with_defaults ? @bound_variables_with_defaults.dup : {}
end

#bound_variablesObject



104
105
106
107
# File 'lib/optbind.rb', line 104

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

Returns:



114
115
116
117
118
# File 'lib/optbind.rb', line 114

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/optbind.rb', line 68

def option(*opts, &handler)
  opts, handler, bound, variable, default = *several_variants(*opts, &handler)

  @parser.on(*opts) do |r|
    if opts.include? :REQUIRED
      a = opts.select { |o| o =~ /\A-/ }.sort_by { |o| o.length }[-1]
      @parser.abort "missing argument: #{a}=" if !r || (r.respond_to?(:empty?) && r.empty?)
    end

    handle! handler, r, bound, variable, default
  end

  (@bound_variables_with_defaults ||= {})[variable] = default if bound
  self
end

#usage(*args) ⇒ Object Also known as: use



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/optbind.rb', line 53

def usage(*args)
  line = (args * ' ') << "\n"

  if @parser.banner =~ /\Ausage:.+\n\n/i
    @parser.banner = "usage: #{program} " << line
    @parser.separator "\n"
  else
    @parser.banner << "   or: #{program} " << line
  end

  self
end