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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/optbind.rb', line 112

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

#bound_defaultsObject



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

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

#bound_variablesObject



130
131
132
133
# File 'lib/optbind.rb', line 130

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:



135
136
137
138
139
# File 'lib/optbind.rb', line 135

def default?(v)
  v = v.to_sym
  return nil unless (@bound_variables_with_defaults || {}).has_key? v
  @bound_variables_with_defaults[v] == @reader.call(v)
end

#option(*opts, &handler) ⇒ Object Also known as: opt



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/optbind.rb', line 94

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

    (handler || -> (_) { r }).call(r == nil ? default : r).tap { |x| @writer.call variable, x if bound }
  end

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

#order(*argv, &blk) ⇒ Object



43
44
45
46
# File 'lib/optbind.rb', line 43

def order(*argv, &blk)
  @parser.order *argv, &blk
  parse_args argv
end

#order!(argv) ⇒ Object



48
49
50
51
# File 'lib/optbind.rb', line 48

def order!(argv)
  @parser.order! argv
  parse_args argv
end

#parse(*argv) ⇒ Object



63
64
65
66
# File 'lib/optbind.rb', line 63

def parse(*argv)
  @parser.parse *argv
  parse_args argv
end

#parse!(argv) ⇒ Object



68
69
70
71
# File 'lib/optbind.rb', line 68

def parse!(argv)
  @parser.parse! argv
  parse_args! argv
end

#permute(*argv) ⇒ Object



53
54
55
56
# File 'lib/optbind.rb', line 53

def permute(*argv)
  @parser.permute *argv
  parse_args argv
end

#permute!(argv) ⇒ Object



58
59
60
61
# File 'lib/optbind.rb', line 58

def permute!(argv)
  @parser.permute! argv
  parse_args argv
end

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



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/optbind.rb', line 79

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